【发布时间】:2012-03-30 15:26:07
【问题描述】:
我正在尝试将单个字符串和二维数组输出到 .txt 文件中,但遇到了问题,这是我项目的最后一部分。程序应该输出单行,逗号分隔,字符串,然后在下一行,打印出二维数组中的双精度,逗号分隔。我想能够将这全部打印到 .txt 文件中,然后在 excel 文件中打开该 .txt 文件以绘制信息。以下是我目前所拥有的,我知道可能存在比我看到的更多的错误:
public void writeFile(double[][] array, String filename)
{
try{
File f = new File("output.txt");
Scanner scan = new Scanner(f);
scan.useDelimiter("\\s*,\\s*");
String label = "Algoritm-1, Algorithm-2, Algorithm-3, "
+ "n, n-squared, n-cubed"; //Only one printing of this line
for (int i = 0; i <= 18; i++)
{
for (int j = 0; j <= 5; j++)
{
array[i][j] = scan.nextDouble(); //Having the current issue
}
scan.nextLine();
}
}
catch (FileNotFoundException fnf) {
System.out.println(fnf.getMessage());
}
}
【问题讨论】:
-
为什么要从输出文件中读取?这就是你的意思吗?你的代码没有意义
-
我以为我也看到了,但我想我一定是搞错了......!
-
您不会在该代码中写入文件。
-
扫描仪用于从文件中读取,您想要输出到文件中。用谷歌快速搜索 java 文件 i/o 应该会为您提供完成此操作所需的一切。
标签: java string io multidimensional-array