In this post, you will lean how to write file in Java. 4 various ways will be used :
1- BufferedWriter
Since JDK 1.1
The first way to write file in Java is by using the BufferedWriter class from the java.io package. Below an example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public static void main(String[] args) { // Make sure you use \\ instead of \ String filePath = "C:\\files\\file.txt"; // Content that will be written in the file List<String> lines = Arrays.asList("line1", "line2", "line3"); Writer bufferedWriter = null; try { //Creating a file Writer fileWriter = new FileWriter(filePath); bufferedWriter = new BufferedWriter(fileWriter); // Writing the content for(String line : lines) { bufferedWriter.write(line); bufferedWriter.write(System.getProperty("line.separator")); } } catch (IOException e) { System.out.println("Problem occurs when creating file " + filePath); e.printStackTrace(); } finally { //Closing the file if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException e) { System.out.println("Problem occurs when closing file !"); e.printStackTrace(); } } } } |
System.getProperty(“line.separator”) is used to separate lines in text files.
Note that the file will be overwritten each time you lunch your program. If you want to append your content into the existing file, use:
1 |
Writer fileWriter = new FileWriter(file, true); |
1 |
Writer fileWriter = new FileWriter(file); |
2- PrintWriter
The second way to write file in Java is to use PrintWriter class from the java.io package. You can use it as following :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public static void main(String[] args) { // Make sure you use \\ instead of \ String filePath = "C:\\files\\file.txt"; // Content that will be written in the file List<String> lines = Arrays.asList("line1", "line2", "line3"); PrintWriter printWriter = null; try { Writer fileWriter = new FileWriter(filePath, true); printWriter = new PrintWriter(fileWriter); for (String line : lines) { printWriter.write(line); printWriter.write(System.getProperty("line.separator")); //The two above lines can be replaced with : printWriter.println(line); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // Closing the file if (printWriter != null) { printWriter.close(); } } } |
In the two earlier examples, if the writing string is null; the system will throw a java.lang.NullPointerException. If you want to print the string even if it is null so use
1 |
printWriter.print(line) |
1 |
printWriter.write(line); |
3- Try with resources
Since JDK 1.7
The try with resources statement ensures that each resource is closed at the end of the statement. No need to use finally block to ensure the closing of the resource.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public static void main(String[] args) { // Make sure you use \\ instead of \ String filePath = "C:\\files\\file.txt"; // Content that will be written in the file List<String> lines = Arrays.asList("line1", "line2", "line3"); try (Writer writer = new BufferedWriter(new FileWriter(filePath))) { for(String line : lines) { writer.write(line); writer.write(System.getProperty("line.separator")); } } catch (IOException e) { System.out.println("Problem occurs when creating file " + filePath); e.printStackTrace(); } } |
4 – Apache Commons IO
Since JDK 1.6 For Commons IO 2.4
Since JDK 1.5 For Commons IO 2.2
You can also use the Apache Commons IO library to read file in Java. To do this, add the following dependency to you pom.xml
1 2 3 4 5 |
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> |
The Apache Commons IO library offers the FileUtils class to deal with files and can be very useful if all your content is on a Collection. In this case, use writeLines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static void main(String[] args) { // Make sure you use \\ instead of \ String filePath = "C:\\files\\file.txt"; // Content that will be written in the file List<String> lines = Arrays.asList("line1", "line2", "line3"); File file = new File(filePath); try { //Writing file FileUtils.writeLines(file, lines); } catch (IOException e) { System.out.println("Problem occurs when writing file " + filePath); e.printStackTrace(); } } |
If you want to append your content into the end of the file use :
1 |
FileUtils.writeLines(file, lines, true); |