【问题标题】:Write console output to a .txt file将控制台输出写入 .txt 文件
【发布时间】:2013-07-08 18:39:27
【问题描述】:

我正在尝试将我的 java 控制台输出写入桌面中的 .txt 文件。 但是当该方法开始时,我有控制台输出并创建了 txt 文件,但它是空的,我意识到 BufferedReader (in) 不起作用......(因为“ok1 - i”语句)

问题是为什么?或者我的代码有什么问题? 这是我的代码,所以你可以看到并运行它

   package noobxd;

   import java.io.BufferedReader;
   import java.io.BufferedWriter;
   import java.io.FileWriter;
   import java.io.IOException;
   import java.io.InputStreamReader;
   import java.util.Random;

   public class Main {

public static void main(String[] args) throws IOException {
    String path = "C:\\Users\\Mario\\Desktop\\output.txt";

    generate_codes();

    writetxt(path);
}

private static void writetxt(String path) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    BufferedWriter out = new BufferedWriter(new FileWriter(path));
    try {
        String inputLine;
        inputLine = "";
        int i=0;
        System.out.println("Starting");
        while (!inputLine.isEmpty()){
            System.out.println("ok1"+i);
            inputLine = in.readLine();
            System.out.println("ok2"+i);
            out.write(inputLine);
            System.out.println("ok3"+i);
            out.newLine();
            System.out.println("ok4"+i);
            i++;
        }
        System.out.print("Write Successful");
    } catch (IOException e1) {
        System.out.println("Error during reading/writing");
    } finally {
        out.close();
        in.close();
    }
}

private static void generate_codes() {
    Random rnd = new Random();
    for (int i = 0; i < 30; i++) {
        int code = rnd.nextInt(201) + 100;
        int students = rnd.nextInt(31) + 40;
        int j = rnd.nextInt(4);
        String type = new String();
        switch (j) {
            case 0:
                type = "Theory";
                break;
            case 1:
                type = "Lab";
                break;
            case 2:
                type = "Practice";
                break;
            case 3:
                type = "Exam";
                break;
        }
        System.out.println("TEL" + code + "-TopicNumber" + i + "-" + students + "-" + type);

    }
}
}

感谢您的宝贵时间,请帮我解决我的问题。

【问题讨论】:

  • +1 表示 noobxd 包名 ;)
  • @UncleIroh haha​​h lol 那是因为这是一个“额外的应用程序”,可以帮助我导出一个 txt 文件以在我的基于 java-sql 的应用程序中使用(主要的,可以导入一个 txt 文件)所以这只是在控制台中然后我将其命名为 noobxd

标签: java bufferedreader bufferedwriter


【解决方案1】:
String inputLine;
inputLine = "";
...
while (!inputLine.isEmpty())  // this is false and so the loop is not executed

以后,请学习使用调试工具,并更仔细地阅读您的代码。如果您尝试阅读直到 EOF,请使用

while ((inputLine = in.readLine()) != null) {
    ...
}

【讨论】:

  • 好吧,但是如果我将其设置为(while(inputLine.isEmpty())),则输出只是... TEL221-TopicNumber27-55-Lab TEL280-TopicNumber28-58-Practice TEL174-TopicNumber29-51-Lab Starting ok10
  • 它应该像一个“标志”,显示该方法是否已正确执行,因此使用此输出(只是 ok1 0)(当第 i = 0 行(第一行)时首先确定)我知道我进入了while语句,但是inputLine = in.readLine()方法没有执行
  • @user2161991 我知道它是什么,但我不知道它应该是什么。你不能指望我们非常熟悉你的程序的行为......
【解决方案2】:

如果你想一直循环直到用户在控制台输入一个空行,你可能想要类似的东西

while (true) {
        System.out.println("ok1"+i);
        inputLine = in.readLine();
        if (inputLine.isEmpty()) 
            break;
        // the rest of your loop
}

【讨论】:

    【解决方案3】:

    你可能应该这样做:

    inputLine = in.readLine();
    while (inputLine != null && !inputLine.isEmpty()){
        System.out.println("ok1"+i);
        System.out.println("ok2"+i);
        out.write(inputLine);
        System.out.println("ok3"+i);
        out.newLine();
        System.out.println("ok4"+i);
        i++;
        inputLine = in.readLine();
    }
    

    【讨论】:

    • 我按照你说的做了,但是inputLine=in.readLine();还是没有被执行,我不知道为什么
    猜你喜欢
    • 2023-03-16
    • 2012-12-18
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 2020-04-10
    • 2012-02-01
    相关资源
    最近更新 更多