Friday 24 May 2019

Java: Append text to a file


Approach 1: Using Files.write method.

Example
Files.write(Paths.get(filePath), content.getBytes(), StandardOpenOption.APPEND);

$cat welcome.txt
Hello, Welcome to Java Programming

App.java
package com.sample.app;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

import javax.script.ScriptException;

public class App {

 public static boolean appendContent(String filePath, String content) {
  if (content == null)
   return false;

  try {
   Files.write(Paths.get(filePath), content.getBytes(), StandardOpenOption.APPEND);
  } catch (IOException e) {
   e.printStackTrace();
   return false;
  }

  return true;
 }

 public static void main(String args[]) throws ScriptException {
  appendContent("/Users/krishna/Documents/welcome.txt", "Appending second line to the file");
 }
}


Run App.java, open welcome.txt file, you can see that new content is appended to the file.

$cat welcome.txt 
Hello, Welcome to Java Programming
Appending second line to the file

Approach 2: Using FileWriter class.

FileWriter fw = new FileWriter(filePath, true);
fw.write(content);


App.java
package com.sample.app;

import java.io.FileWriter;
import java.io.IOException;

import javax.script.ScriptException;

public class App {

 public static boolean appendContent(String filePath, String content) {
  if (content == null)
   return false;

  try (FileWriter fw = new FileWriter(filePath, true)) {
   fw.write(content);
  } catch (IOException e) {
   e.printStackTrace();
   return false;
  }

  return true;
 }

 public static void main(String args[]) throws ScriptException {
  appendContent("/Users/krishna/Documents/welcome.txt", "\nAppending 3rd line to the file");
 }
}


Run App.java and open welcome.txt, you can see new content is appended to the file.

$cat welcome.txt 
Hello, Welcome to Java Programming
Appending second line to the file

Appending 3rd line to the file

Approach 3: Using PrintWriter class
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filePath, true)));
writer.println(content);


App.java
package com.sample.app;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.script.ScriptException;

public class App {

 public static boolean appendContent(String filePath, String content) {
  if (content == null)
   return false;

  try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filePath, true)))) {
   writer.println(content);
  } catch (IOException e) {
   e.printStackTrace();
   return false;
  }

  return true;
 }

 public static void main(String args[]) throws ScriptException {
  appendContent("/Users/harikrishnagurram/Documents/welcome.txt", "\nAppending 4th line to the file");
 }
}

Run App.java, open welcome.txt, you can see that new content is appended to the file.

Hello, Welcome to Java Programming
Appending second line to the file

Appending 3rd line to the file


Appending 4th line to the file


You may like

No comments:

Post a Comment