3

I specified the full path of the file location when I created a FileWriter, but I did not see the file being created. I also did not get any error during file creation.

Here's a snippet of my code:

public void writeToFile(String fullpath, String contents) {
    File file = new File(fullpath, "contents.txt");
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));
        bw.write(contents);
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

fullpath is "D:/codes/sources/logs/../../bin/logs". I have searched my whole directory, but I cannot find the file anywhere. If I specify just the filename only [File file = new File("contents.txt");] , it is able to save the contents of the file, but it is not placed on my preferred location.

How can I save the file content to a preferred location?

UPDATE: I printed the full path using file.getAbsolutePath(), and I am getting the correct directory path. [D:\codes\sources\logs....\bin\logs\contents.txt] But when I look for the file in directory, I cannot find it there.

buræquete
  • 14,226
  • 4
  • 44
  • 89
chris yo
  • 1,187
  • 4
  • 13
  • 30
  • 2
    Do you literally have the "../.." in there? If so, you need to look in `D:/codes/bin/logs` – Jon Skeet Jul 18 '13 at 08:43
  • Also with Java 7, you can do the same with: `Files.write(Paths.get(fullPath), contents.getBytes("UTF-8"));` – assylias Jul 18 '13 at 08:48
  • 3
    `createNewFile()` is not needed. – Joop Eggen Jul 18 '13 at 08:50
  • @JonSkeet: Yes, I have the "..\.." in the path, since I want to save the file at D:/codes/bin/logs. – chris yo Jul 18 '13 at 09:00
  • @JoopEggen: I added that after several failed attempts in writing the contents to a file. :) I will remove that once I established that this line is not needed. – chris yo Jul 18 '13 at 09:01
  • Mysterious. Do you reach that code? How about some other exception, SecurityException, or the like? No empty `catch (Throwable e) {}` I hope? – Joop Eggen Jul 18 '13 at 09:16
  • I found out the cause already. It seems that the batch file is deleting all the .txt files after execution. Hmph! The pitfalls of a developer! – chris yo Jul 18 '13 at 10:04
  • 1
    The file is being created but not where you're looking, unless there was an exception. The `exists()/createNewFile()` block is a complete waste of time and space. `new FileWriter()` causes the operating system to do that anyway, and now you're forcing it to add a deletion of the file you just created as well. Don't write pointless code. `new FileWriter(file)` would do just as well. – user207421 Jul 02 '16 at 08:13
  • 1
    Have you tried to see what `file.getCanonicalPath()` returns? Is it what you expected? – C.Champagne Jul 02 '16 at 10:51

1 Answers1

0

Make sure you add trailing backslashes to the path parameter so the path is recognized as a directory. The example provide is for a Windows OS which uses backslashes that are escaped. For a more robust method use the file.separator property for the system.

Works

writeToFile("D:\\Documents and Settings\\me\\Desktop\\Development\\",
                "this is a test");

Doesn't Work

writeToFile("D:\\Documents and Settings\\me\\Desktop\\Development",
                "this is a test");

File Separator Example

String fs = System.getProperty("file.separator");
String path = fs + "Documents and Settings" + fs + "me" + fs
        + "Desktop" + fs + "Development" + fs;
writeToFile(path, "this is a test");
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • I printed the absolute path via file.getAbsolutePath(), and I am getting the correct directory structure. e.g. D:\codes\sources\logs\..\..\bin\logs\contents.txt. – chris yo Jul 18 '13 at 09:05
  • 3
    Since the OP is using `File` objects to construct the path there is no need to get the separator and concatenate the full path with strings. – Viktor Seifert Jul 18 '13 at 09:08
  • 3
    The constructor `File(String parent, String child)` doesn't care about trailing (back-)slashes (see [the javadocs](http://docs.oracle.com/javase/6/docs/api/java/io/File.html#File(java.lang.String,%20java.lang.String)) ). – creinig Jul 18 '13 at 09:08