递归和文本文件的麻烦

时间:2016-11-14 23:36:24

标签: java recursion text-files

这个程序输入“你好我的名字是bob”并向后吐出来。我真的需要帮助,使程序读入文本文件并向后吐出文本文件。提前致谢!

public class Recursion
{
 public static void main(String[] args) throws FileNotFoundException {
 System.out.println(printBackwards("hello my name is bob")); 
}



public static String printBackwards(String s){
 if(s.length() <= 1)
        return s;
    else 
        return printBackwards(s.substring(1,s.length()))+s.charAt(0);
  }
}

3 个答案:

答案 0 :(得分:0)

根据问题的评论,这将读取一个名为input.txt的文件,并使用您的方法将其保存到名为output.txt的新文件中,以反转String

input.txt中的所有行首先添加到List

然后从最后一个元素向后迭代List,并在每次迭代时将反转的String写入output.txt。

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;

public class Example {

    public static void main(String[] args) throws IOException {    
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
            Scanner scanner = new Scanner(new File("input.txt"));
            List<String> fileContents = new ArrayList<>();
            while (scanner.hasNextLine()) {
                fileContents.add(scanner.nextLine());
            }
            ListIterator<String> it = fileContents.listIterator(fileContents.size());
            while (it.hasPrevious()) {
                writer.write(printBackwards(it.previous()));
                writer.newLine();
            }
        }
    }

    public static String printBackwards(String s) {
        if (s.length() <= 1) {
            return s;
        } else {
            return printBackwards(s.substring(1, s.length())) + s.charAt(0);
        }
    }
}

但是,如果您只想将其显示到标准输出,则可以将其调整为以下内容:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;

public class Example {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File("input.txt"));
        List<String> fileContents = new ArrayList<>();
        while (scanner.hasNextLine()) {
            fileContents.add(scanner.nextLine());
        }
        ListIterator<String> it = fileContents.listIterator(fileContents.size());
        while (it.hasPrevious()) {
            System.out.println(printBackwards(it.previous()));
        }
    }

    public static String printBackwards(String s) {
        if (s.length() <= 1) {
            return s;
        } else {
            return printBackwards(s.substring(1, s.length())) + s.charAt(0);
        }
    }
}

或者正如我之前在评论中所说的那样,您可以一次性阅读整个文件并将其反转:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Example {

    public static void main(String[] args) throws FileNotFoundException {
        System.out.println(printBackwards(new Scanner(new File("file.txt")).useDelimiter("\\Z").next()));
    }

    public static String printBackwards(String s) {
        if (s.length() <= 1) {
            return s;
        } else {
            return printBackwards(s.substring(1, s.length())) + s.charAt(0);
        }
    }
}

答案 1 :(得分:-1)

这应该按预期工作。我假设在filename_in.txt你只有一行,否则你必须循环(我允许你这样做作为练习):

public static void main(String[] args){
       Scanner in = null; 
       PrintWriter writer = null;
       try{
           in = new Scanner(new FileReader("filename_in.txt"));
           writer = new PrintWriter("filename_out.txt");
           writer.println(printBackwards(in.nextLine()));
        } catch (Exception e) {
           e.printStackTrace();
        }
       finally {
           in.close(); 
           writer.close();
       }
    }

答案 2 :(得分:-1)

使用此代码从文本文件中获取要反转的字符串:

try{
    String myString;
    BufferedReader input = new BufferedReader(new FileReader("Filepath.txt");
    while((myString = input.readLine()) != null){}
}
catch(FileNotFoundException ex){
    //Error Handler Here
}
catch(IOException ex){
    //Error Handler Here
} finally {
    try{
        if(br != null) br.close();
    }
    catch(IOException ex){
        ex.printStackTrace();
    }
}

不要忘记导入:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException