2012-12-07 72 views
0

我有一个非常愚蠢的问题......我不知道如何将结果保存在输出文件中,第二个数组保存在较低的行中。将结果保存到文件 - JAVA

这是如何保存:

DIST [0 10 13 10 6 18] PRED [-15 2 10 1 4]

我希望它保存这样的:

dist[0 10 13 10 6 18 ] 
pred[-15 2 5 1 4 ] 

CODE:

try{ 
outputFile = new File("Out0304.txt"); 
out = new FileWriter(outputFile); 
out.write("\n" + "dist["); 
out.write("\n"); 

for (Top way : tops){ 
    out.write(way.shortest_path + " "); 
} 

out.write("]\n"); 
out.write("\n"); 
out.write("\n" + "pred["); 

for (Top ww : tops){ 
    if (ww.previous != null) { 
     out.write(ww.previous.number + " "); 
    } 
      else{ 
     out.write("-1"); 
    } 
} 
out.write("] \n "); 
out.close(); 
} 

catch (IOException e){ 
System.out.println("Blad: " + e.toString()); 
} 
} 
} 

回答

0

您可以使用以下写入文件:

PrintWriter pw = new PrintWriter(new FileWriter("fileName"),true); 
pw.println("["); 
for(int i=0;i<pred.length;i++) 
{ 
pw.println(pred[i]+" "); 
} 
pw.println("]"); 
pw.close(); 
+0

你又在写什么'文件? – Makoto

+0

@Makoto编辑代码 – 2012-12-07 07:15:35

2

在Windows,你需要"\r\n"新线

+0

该死的,谢谢:)这是我需要的:) –

+0

@MaciejJanuszewski,可以接受的答案呢? –

相关问题