File Update In Java

We are examining how to update txt – Csv and JSON files in Java with examples.



Java File Update – Txt

In java file update, we can replace any word in a txt file at its first occurrence by using the replace() method from the String methods:

Example: 

import java.io.*;public class Main { public static void main(String[] args) { try { // Read the file into a String FileReader fr = new FileReader("FirstFile.txt"); BufferedReader mrx = new BufferedReader(fr); // helps in efficient reading of data from a file StringBuilder ample = new StringBuilder(); // less time is taken to read the file String line; while ((line = mrx.readLine()) != null) { ample.append(line); ample.append(System.lineSeparator()); } String replace_word = ample.toString(); mrx.close(); fr.close();// Replace the specific word with the new word replace_word = replace_word.replace("Hey", "Greetings");// Write the modified content back to the file FileWriter fw = new FileWriter("FirstFile.txt"); fw.write(replace_word); fw.close(); System.out.println("File updated successfully."); } catch (IOException e) { e.printStackTrace(); } } }

The same goal can also be achieved by creating a method once and calling it as many time as we want:

Example: 

import java.io.*;public class Main {public static void replace_word(String match_word,String new_word){ try { // Read the file into a String FileReader fr = new FileReader("FirstFile.txt"); BufferedReader mrx = new BufferedReader(fr); // helps in efficient reading of data from a file StringBuilder ample = new StringBuilder(); // less time is taken to read the file String line; while ((line = mrx.readLine()) != null) { ample.append(line); ample.append(System.lineSeparator()); } String replace_word = ample.toString(); mrx.close(); fr.close();// Replace the specific word with the new word replace_word = replace_word.replace(match_word,new_word);// Write the modified content back to the file FileWriter fw = new FileWriter("FirstFile.txt"); fw.write(replace_word); fw.close(); System.out.println("File updated successfully."); } catch (IOException e) { e.printStackTrace(); }} public static void main(String[] args) { replace_word("Greetings","Hello User"); } }

Correspondingly, We can Also replace all the same words in a txt file by matching it with any specific String, using the replaceAll() method from Java Strings.

Example: 

import java.io.*;public class Main { public static void main(String[] args) { try { // Read the file into a String FileReader fr = new FileReader("FirstFile.txt"); BufferedReader mrx = new BufferedReader(fr); // helps in efficient reading of data from a file StringBuilder ample = new StringBuilder(); // less time is taken to read the file String line; while ((line = mrx.readLine()) != null) { ample.append(line); ample.append(System.lineSeparator()); } String replace_word = ample.toString(); mrx.close(); fr.close();// Replace the specific word with the new word replace_word = replace_word.replaceAll("we", "you");// Write the modified content back to the file FileWriter fw = new FileWriter("FirstFile.txt"); fw.write(replace_word); fw.close(); System.out.println("File updated successfully."); } catch (IOException e) { e.printStackTrace(); } } }

In the similar way, we can also create a method to match a string and replace all the corresponding words from the file.

Here we have created a method named replace_word_multiple_times() to replace multiple words in a txt file:

Example: 

import java.io.*;public class Main { public static void replace_word_multiple_times(String file_name,String word_match,String new_word){ try {// Read the file into a String FileReader fr = new FileReader(file_name); BufferedReader mrx = new BufferedReader(fr); // helps in efficient reading of data from a file StringBuilder ample = new StringBuilder(); // less time is taken to read the file String line; while ((line = mrx.readLine()) != null) { ample.append(line); ample.append(System.lineSeparator()); } String replace_word = ample.toString(); mrx.close(); fr.close();// Replace the specific word with the new word replace_word = replace_word.replace(word_match,new_word);// Write the modified content back to the file FileWriter fw = new FileWriter(file_name); fw.write(replace_word); fw.close(); System.out.println("File updated successfully."); } catch (IOException e) { e.printStackTrace(); }} public static void main(String[] args) { replace_word_multiple_times("FirstFile.txt","you","We"); } }

Java File Update – CSV

We can also update the content of the .csv file as shown in the examples below.

To modify a word at its first occurrence follow the example below:

Example: 

import java.io.*;public class Main { public static void main(String[] args) { try { // Read the file into a String FileReader fr = new FileReader("NewFile.csv"); BufferedReader mrx = new BufferedReader(fr); // helps in efficient reading of data from a file StringBuilder ample = new StringBuilder(); // less time is taken to read the file String line; while ((line = mrx.readLine()) != null) { ample.append(line); ample.append(System.lineSeparator()); } String replace_word = ample.toString(); mrx.close(); fr.close();// Replace the specific word with the new word replace_word = replace_word.replace("writing", "declaring");// Write the modified content back to the file FileWriter fw = new FileWriter("NewFile.csv"); fw.write(replace_word); fw.close(); System.out.println("File updated successfully."); } catch (IOException e) { e.printStackTrace(); } } }

Correspondingly, we can also modify all the same words in a .csv file after matching them all:

Example: 

import java.io.*;public class Main { public static void main(String[] args) { try { // Read the file into a String FileReader fr = new FileReader("NewFile.csv"); BufferedReader mrx = new BufferedReader(fr); // helps in efficient reading of data from a file StringBuilder ample = new StringBuilder(); // less time is taken to read the file String line; while ((line = mrx.readLine()) != null) { ample.append(line); ample.append(System.lineSeparator()); } String replace_word = ample.toString(); mrx.close(); fr.close();// Replace the specific word with the new word replace_word = replace_word.replaceAll("we", "you");// Write the modified content back to the file FileWriter fw = new FileWriter("NewFile.csv"); fw.write(replace_word); fw.close(); System.out.println("File updated successfully."); } catch (IOException e) { e.printStackTrace(); } } }

Now, to do this work more efficiently, let’s make methods named replace_word_multiple_times() and replace_word_single_time() as shown below:

Example: 

import java.io.*;public class Main { public static void replace_word_multiple_times(String file_name,String word_match,String new_word) { try {// Read the file into a String FileReader fr = new FileReader(file_name); BufferedReader mrx = new BufferedReader(fr); // helps in efficient reading of data from a file StringBuilder ample = new StringBuilder(); // less time is taken to read the file String line; while ((line = mrx.readLine()) != null) { ample.append(line); ample.append(System.lineSeparator()); } String replace_word = ample.toString(); mrx.close(); fr.close();// Replace the specific word with the new word replace_word = replace_word.replace(word_match, new_word);// Write the modified content back to the file FileWriter fw = new FileWriter(file_name); fw.write(replace_word); fw.close(); System.out.println("Replacing all words after matching File updated successfully."); } catch (IOException e) { e.printStackTrace(); } } public static void replace_word_single_time(String file_name,String word_match,String new_word){ try {// Read the file into a String FileReader fr = new FileReader(file_name); BufferedReader mrx = new BufferedReader(fr); // helps in efficient reading of data from a file StringBuilder ample = new StringBuilder(); // less time is taken to read the file String line; while ((line = mrx.readLine()) != null) { ample.append(line); ample.append(System.lineSeparator()); } String replace_word = ample.toString(); mrx.close(); fr.close();// Replace the specific word with the new word replace_word = replace_word.replace(word_match,new_word);// Write the modified content back to the file FileWriter fw = new FileWriter(file_name); fw.write(replace_word); fw.close(); System.out.println("Word changed at first occurrence File updated successfully."); } catch (IOException e) { e.printStackTrace(); }} public static void main(String[] args) {replace_word_single_time("NewFile.csv","Hey","Hey learner "); replace_word_multiple_times("NewFile.csv","you","We"); } }

 


Java File Update – JSON

We can also update the data of the .json file as shown in the examples below.

To modify a word at its first occurrence follow the example below:

Example: 

import java.io.*;public class Main { public static void main(String[] args) { try { // Read the file into a String FileReader fr = new FileReader("NewFile.json"); BufferedReader mrx = new BufferedReader(fr); // helps in efficient reading of data from a file StringBuilder ample = new StringBuilder(); // less time is taken to read the file String line; while ((line = mrx.readLine()) != null) { ample.append(line); ample.append(System.lineSeparator()); } String replace_word = ample.toString(); mrx.close(); fr.close();// Replace the specific word with the new word replace_word = replace_word.replace("Java Programming Language", "Java Programming");// Write the modified content back to the file FileWriter fw = new FileWriter("NewFile.json"); fw.write(replace_word); fw.close(); System.out.println("File updated successfully."); } catch (IOException e) { e.printStackTrace(); } } }

Correspondingly, we can also modify multiple words in a .json file after matching them and then replacing them using replaceAll() method :

Example: 

import java.io.*;public class Main { public static void main(String[] args) { try { // Read the file into a String FileReader fr = new FileReader("NewFile.json"); BufferedReader mrx = new BufferedReader(fr); // helps in efficient reading of data from a file StringBuilder ample = new StringBuilder(); // less time is taken to read the file String line; while ((line = mrx.readLine()) != null) { ample.append(line); ample.append(System.lineSeparator()); } String replace_word = ample.toString(); mrx.close(); fr.close();// Replace the specific word with the new word replace_word = replace_word.replaceAll("Java", "_Java_");// Write the modified content back to the file FileWriter fw = new FileWriter("NewFile.json"); fw.write(replace_word); fw.close(); System.out.println("File updated successfully."); } catch (IOException e) { e.printStackTrace(); } } }

Here, we have create two different methods – One for updating first match word occurrence and second for all match words in a JSON file:

Example: 

import java.io.*;public class Main { public static void replace_word_multiple_times(String file_name,String word_match,String new_word) { try {// Read the file into a String FileReader fr = new FileReader(file_name); BufferedReader mrx = new BufferedReader(fr); // helps in efficient reading of data from a file StringBuilder ample = new StringBuilder(); // less time is taken to read the file String line; while ((line = mrx.readLine()) != null) { ample.append(line); ample.append(System.lineSeparator()); } String replace_word = ample.toString(); mrx.close(); fr.close();// Replace the specific word with the new word replace_word = replace_word.replace(word_match, new_word);// Write the modified content back to the file FileWriter fw = new FileWriter(file_name); fw.write(replace_word); fw.close(); System.out.println("Replacing all words after matching File updated successfully."); } catch (IOException e) { e.printStackTrace(); } } public static void replace_word_single_time(String file_name,String word_match,String new_word){ try {// Read the file into a String FileReader fr = new FileReader(file_name); BufferedReader mrx = new BufferedReader(fr); // helps in efficient reading of data from a file StringBuilder ample = new StringBuilder(); // less time is taken to read the file String line; while ((line = mrx.readLine()) != null) { ample.append(line); ample.append(System.lineSeparator()); } String replace_word = ample.toString(); mrx.close(); fr.close();// Replace the specific word with the new word replace_word = replace_word.replace(word_match,new_word);// Write the modified content back to the file FileWriter fw = new FileWriter(file_name); fw.write(replace_word); fw.close(); System.out.println("Word changed at first occurrence File updated successfully."); } catch (IOException e) { e.printStackTrace(); }} public static void main(String[] args) {replace_word_single_time("NewFile.json","Developer","Java Developer"); replace_word_multiple_times("NewFile.json","_Java_","Java"); } }

Now you know how to update Txt, JSON and Csv files in Java.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *