Java - how do I write a file to a specified directory

后端 未结 4 1348
温柔的废话
温柔的废话 2020-12-24 13:25

I want to write a file results.txt to a specific directory on my machine (Z:\\results to be precise). How do I go about specifying the directory to BufferedWriter/FileWriter

4条回答
  •  死守一世寂寞
    2020-12-24 13:44

    Use:

    File file = new File("Z:\\results\\results.txt");
    

    You need to double the backslashes in Windows because the backslash character itself is an escape in Java literal strings.

    For POSIX system such as Linux, just use the default file path without doubling the forward slash. this is because forward slash is not a escape character in Java.

    File file = new File("/home/userName/Documents/results.txt");
    

提交回复
热议问题