【发布时间】:2015-02-27 23:52:27
【问题描述】:
所以我坚持做家庭作业,只需要一点帮助我有 3 个包含多个学生姓名和成绩的文本文件,格式为:
Student Name
Test 1: 93
______________
Student Name
Test 1: 99
_____________
I need to figure out a way to split the line where the grade is and skip the ------- that splits the students and their grades up. I would then need to parse the test grade into a variable so i could do calculations with it.
public void openFile() throws IOException {
//opens text file
try {
FileReader inputFile = new FileReader("C:\\Users\\Chris\\IdeaProjects\\workshop3\\src\\filereader\\file1.txt");
//delimiter from scanner object skips characters passed as parameters
kb = new Scanner(inputFile).useDelimiter("\n");
while(kb.hasNext()){
line = kb.next();
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
我现在遇到了这个方法的问题,我正在使用一个数组列表来循环写入学生。但是每次将某些内容写入指定文件时,它都会不断覆盖文件中现有的学生信息。希望有人可以提供帮助。
public void writeFile(Student stud) throws IOException{
try{
PrintWriter outputFile = new PrintWriter(new FileOutputStream("C:\\Users\\Chris\\IdeaProjects\\workshop3\\src\\filereader\\file4.txt"));
outputFile.println("\nStudent: " + stud.getName());
outputFile.println("Midterm 1: " + stud.getMid1());
outputFile.println("Midterm 2: " + stud.getMid2());
outputFile.println("Final: " + stud.getFin());
outputFile.printf("Average: %.2f" ,stud.calculateAverage(stud.getMid1(),stud.getMid2(),stud.getFin()));
outputFile.close();
}catch(IOException e){
}
}
【问题讨论】:
-
我仍然无法弄清楚为什么这个文件在写入时会不断地重写自己。
标签: java java.util.scanner delimiter