文件编写器重新创建文件但程序消息已经是文件.... [英] File writer recreates file but program message is file already ....

查看:72
本文介绍了文件编写器重新创建文件但程序消息已经是文件....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建多个文件的代码,但是只创建一次,如果你删除文件并运行程序,程序输出文件已经存在,但它确实重新创建了文件,应该写入文件的printwriter没有写道



包FileSystem; 

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

公共类CreateFiles
{
public static void main(String [] args)抛出IOException
{
try
{
String [] Names = new String [20];
姓名[0] =Android;
Names [1] =java;
Names [2] =computerscience;
姓名[3] =卫星;
姓名[4] =沟通;
Names [5] =navigator;
姓名[6] =科学;
姓名[7] =a;
姓名[8] =b;
姓名[9] =c;
姓名[10] =d;
姓名[11] =e;
Names [12] =f;
姓名[13] =g;
Names [14] =h;
姓名[15] =我;
Names [16] =j;
姓名[17] =l;
姓名[18] =m;
Names [19] =n;

for(int j = 0; j< Names.length; j ++)
{
File file = new File(F:\\+ Names [j ] +。txt);

PrintWriter printer = new PrintWriter(file,UTF-8);
printer.write(android is advanced java);
if(file.createNewFile()){
System.out.println(File is created!);
} else {
System.out.println(文件已经存在。);
}

}

}
catch(例外e)
{
e.printStackTrace();
}
最后
{
}
}
}





我尝试过:



更改了代码并搜索了文件类

解决方案

只需阅读已使用函数的文档:

PrintWriter(文件文件)

...

file - 用作本作者目的地的文件。如果该文件存在,那么它将被截断为零大小;否则,将创建一个新文件。





public boolean createNewFile()

...

当且仅当具有此名称的文件尚不存在时,以原子方式创建一个由此抽象路径名命名的新空文件。

...

返回:

如果指定文件不存在且成功创建,则为true;如果指定的文件已经存在,则返回false

这将始终返回false,因为该文件是由先前创建的 PrintWriter 创建的对象。



所以你必须在创建 PrintWriter 之前检查文件是否存在:

文件file =  new 文件(  F:\\ + Names [j] +   .txt ); 
if (file.exists()){
System.out.println( 文件已存在。);
} else {
System.out.println( 将创建文件!);
}
// 如果文件已经存在,将立即创建文件
PrintWriter printer = new PrintWriter(file, UTF-8\" );


code that creates several files, however it just creates them once after which if you delete the files and run the program the program output is file already exists but it does recreates the file and the printwriter which should write to file doesn't writes

package FileSystem;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

public class CreateFiles
{
    public static void main( String[] args ) throws IOException
    {	
        try
        {
        String[] Names = new String[20]; 
        Names[0] = "Android";
        Names[1] = "java";
        Names[2] = "computerscience";
        Names[3] = "satellite";
        Names[4] = "communication";
        Names[5] = "navigator";
        Names[6] = "science";
        Names[7] = "a";
        Names[8] = "b";
        Names[9] = "c";
        Names[10] = "d";
        Names[11] = "e";
        Names[12] = "f";
        Names[13] = "g";
        Names[14] = "h";
        Names[15] = "i";
        Names[16] = "j";
        Names[17] = "l";
        Names[18] = "m";
        Names[19] = "n";
        
        for(int j = 0; j<Names.length; j++)
        {
            File file = new File("F:\\" + Names[j] + ".txt");   
            
            PrintWriter printer = new PrintWriter(file, "UTF-8"); 
            printer.write("android is advanced java");
            if (file.createNewFile()){
	        System.out.println("File is created!");
	      }else{
	        System.out.println("File already exists.");
	      }            
            
        }        
        
        }
        catch(Exception e)            
        {
            e.printStackTrace();
        }    
        finally
        {
        }
    }
}



What I have tried:

changed code and searched for file classes

解决方案

Just read the documentation for the used functions:

PrintWriter(File file)
...
file - The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created.



public boolean createNewFile()
...
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
...
Returns:
true if the named file does not exist and was successfully created; false if the named file already exists

This will always return false because the file has been created by the preceeding creation of the PrintWriter object.

So you have to check if the file exists before creating the PrintWriter:

File file = new File("F:\\" + Names[j] + ".txt");
if (file.exists()){
    System.out.println("File already exists.");
}else{
    System.out.println("File will be created!");
}
// File will be created now if it does not exist already            
PrintWriter printer = new PrintWriter(file, "UTF-8");             


这篇关于文件编写器重新创建文件但程序消息已经是文件....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆