将数据从标准输出写入java中的文本文件不起作用



我是java编程的新手,很长一段时间以来我一直在尝试让以下代码发挥作用,但尽管程序运行良好,但我似乎在重定向输出到的文本文件中没有看到任何输出。如果有人能帮我,我将不胜感激。以下是整个代码。我遇到的问题是,只有一行被写入文本文件,而在任何时间点,我在文件中只有一行。我知道这与每次重新初始化循环和对象有关,但我不知道如何克服这个问题,我知道这将是一个非常基本的错误。但我现在看不到,所以任何帮助都将不胜感激。感谢所有已经尽力提供帮助的人,你的建议非常有帮助。

 PrintStream out;
    for(int count = 0; count<list2.size(); count++)
    {
            String originalString = list2.get(count);
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String readLine = br.readLine();
                out = new PrintStream(new FileOutputStream("/Users/xyz/Desktop/metaDataFormatted.txt"));
                System.out.println(readLine);
                System.setOut(out);
                //out.flush();
                //out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("Error During Reading/Writing");
                }
                System.out.println(count+" >");
                //the following line is a method from another class which produces all the output on standard out.
                                    md.getHomePageLinks(originalString);



    }

在关闭流之前尝试调用System.out.println(data)

 System.out.println(readLine);
 System.setOut(out);

最新更新