-2

This is the code I have written to delete a file if it already 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());
    }
}

This file.exists is showing false for the file which already exists in that path so its not deleting that file and append the contents to that file.Any idea?

Freak
  • 6,786
  • 5
  • 36
  • 54
Deepak_Sharma
  • 61
  • 2
  • 10
  • 1
    the file if no longer existed is already deleted and you have to refresh the folder to update the content. –  Dec 13 '13 at 15:08
  • 3
    You need to learn to indent your code, at very least. – kviiri Dec 13 '13 at 15:10
  • @nikpon What is your point? – Freak Dec 13 '13 at 15:10
  • 3
    can you please do a "file.getAbsolutePath()" before checking the file.exists and check if it is giving you proper path. – A Paul Dec 13 '13 at 15:10
  • Why not just overwrite it instead of trying to delete it? – Andrew Dec 13 '13 at 15:12
  • Are you using linux? Are you sure about the path? – Freak Dec 13 '13 at 15:12
  • This question appears to be off-topic – Freak Dec 13 '13 at 15:15
  • @freak the file is already deleted, so could you elaborate a bit? –  Dec 13 '13 at 15:18
  • @nikpon I'm not sure what you mean by "refresh the folder to update the content". The file system should update itself properly without the user having to intervene. OP's program (is supposed to) delete the file if it exists and then creates a new one with the same name. The same logic works for me (with a valid path) without having to do any refreshing. – MxLDevs Dec 13 '13 at 15:21
  • @nikpon `This file.exists is showing false for the file [which already exists]` do you read that? – Freak Dec 13 '13 at 15:22
  • @MxyL It's about 'File.exists not working in java". –  Dec 13 '13 at 15:23
  • @freak But it appears in the file list, so what can you say about it? –  Dec 13 '13 at 15:25
  • @nikpon 'But it appears in the file list' ??? sorry? how do you know? Are you in your senses? – Freak Dec 13 '13 at 15:27
  • 1
    @nikpon Yes, but I don't think refreshing the folder would have anything to do with it, but maybe I am misunderstanding what you mean. – MxLDevs Dec 13 '13 at 15:30
  • @freak "How do you know" it could be another question. What do you talk about senses? –  Dec 13 '13 at 15:30
  • @MxyL What do you think you can express in your answer, but OP says that `File`'s `exists()` doesn't work and asking you for your ideas, so what ideas you have? Also could you explain how the file system is related to java if not natively? –  Dec 13 '13 at 15:37
  • @nikpon I don't know what the file system has to do with Java, which is why I don't understand your comment on "refreshing the folder to update its contents" – MxLDevs Dec 13 '13 at 15:43
  • @MxyL ... and of course it doesn't update because it could be broken or the OP has a broken path, but the file is deleted normally before. –  Dec 13 '13 at 15:44
  • Are you sure you have permission on the file? It might exist, but you can't necessarily delete it. – Elliott Frisch Dec 13 '13 at 18:16

3 Answers3

3

if you change

FileWriter fw = new FileWriter(file, true);

to

FileWriter fw = new FileWriter(file, false);

it will not append text but paste new

edit:

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());
}
}
sala
  • 181
  • 1
  • 12
  • +1. This. The OP shouldn't even bother checking for "exists" or not. This takes care of it. The file will be created if it doesn't exist or it will be overwritten. I'm not sure why the OP used `true` in the first place. – MadConan Dec 13 '13 at 15:35
  • http://windows.microsoft.com/en-us/windows/show-hide-file-name-extensions#show-hide-file-name-extensions=windows-7 – Deepak_Sharma Dec 13 '13 at 16:13
  • This is the link I followed and its working fine for now – Deepak_Sharma Dec 13 '13 at 16:13
  • It is common to hide extensions and then mistakenly type the extension into the filename, resulting in things like `file.csv.csv` as the actual filename, but I don't know whether that's actually the problem. – MxLDevs Dec 13 '13 at 16:52
0

you may need to give a full path to the file.

try using file.getAbsoluteFile().exists()

check similar post here

Community
  • 1
  • 1
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • Why is a full path needed? There is nothing wrong with using relative paths. The user just needs to make sure that it is relative to whatever they think it is. – MxLDevs Dec 13 '13 at 15:17
  • there are some issues with windows 7.. see similar answer. .. http://stackoverflow.com/questions/919918/file-exists-returns-false-when-file-exists – stinepike Dec 13 '13 at 15:20
  • Thanks for pointing it out. I'm on Win7 and not having any of those issues (on a test file that I created myself with no other processes using it), so that may be the reason why the bug doesn't occur for me. – MxLDevs Dec 13 '13 at 15:29
0

FileWriter is going to use the canonical file form to create your file. So perform your checks against the same form and see if that matches your expectations.

Change the first two lines of the code to this:

File file = new File("./Analysis/files/master.csv").getCanonicalFile();
System.out.println("Creating new File "+ file.getAbsolutePath());
jmehrens
  • 10,580
  • 1
  • 38
  • 47