无法使用for创建.txt

时间:2014-03-09 15:13:52

标签: java syntax-error

我想在Eclipse中使用for创建10个txt文件 为什么当我这样做时我得到错误?

for (int i=0; i< 10; i++) 
File i = new File("C:\\Users\\User\\Desktop\\FOLDER- OS test\\primer" + i + ".txt");
Multiple markers at this line
    - Syntax error, insert "AssignmentOperator Expression" to complete 
     Assignment
    - File cannot be resolved to a variable
    - Syntax error, insert ";" to complete Statement
    - i cannot be resolved to a variable
    - i cannot be resolved to a variable

1 个答案:

答案 0 :(得分:3)

您正尝试重新分配已定义为i的变量int。只需为文件使用另一个变量名称,如下所示:

for (int i=0; i< 10; i++) {
   File foo = new File("C:\\Users\\User\\Desktop\\FOLDER- OS test\\primer" + i + ".txt");
   foo.createNewFile();
}