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.

Read each line and put it in a stack. Once that is done, remove each element from the stack and print it out.

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.

Cale - that's a correct solution, for a different problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.