【问题标题】:Saving the result to file - JAVA将结果保存到文件 - JAVA
【发布时间】:2012-12-07 07:00:15
【问题描述】:

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

这是它的保存方式:

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

我希望它像这样保存:

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

代码:

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());
}
}
}

【问题讨论】:

    标签: java dijkstra output


    【解决方案1】:

    在 Windows 中,您需要 "\r\n" 换行

    【讨论】:

      【解决方案2】:

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

      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();
      

      【讨论】:

      • 你又在写什么File?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 2017-05-07
      • 1970-01-01
      • 2020-02-15
      • 2019-03-25
      • 1970-01-01
      相关资源
      最近更新 更多