How to write a Java text file

The following code does not create the file (I do not see the file anywhere). What is missing?

try { //create a temporary file String timeLog = new SimpleDateFormat("yyyyMMdd_HHmmss").format( Calendar.getInstance().getTime()); File logFile=new File(timeLog); BufferedWriter writer = new BufferedWriter(new FileWriter(logFile)); writer.write (string); //Close writer writer.close(); } catch(Exception e) { e.printStackTrace(); } 
+61
java file file-io
Apr 02 '13 at 1:13
source share
7 answers

I think your expectations and reality do not match (but when are they ever;))

Basically, if you think the file is written and where the file is actually written, they are not equal (hmmm, maybe I should write an if ;))

 public class TestWriteFile { public static void main(String[] args) { BufferedWriter writer = null; try { //create a temporary file String timeLog = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime()); File logFile = new File(timeLog); // This will output the full path where the file will be written to... System.out.println(logFile.getCanonicalPath()); writer = new BufferedWriter(new FileWriter(logFile)); writer.write("Hello world!"); } catch (Exception e) { e.printStackTrace(); } finally { try { // Close the writer regardless of what happens... writer.close(); } catch (Exception e) { } } } } 

Also note that your example will overwrite any existing files. If you want to add text to a file, you must do the following:

 writer = new BufferedWriter(new FileWriter(logFile, true)); 
+118
Apr 02 '13 at 1:29
source share

I would like to add a little more to MadProgrammer's answer.

In the case of multi-line recording when executing the command

 writer.write(string); 

you may notice that newlines are omitted or omitted in the recorded file, even if they appear during debugging or if the same text is printed on the terminal using

 System.out.println("\n"); 

Thus, the entire text appears as one large piece of text, which is undesirable in most cases. A newline character may be platform dependent, so it’s best to get that character from the properties of the java system using

 String newline = System.getProperty("line.separator"); 

and then using a newline variable instead of "\ n". This will give the result the way you want.

+16
Dec 04 '13 at 9:47
source share

In java 7 you can now do

 try(BufferedWriter w = ....) { w.write(...); } catch(IOException) { } 

and w.close will be done automatically

+14
Apr 6 '14 at 19:54
source share

It does not create the file because you never created the file. You have made an object for this. Creating an instance does not create a file.

 File newFile = new File("directory", "fileName.txt"); 

You can do this to create a file:

 newFile.createNewFile(); 

You can do this to create a folder:

 newFile.mkdir(); 
+4
May 9 '15 at
source share

You can try the Java library. FileUtils , it has many functions that are written to files.

0
Oct 13 '14 at 5:39
source share

He works with me. Make sure you add ".txt" next to timeLog. I used it in a simple program opened with Netbeans, and it writes the program to the main folder (where the builder and src folders are located).

0
May 22 '17 at 17:27
source share

Using java 8 LocalDateTime and java 7 try-with operator:

 public class WriteFile { public static void main(String[] args) { String timeLog = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss").format(LocalDateTime.now()); File logFile = new File(timeLog); try (BufferedWriter bw = new BufferedWriter(new FileWriter(logFile))) { System.out.println("File was written to: " + logFile.getCanonicalPath()); bw.write("Hello world!"); } catch (IOException e) { e.printStackTrace(); } } } 
0
Dec 21 '18 at 22:23
source share



All Articles