Java if语句清空输出
我试图显示符号化项目的列表,并让用户选择一个要删除的项目Java if语句清空输出,java,arrays,while-loop,filereader,Java,Arrays,While Loop,Filereader,我试图显示符号化项目的列表,并让用户选择一个要删除的项目 我将信息写入一个txt文件 我将文件的内容存储在一个数组中 我把数组打印出来 用户输入(例如L101),它将从 档案 问题在步骤(1)中 我希望该文件被写入,当且仅当该文件不存在时。 因此,我在while循环之前,该循环将使用 if(file.isFile() ==false). 然而,这似乎会清空输出,就好像读卡器无法读取文件并将其存储在数组中一样。为什么? 注:如果我没有输入if和else语句,那么输出是正常的,只是它再次写入了文件
if(file.isFile() ==false).
然而,这似乎会清空输出,就好像读卡器无法读取文件并将其存储在数组中一样。为什么?
注:如果我没有输入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!");}
}
}
publicstaticvoidmain(字符串args[])引发IOException
{
//声明文件
File tempFile=新文件(“D:\\myTempFile.txt”);
//宣告作者
BufferedWriter writer=new BufferedWriter(new FileWriter(tempFile));
int n=0;
int L=1;
//如果文件不存在,则写入该文件
if(tempFile.isFile()==false)
{
while(nisFile()
检查它是否是文件(不是目录)
exists()
检查文件是否存在
改变它。另外,在使用它之前,一定要阅读有关该方法的文档并确切知道它的用途
注意,if语句采用布尔值,因此if(file.exists()){}
或if(file.isFile()){}
是正确的方法
编辑:
由于您在将数据放入文件中时仍然存在问题,因此您应该在此处执行操作
首先,处理IOException
。当您执行抛出IOException
时,您基本上是在说您将在更高级别上处理异常,但main也声明了这一点,因此永远不会捕获异常(学习使用IDE和自动更正时的问题之一)
要实现这一点,您需要执行以下操作:
1) 删除所有抛出的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();
}
publicstaticvoidmain(字符串[]args){
试一试{
File tempFile=新文件(“c:/testing/temp.txt”);
tempFile.getParentFile().mkdirs();
tempFile.createNewFile();
写文件(tempFile);
//readFromFile();
//deleteFile();
}捕获(ioe异常ioe){
ioe.printStackTrace();
}
}
公共静态void writeToFile(文件文件)引发IOException{
//宣告作者
BufferedWriter writer=新的BufferedWriter(新文件编写器(文件));
int n=0;
int L=1;
//如果文件不存在,则写入该文件
虽然在isFile之前就尝试过它,但同样的问题也发生了tempFile.isFile()==false,这里你是说如果它不是一个文件,那么就写,所以删除它并在tempFile.exists()时执行。谢谢,我删除了isFile并执行了exists(),仍然没有任何更改。首先非常感谢你对我的容忍。所以我编辑了代码(编辑了if语句,删除了IOException,更改了main和freeparkingspace方法中的捕获,将“tempFile”更改为“pLotsFile”)但问题仍然存在,我猜问题不在main方法中?我根本不同意异常处理部分。如果main
抛出异常,异常仍然会显示出来-您所做的只是使代码更加复杂,并且隐藏堆栈跟踪,如果出现IOException
以外的任何异常,则堆栈跟踪将被隐藏rown。这已经很好了。OP的真正问题是他们正在创建一个文件编写器,它会立即创建文件-在这一点上,他们无法判断该文件是否存在于该语句之前。欢迎使用堆栈溢出。您已经发布了100多行代码-并且它的格式非常难以重新设置你能不能试着把这个问题简化成一个能说明问题的最小的例子,然后把它清晰地格式化,然后把它编辑成问题?