Question

I have to edit the contents of a file and write the edited conted to another file.Here is the code iam using .

  import java.io.*;     
import java.util.*;     

public class TestRef {     
    ArrayList<String> lines = new ArrayList<String>();     
    String line= null;     
    public void printThis(){     
        try{     
    FileReader fr = new FileReader("C:\\Users\\questions.txt");     
    BufferedReader br = new BufferedReader(fr);     
    FileWriter fw = new FileWriter("C:\\Users\\questions_out.txt");     
    BufferedWriter out = new BufferedWriter(fw);     
    while((line=br.readLine()) != null) {  
     if(line.contains("Javascript"))
      line.replace("Javascript"," JAVA");          
        lines.add(line);     
        out.write(line);  
        }                    

        }     
    catch(Exception e){}     
    }     

    public static void main(String [] args){     
            TestRef tr = new TestRef();     
            tr.printThis();     
        }     
} 

So this is like reading one line at a time and printing it back to the file. But when I execute this code the output file is blank.? Can you please provide me with a sample code, how to read from a file, make change in the content and write the whole file to a new file ?

Was it helpful?

Solution

Well, a few problems:

  • You're never closing either your input or your output. Closing will also flush - it's possible that something's just not being flushed. You should close stream-based resources in a finally block, so that they end up being closed even in the face of an exception. (Given that you should be closing, I wouldn't bother explicitly flushing as well. Just make sure you close the top-level abstraction - i.e. out (and br).
  • You're catching Exception and then swallowing it. It could well be that an exception is being thrown, but you're not able to tell because you've swallowed it. You should at least be logging it, and probably stopping at that point. (I'd also suggest catching IOException instead of Exception.)
  • You're using FileWriter and FileReader which doesn't allow you to specify the input/output encoding - not the issue here, but personally I like to take more control over the encodings I use. I'd suggest using FileInputStream and FileOutputStream wrapped in InputStreamReader and OutputStreamWriter.
  • You're calling String.replace() and ignoring the result. Strings are immutable - calling replace won't change the existing string. You want:

    line = line.replace("Javascript"," JAVA");
    
  • You're never using your lines variable, and your line variable would be better as a local variable. It's only relevant within the method itself, so only declare it in the method.
  • Your code would be easier to follow if it were more appropriately indented. If you're using an IDE, it should be able to do this for you - it makes a huge difference in readability.

The first one is the most likely cause of your current problem, but the rest should help when you're past that. (The point about "replace" will probably be your next issue...)

OTHER TIPS

You are missing out.flush().

BufferedWriters don't write anything until either you flush them, or their buffer fills up.

Close the print writer, outside the loop.

out.flush();
out.close();

Moreover you are writing strings to new lines, if you just want to replace javascript with Java, then you might also wanna write '\n', next line character to new file where old file contains new line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top