Creating, writing and editing the same text file in java

Let's say I have the following code:

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class EditFile { public static void main(String[] args) { try{ String verify, putData; File file = new File("file.txt"); file.createNewFile(); FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); bw.write("Some text here for a reason"); bw.flush(); bw.close(); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while( br.readLine() != null ){ verify = br.readLine(); if(verify != null){ putData = verify.replaceAll("here", "there"); bw.write(putData); } } br.close(); }catch(IOException e){ e.printStackTrace(); } } } 

All I wanted to do was write something in a text file, in my case "Some text is here for some reason." Then, to read the data from my file and finally change the text from my file from "Some text is here for a reason" to "Some text is there for some reason". I ran the code, but all that happens is to write in my file "Some text here for some reason."

I tried to figure out what might be wrong in my code, but unfortunately it was in vain. Any advice or rewriting is much appreciated from me.

+6
source share
5 answers

Change your code to:

 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class EditFile { public static void main(String[] args) { try{ String verify, putData; File file = new File("file.txt"); file.createNewFile(); FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); bw.write("Some text here for a reason"); bw.flush(); bw.close(); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while( (verify=br.readLine()) != null ){ //***editted //**deleted**verify = br.readLine();** if(verify != null){ //***edited putData = verify.replaceAll("here", "there"); bw.write(putData); } } br.close(); }catch(IOException e){ e.printStackTrace(); } } } 

The problem is that you call br.readLine() twice, which causes the application to read lines1 and then line2, and in your case you only have one line, which means that your program reads it in conditional form and when it’s comes to declaring it a verify variable, it stops because you no longer have data to read your file.

+6
source

I would do it like this:

 import java.io.*; public class EditFile { public static void main(String[] args) { try{ String verify, putData; File file = new File("file.txt"); file.createNewFile(); FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); bw.write("Some text here for a reason"); bw.flush(); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while( (verify=br.readLine()) != null ) { if(verify != null) { putData = verify.replaceAll("here", "there"); bw.write(putData); } } br.close(); bw.close(); }catch(IOException e){ e.printStackTrace(); } } } 
+1
source

use this code, I used it to delete the System.out logs and instructions in the java file. just change the match and replace string.

 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class FileReplace { List<String> lines = new ArrayList<String>(); String line = null; Scanner scan = null; public void doIt() { scan = new Scanner(System.in); while (true) { try { System.out .println("enter qualified file name ex.D:\\shiv\\shiv android all\\Main work space\\Welcomescreen1.java"); String path = scan.nextLine(); File f1 = new File(path); FileReader fr = new FileReader(f1); BufferedReader br = new BufferedReader(fr); int i = 0; while ((line = br.readLine()) != null) { if (line.contains("System.out")) { line = line.replace("System.out", "//"); } else if (line.contains("Log.")) { line = line.replace("Log", "//"); } lines.add(i, line); i++; } fr.close(); br.close(); FileWriter fw = new FileWriter(f1); BufferedWriter out = new BufferedWriter(fw); for (int j = 0; j < lines.size(); j++) { System.out.println(j + "." + lines.get(j)); out.append(lines.get(j)); out.newLine(); } out.flush(); out.close(); System.out .println("====================work done==================="); } catch (Exception ex) { ex.printStackTrace(); } } } public static void main(String args[]) { FileReplace fr = new FileReplace(); fr.doIt(); } } 
0
source
 import java.io.*; public class TextFile { public static void main(String[] args) { try { String verify, putData; File file = new File("G:\\Dairy.txt"); file.createNewFile(); FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); bw.write("I am Shah Khalid"); bw.flush(); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while( (verify=br.readLine()) != null ) { if(verify != null) { putData = verify.replaceAll("here", "there"); //bw.write(putData); } } br.close(); bw.close(); } catch(IOException e) { e.printStackTrace(); } System.out.println("Shah"); } } 

No need to enter bw.write(putData); because it just prints the expression twice.
No matter what you want in the file, just specify the correct path to the file and use the code above accordingly.

0
source
  File file = new File("/tmp/my.txt"); FileWriter fw; BufferedReader br; BufferedWriter bw; boolean no=false; String line; String data=""; String lessonPath="my new line"; try { if(!file.exists()){ fw = new FileWriter(file); bw = new BufferedWriter(fw); bw.write(lessonPath); bw.flush(); bw.close(); }else{ br = new BufferedReader(new FileReader(file)); while((line =br.readLine()) !=null){ if(!no){ data=line; no=true; }else{ data = data+"\n"+line; } } bw = new BufferedWriter(new FileWriter(file)); bw.write(data+"\n"+lessonPath); bw.flush(); bw.close(); } } catch (Exception ex) { ex.printStackTrace(); } 
-1
source

All Articles