Java会在循环中覆盖文件

时间:2017-09-13 12:38:03

标签: java loops

我似乎无法找到方法,所以每次我继续循环时文件都不会覆盖。似乎无法找到保存数组的旧值,因此它不会覆盖。

将用户输入写入数组:

Scanner menu = new Scanner(System.in);
 int sakums = menu.nextInt();
 switch(sakums)
 {
 case 1:
Scanner in = new Scanner(System.in);

//get the input for number of students:
System.out.println("Studentu skaits:");
int totalstudents = in.nextInt();
in.nextLine();// just to ignore the line
//store into String array

String studentname[] = new String[totalstudents];

for(int i = 0; i < studentname.length;i++)
{

    System.out.println("Ievadi Uzvardu Vardu : "+i);
    studentname[i] = in.nextLine();
}
System.out.println("-----------------Ievade------------------------");
for(String names:studentname)
{

    System.out.println(names);
}

将数组写入文件:

FileWriter fw = new FileWriter("C:\\input.txt");
for (int i = 0; i < studentname.length; i++) {
  fw.write(studentname[i] + "\n");
  int jaunais = i;
}
fw.close();

      break;
      case 2:

0 个答案:

没有答案