【发布时间】:2013-11-27 05:49:33
【问题描述】:
我正在尝试显示符号化项目列表并让用户选择要删除的项目。
- 我将信息写入 txt 文件。
- 我将文件的内容存储在一个数组中。
- 我打印出数组。
- 用户输入(例如 L101),它将被删除 文件。
问题出在步骤(1) 当且仅当文件不存在时,我希望写入文件。 所以我在 while 循环之前,它将用
写入文件if(file.isFile() ==false).
然而,这似乎使输出空白,就好像阅读器无法读取文件并将其存储在数组中一样。为什么? P.S 如果我不放 if 和 else 语句,输出是好的,只是它再次写入文件,我不想发生这种情况,我希望 ti 显示除用户键入的内容之外的所有内容。
这是代码
public static void main (String args[]) throws IOException
{
//Declaring the file
File tempFile = new File("D:\\myTempFile.txt");
//Declaring the writer
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
int n=0;
int L=1;
//Writing to the file if it did not exist
if(tempFile.isFile()==false)
{
while(n<15 && L<3)
{
n++;
writer.write("L"+L+"0"+n);
writer.newLine();
L++;
writer.write("L"+L+"0"+n);
writer.newLine();
L++;
writer.write("L"+L+"0"+n);
writer.newLine();
L=1;
//}
}
writer.close();
freeParkingSpaces();
}
//read the and store to array if the file exists
else if(tempFile.isFile()==true)
{
freeParkingSpaces();
}
}
public static void freeParkingSpaces() throws IOException
{
File tempFile = new File("D:\\myTempFile.txt");
//using try and catch to handle errors and print propriet message instead.
try{
Scanner readSpaces; // declare scanner variable.
//reading from file that contains the available spaces and storing them into an array.
System.out.println("Please choose one of the following parking spaces: ");
System.out.println(" ");
String [] freeSpaces= new String[45]; // declaring the array.
int i=0;
//getting input from file that contains the available spaces and storing them into an array.
readSpaces=new Scanner(new File("D://myTempFile.txt"));
System.out.print("\t\t");
//Display the values of the array which are the available spaces.
while(i<15 && readSpaces.hasNext()){
freeSpaces[i]=readSpaces.nextLine();
System.out.print(" "+freeSpaces[i]);
i++;
}
System.out.println(" ");
System.out.print("\t\t");
while(i>=15&&i<30&&readSpaces.hasNext()){
freeSpaces[i]=readSpaces.nextLine();
System.out.print(" "+freeSpaces[i]);
i++;
}
System.out.println(" ");
System.out.print("\t\t");
while(i>=30&&i<45&&readSpaces.hasNext()){
freeSpaces[i]=readSpaces.nextLine();
System.out.print(" "+freeSpaces[i]);
i++;
}
System.out.println(" ");
readSpaces.close();
//create scanner object to hold the input of a user which is a park space.
Scanner holder5=new Scanner(System.in);
System.out.print("Your choice: ");
String spaceName=holder5.nextLine();
//declaring files
File inputFile=new File("D://Temporary.txt");
// creating object to read from Free_ParkingSpaces file
BufferedReader reader=new BufferedReader(new FileReader(tempFile));
// creating object to write to the tempFile
BufferedWriter writer0=new BufferedWriter(new FileWriter(inputFile));
String currentLine;
while((currentLine = reader.readLine()) != null){
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(spaceName)) continue;//leave the required space by the user.
writer0.write(currentLine); //write the lines to the tempFile.
writer0.newLine(); // start from new line.
}
writer0.close(); //close BufferedWriter Object
reader.close(); //close BufferedReader Object
tempFile.delete(); // Delete the old available spaces file.
inputFile.renameTo(tempFile); // Naming the temp file myTempFile.
System.out.println("Registration has completed successfuly");}
catch(Exception e){
System.out.println("\n\t\t\t\tSorry, there's something wrong try again!");}
}
}
【问题讨论】:
-
欢迎来到 Stack Overflow。你已经发布了一百多行代码——而且它的格式非常难以阅读。您能否尝试将其简化为仍然演示问题的最小示例,然后将其格式化,并将其编辑到问题中?
标签: java arrays while-loop filereader