使用递归 Java 反转行 [英] Reversing Lines With Recursion Java

查看:72
本文介绍了使用递归 Java 反转行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想反转从输入中接收到的行,但是每次运行我的代码时,output.txt 文件都是空的.我错过了什么?对我来说,它似乎大部分是正确的,即使是递归段落.

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.

谢谢

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);
    }    

  }

}

推荐答案

关闭 main 方法中的 PrintWriter:

output.close();

这篇关于使用递归 Java 反转行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆