所以我试图显示一个符号化项目列表,让用户选择一个要删除的项目。
问题出在步骤(1) 当且仅当文件不存在时,我希望写入文件。 所以我先于while循环,它将使用
写入文件if(file.isFile() ==false).
然而,这似乎使输出空白,就好像读者无法读取文件并将其存储在数组中一样。为什么? PS如果我没有输入if和else语句,输出就可以了,只是它再次写入文件,我不想发生这种情况,我想要显示除用户输入内容之外的所有内容。
请在这里与我裸露我几乎没有从java开始。所以我深深地为任何错误道歉。 这是代码
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!");}
}
}
答案 0 :(得分:0)
isFile()
检查它是否是文件(不是目录)
exists()
检查文件是否存在。
http://docs.oracle.com/javase/7/docs/api/java/io/File.html
改变它。此外,在使用之前,请务必阅读有关该方法的文档并确切了解其用途。
注意,如果语句采用布尔值,那么if(file.exists()){}
或if(file.isFile()){}
是正确的方法
修改强>
由于您仍然无法将数据存入此文件,因此您应该这样做。
首先,处理IOException
。当你执行throws IOException
时,你基本上是说你将在更高级别处理异常,但是main也声明了这一点,因此异常永远不会被捕获(使用IDE和自动学习时出现的问题之一)校正)。
要实现这一目标,您需要这样做:
1)删除所有throws IOException
2)将捕获更改为
try{
//...
} catch(IOException ioe){
ioe.printStackTrace();
} catch(Exception e){
System.out.println("\n\t\t\t\tSorry, there's something wrong try again!");
System.out.println("\n\t\t\t\tReason:"+e.getMessage());
}
执行此操作将显示是否存在任何IO异常。
您还可以评论tempFile.delete();
以检查文件是否确实包含任何数据。如果你想一直删除它,那么创建一个新的
public static void main (String args[]) {
//Declaring the file
File tempFile = new File("D:\\myTempFile.txt");
if(!tempFile.exists()){
tempFile.createNewFile();
}
修改2
这是一个工作示例
public static void main(String[] args){
try{
File tempFile = new File("c:/testing/temp.txt");
tempFile.getParentFile().mkdirs();
tempFile.createNewFile();
writeToFile(tempFile);
//readFromFile();
//deleteFile();
} catch(IOException ioe){
ioe.printStackTrace();
}
}
public static void writeToFile (File file) throws IOException{
//Declaring the writer
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
int n=0;
int L=1;
//Writing to the file if it did not exist
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();
}