import java.io.*;public class Ex9a{

public void rollBack(BufferedReader in) throws IOException{	
	String line=""; 
	if((line=in.readLine())!=null){	 
		rollBack(in);	 
		}	 
	if(line!=null&&!"".equals(line)){ System.out.println(line);	 
	}	
}	 
	public static void main(String[] args) throws FileNotFoundException{	
		PrintStream out;
		
		Ex9a t=new Ex9a();		 
		out = new PrintStream(new FileOutputStream("outfile.txt"));
		out.println(ioe);
		out.close( );

		String fileName="infile.txt";	 
		try{	 
			BufferedReader in=new BufferedReader(new FileReader(fileName));
			t.rollBack(in);	 
			}catch(IOException ioe){
				System.out.println(ioe);	
				}	
			}
	}

infile is basically a bunch of lines of a poem and I need to reverse these lines and output them into an out file. The output file is created but it has nothing in it. Please help.

You're not actually writing anything to the file, which is why it comes back empty.

out = new PrintStream(new FileOutputStream("outfile.txt"));
		out.println(ioe);
		out.close( );

I'm not sure how this compiles, since there is nothing called ioe in scope at this point. This just creates outfile.txt for writing and closes it.


Your recursive read looks good. What if you passed it the handle to the outfile ("out" in your code) and replace the print-to-screen with a write-to file?
You'll want to delay the out.close() call until after the rollback method terminates.

This article has been dead for over six months. Start a new discussion instead.