所以我们开始学习一些文件处理,我正在编写一些代码来输出和文件到文本,当编写该文件时我需要再次打开它并提取信息然后将该信息输出到另一个文件,我遇到的问题是,当我去提取该信息时,它似乎覆盖了两个文件,它们最终都是空白的。我的switch语句中的代码的第一部分有效,但我似乎无法弄清楚我的错误在哪里,任何帮助都会非常感激! =]
所以这是我的行处理程序
public class AssignementHandler {
private String studentNo;
private String studentName;
private int result1;
public String getstudentNo() {
return studentNo;
}
public String getstudentName() {
return studentName;
}
public int getResult1() {
return result1;
}
public static boolean checkStringForDigits(String s){
for(int i = 1; i < 8; i++){
if(!Character.isDigit(s.charAt(i)))
return false;
}return true;
}
public void lineSeperate(String line){
//read each char until I find a digit
int i = 0;
while(!Character.isWhitespace(line.charAt(i)))//while it is not a space
i++; //keep going
//stops when I find a space - so this is the start of the name
studentNo = line.substring(0, i).trim();
int nameStart = i;
while(!Character.isDigit(line.charAt(i)))//while it is not a space
i++; //keep going
studentName = line.substring(nameStart, i).trim();
result1 = Integer.parseInt(line.substring(i).trim());
}
然后我的测试人员如下
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
public class studentTester {
public static void main(String[] args) throws IOException, FileNotFoundException, RuntimeException {
@SuppressWarnings("resource")
Scanner keyIn = new Scanner(System.in);
int option;
do{
System.out.println("\nStudent Application\n*******************");
System.out.println("1. Enter Student Details");
System.out.println("2. Write test marks to results.txt and highest.txt");
System.out.println("3. Display contents of any file on screen");
System.out.println("0. Quit\n*******************");
System.out.print("\nPlease enter option: ");
option = keyIn.nextInt();
String studentNo;
String studentName;
int result1;
PrintWriter studentOUT = new PrintWriter(new FileWriter("student.txt"));
AssignementHandler handlelines = new AssignementHandler();
switch (option) { //start switch
case 1: //add Student Details - Menu Option 1
char userResponse = 'y';
System.out.print("\nOption Selected - Enter Student Details" + "\n------------------\n");
keyIn.nextLine(); //clear buffer of option 1
System.out.print("Please enter student number in the format L0000000: ");
studentNo = keyIn.nextLine();
if(studentNo.length() !=9){
System.out.println("Incorrect Length: Must have 1 Letter and 8 Numbers!");
continue;
}
else if(!(Character.isLetter(studentNo.charAt(0)))){
System.out.println("Student Number must begin with a Letter!");
continue;
}
else if(!AssignementHandler.checkStringForDigits(studentNo)){
//if it returns false
System.out.println("Incorrect Format, Student Number Must Have 8 numbers!" );
continue;
}
System.out.print("Please Enter Student Name: ");
studentName = keyIn.nextLine();
System.out.print("Please Enter Result - (1): ");
result1 = keyIn.nextInt();
studentOUT.println(studentNo + " " + studentName + " " + result1);
do{
System.out.print("Do you want to add another student?: \n" +
"(Y) or (N)");
userResponse = keyIn.next().charAt(0);
keyIn.nextLine(); //clear buffer
}while (userResponse != 'n');
studentOUT.close();
System.out.println("Student Records have been written successfully to - student.txt - !");
break;
////////////////////////////////////////////////////////////////////////////////////////////
case 2: //write test marks to results.txt and highest.txt - Menu Option 2
System.out.print("\nOption Selected - Write Files" + "\n------------------\n");
System.out.println("Files have been written successfully!");
keyIn.nextLine(); //clear buffer of option 2
Scanner studentsIN = new Scanner(new File("student.txt"));
try{
PrintWriter resultsOut = new PrintWriter( new FileWriter("results.txt", true));
try{
while(studentsIN.hasNextLine()){
String line = studentsIN.nextLine(); //read the next line
//System.out.println(line); //for testing
//process the line
handlelines.lineSeperate(line);
String studentNumber = handlelines.getstudentNo();
//String studName = handlelines.getstudentName();
int resultIN = handlelines.getResult1();
//write to the file
resultsOut.printf(studentNumber+""+resultIN+"");
}//end while
}//inner try - files are open and being processed
finally{
studentsIN.close();
resultsOut.close();
}
}//end outer try
catch(FileNotFoundException ex){
System.out.println(ex);
}
catch(RuntimeException ex){
System.out.println(ex);
}
catch(IOException ex){
System.out.println(ex);
}
break;
////////////////////////////////////////////////////////////////////////////////////////////
case 0:
System.out.println("Results Application shutting down...\n******** Goodbye ********"); //quit program
break;
default:
System.out.println("Invalid option entered ");
}//end switch
}while (option != 0); //end user interface
}//end main method
}//end class studentTester
它似乎写了两个空白文件而没有提取我需要的信息,不知道我在这里做错了,因为我正在按照我们在课堂上做的一些先前的例子。