Reversing Lines With Recursion Java

假如想象 提交于 2019-12-13 05:51:19

问题


I am just trying to reverse the lines which I receive from the input, but every single time I run my code, the output.txt file is empty. What am I missing? It appears mostly correct to me, even the recursion passage.

Thanks

import java.io.BufferedReader; 
import java.io.FileReader;
import java.io.FileWriter; 
import java.io.PrintWriter;

public class ReverseLines { 

  public static BufferedReader input;
  public static PrintWriter output;

  public static void main(String[] args) throws Exception{

    input = new BufferedReader(new FileReader(args[0]));
    output = new PrintWriter(new FileWriter(args[1]));
    reverse(input, output);

  }

  public static void reverse( BufferedReader input, PrintWriter output)
         throws Exception { 

    String line = input.readLine();
    if(line != null) {
    reverse (input, output);
    output.println(line);
    }    

  }

}

回答1:


Close the PrintWriter in your main method:

output.close();



回答2:


do output.flush() and check whether it works!



来源:https://stackoverflow.com/questions/15909734/reversing-lines-with-recursion-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!