Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File.exists不在java中工作_Java_File_Io - Fatal编程技术网

File.exists不在java中工作

File.exists不在java中工作,java,file,io,Java,File,Io,这是我编写的代码,用于删除已存在的文件 public void createFile(Map<String, String> map, String name) { try { System.out.println("Creating new File..."); File file = new File("./Analysis/files/master.csv"); if (file.exists())

这是我编写的代码,用于删除已存在的文件

public  void createFile(Map<String, String> map, String name) 
{
    try
    {
        System.out.println("Creating new File...");
        File file = new File("./Analysis/files/master.csv");
        if (file.exists()) 
        {
            System.out.println("File Deleted...."+file.delete());
        }
        System.out.println("New File Created   "+file.createNewFile());
        FileWriter fw = new FileWriter(file, true);
        for (Map.Entry<String, String> entry : map.entrySet()) 
        {
            fw.write(entry.getKey());
            fw.write(",");
            fw.write(entry.getValue());
            fw.write("\n");
            fw.flush();
        }
        fw.close();
    }catch(IOException e)
    { 
        throw new BuildException(e.getMessage());
    }
}
public void createFile(映射映射,字符串名称)
{
尝试
{
System.out.println(“创建新文件…”);
文件文件=新文件(“./Analysis/files/master.csv”);
if(file.exists())
{
System.out.println(“文件已删除…”+File.delete());
}
System.out.println(“创建的新文件”+File.createNewFile());
FileWriter fw=新的FileWriter(文件,true);
对于(Map.Entry:Map.entrySet())
{
write(entry.getKey());
fw.写(“,”);
write(entry.getValue());
fw.写入(“\n”);
fw.flush();
}
fw.close();
}捕获(IOE异常)
{ 
抛出新的BuildException(例如getMessage());
}
}

对于已经存在于该路径中的文件,This file.exists显示为false,因此它不会删除该文件并将内容附加到该文件。有什么想法吗?

您可能需要提供该文件的完整路径

尝试使用
file.getAbsoluteFile().exists()

如果更改,请检查类似的帖子

FileWriter fw = new FileWriter(file, true);

它不会追加文本,但会粘贴新文本

编辑:

public void createFile(映射映射,字符串名称)
{
尝试
{
文件文件=新文件(“./Analysis/files/master.csv”);
FileWriter fw=新的FileWriter(文件,false);
对于(Map.Entry:Map.entrySet())
{
write(entry.getKey());
fw.写(“,”);
write(entry.getValue());
fw.写入(“\n”);
fw.flush();
}
fw.close();
}捕获(IOE异常)
{ 
抛出新的BuildException(例如getMessage());
}
}

FileWriter将使用规范文件格式创建文件。因此,对同一表格进行检查,看看是否符合您的期望

将代码的前两行更改为:

File file = new File("./Analysis/files/master.csv").getCanonicalFile();
System.out.println("Creating new File "+ file.getAbsolutePath());

如果文件不再存在,则该文件已被删除,您必须刷新文件夹以更新内容。您至少需要学习缩进代码。@nikpon您的观点是什么?请在检查文件.exists之前执行“file.getAbsolutePath()”并检查它是否为您提供了正确的路径。@nikpon是,但我不认为刷新文件夹与此有任何关系,但也许我误解了你的意思。为什么需要完整的路径?使用相对路径没有错。用户只需确保它是相对于他们认为的任何东西。windows 7存在一些问题。。看到类似的答案。。谢谢你指出这一点。我在Win7上,没有任何问题(在我自己创建的测试文件上,没有其他进程使用它),所以这可能就是为什么我没有出现错误的原因。+1。这OP甚至不应该费心检查“存在”与否。这就够了。如果文件不存在或将被覆盖,则将创建该文件。我不确定OP最初为什么使用
true
。这是我遵循的链接,目前工作正常隐藏扩展名,然后错误地在文件名中键入扩展名是很常见的,导致实际文件名为
file.csv.csv
,但我不知道这是否真的是问题所在。
public  void createFile(Map<String, String> map, String name) 
{
    try
    {

    File file = new File("./Analysis/files/master.csv");
    FileWriter fw = new FileWriter(file, false);
    for (Map.Entry<String, String> entry : map.entrySet()) 
    {
        fw.write(entry.getKey());
        fw.write(",");
        fw.write(entry.getValue());
        fw.write("\n");
        fw.flush();
    }
    fw.close();
}catch(IOException e)
{ 
    throw new BuildException(e.getMessage());
}
}
File file = new File("./Analysis/files/master.csv").getCanonicalFile();
System.out.println("Creating new File "+ file.getAbsolutePath());