我一直在试图让这个代码的输出被打印在一个.txt文件中,但是我得到一个空文件作为输出,但是我在控制台中看到了正确的结果,所以问题出在StreamWriter上认为。该文件没有被打印出来,我做错了什么?
那么我做错了什么?
package Hierarchies;
public static void generateHierarchy() {
Set<Entry<Long, Node>> iterator = graph.entrySet();
int i =0;
for(Entry<Long,Node> e : iterator) {
System.out.println(i++ +" - " +e.getValue().firstParents());
}
}
@SuppressWarnings({ "resource" })
public static void main(String[] args) throws JWNLException {
// TODO Auto-generated method stub
dictionary = Dictionary.getDefaultResourceInstance();
File dir = new File("C:/Users/D060891/Desktop/Thesis/sentencesNYT");
for (File file : dir.listFiles()) {
try {
BufferedReader input = new BufferedReader(new FileReader(file));
String line;
while ((line = input.readLine()) != null) {
demonstrateListHelper(line);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
generateHierarchy();
PrintStream out;
try {
out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
哪里是你的文件作家? – haifzhan
[如何在Java中创建文件并写入文件?](http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-toa-a-文件在Java) – pczeus
可能的原因:1)您不冲洗或关闭PrintSteam。 2)你的目录是空的,没有文件被读取。 3)该目录中的所有文件都是空的。 –