简体   繁体   English

输出未打印到txt文件Java

[英]Output not printing to txt file Java

I am trying to create a program which prints out all the possible combinations of the letters "AUGC". 我正在尝试创建一个程序,以打印出字母“ AUGC”的所有可能组合。 The output would really print to a txt file and the result would be a txt file with something like this: "AAA AAG AAC AAU AGA" etc Here is the code I have so far: 输出实际上将打印到txt文件,结果将是带有类似以下内容的txt文件:“ AAA AAG AAC AAU AGA”等,这是我到目前为止的代码:

import java.io.*;
import java.util.*;
import java.lang.*;

public class Permute {

    static String s = "ACGU";

    static void permute(int level, String prefix) {

        if (level == 0) {
            String fileName = "out.txt";
            PrintWriter outputStream = null;
            try {
                outputStream = new PrintWriter(fileName);
                outputStream.println(prefix);
                System.out.println(prefix);
                outputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return;
        }
        for (int i = 0; i < s.length(); i++) {
            permute(level - 1, prefix + s.charAt(i));
        }
     }

    public static void main(String[] args) {
        int k = 2;
        permute(k, "");
    }   

}

Currently the program is printing all the output to the console and only the last permutation to the txt file. 当前,该程序将所有输出打印到控制台,仅将最后一个排列打印到txt文件。 I would like it to print all the information to both. 我希望将所有信息都打印到两者。

Any help would be greatly appreciated 任何帮助将不胜感激

You are closing OutputStream prematurely. 您正在过早关闭OutputStream。 Since you are using recursion, pass OutputStream as a parameter to permute method from main method. 由于您正在使用递归,因此将OutputStream作为参数传递来从主方法置换方法。 Initialize stream and close it in main method. 初始化流并在main方法中将其关闭。

I maybe wrong on this one but do you not need to include a "\\n" as part of your outputStream.println(prefix); 我可能对此不对,但是您不需要在输出Stream.println(prefix)中包含“ \\ n”吗?

so that is looks like outputStream.println(prefix + "\\n"); 所以看起来像outputStream.println(prefix +“ \\ n”);

This will help as it seems to replacing the line with the newest details and not on a new line which is what you seem to be looking for. 这似乎很有用,因为它似乎用最新的详细信息代替了该行,而不是您正在寻找的新行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM