我有以下問題。在TXT文件Printstream上的錯誤。只有最後的聲明ist寫
```
1 3 1.
```
實際上是字符串變量打印,它必須輸出
```
1 1 1
1 2 1
1 3 1
```
我不知道,這裏的錯誤是在代碼。這似乎是代碼每次都被覆蓋,最後一條語句被覆蓋。任何人都可以幫助我
public class RankingExportTest {
private static final String LINE_SEPARATOR = "\r\n";
Set<String> exportGraph(DirectedUnweightedGraph<CommonNode, CommonEdge<CommonNode>> graph) throws IOException {
Set<String> rows = new LinkedHashSet<>((int)graph.getEdgeCount());
for(CommonNode fromNode: graph.getNodes()) {
for(CommonNode toNode: graph.adjacentTo(fromNode)) {
String row = String.format(
"%d %d 1",
fromNode.getIdentity().intValue(),
toNode.getIdentity().intValue()
);
rows.add(row);
System.out.println(row);
try {
PrintWriter out;
out = new PrintWriter("/Users/......");
out.print(rows);
out.close();
//System.out.println(row);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
return rows;
}
是否可以用覆蓋模式打開文件而不是追加內容?嘗試從這裏的解決方案: [可能的副本](http://stackoverflow.com/questions/14443662/printwriter-add-text-to-file) –