在 Java 中将数组保存到文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1375217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving an Array to a Text file in Java
提问by
I'm trying to print the values of an int
array to a local file. However, I can't seem to find a way to print the integers out in their standard form (1,2,3
) instead of a heap address: ([I@1befab0
)
我正在尝试将int
数组的值打印到本地文件。但是,我似乎找不到以标准形式 ( 1,2,3
) 而不是堆地址打印整数的方法: ( [I@1befab0
)
My code fragment is below:
我的代码片段如下:
PrintWriter pr = new PrintWriter("file");
for (int i=0; i<views.length ; i++){
pr.println(Arrays.toString(views));
}
pr.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("No such file exists.");
}
}
What could I be doing wrong?
我可能做错了什么?
回答by KLE
The toString() method on an array gives the "[I@1befab0" String.
So you need to loop over the array.
数组上的 toString() 方法给出“[I@1befab0”字符串。
所以你需要遍历数组。
You can split the problem in two:
您可以将问题分为两部分:
for building the String (you can check the value in debug)
StringBuilder b = new StringBuilder(); for (Object o : views) { b.append(String.valueOf(o)); }
for writing the String to a file
...
用于构建字符串(您可以在调试中检查值)
StringBuilder b = new StringBuilder(); for (Object o : views) { b.append(String.valueOf(o)); }
用于将字符串写入文件
...
回答by Jon Skeet
You're trying to print the whole array out each time. Try this:
您每次都试图打印整个数组。尝试这个:
(I've made minimal changes. The fact that you're catching Exception
is a generally bad idea though, and you should usually close the writer in a finally
block.)
(我做了最小的更改。不过,您正在捕捉的事实Exception
通常是一个坏主意,您通常应该在一个finally
块中关闭编写器。)
try
{
PrintWriter pr = new PrintWriter("file");
for (int i=0; i<views.length ; i++)
{
pr.println(views[i]);
}
pr.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("No such file exists.");
}
回答by JasCav
When you print out the array, it calls the array's toString() function. So, what you are seeing is the reference value of the "views" array for as many values exist in the array.
当您打印出数组时,它会调用数组的 toString() 函数。因此,您看到的是“views”数组的参考值,因为数组中存在许多值。
As Jon pointed out, you actually need to print the values of the array. Of course, this assumes that the array contains objects that are nicely formatted via toString (or that the array holds primitives).
正如 Jon 指出的,您实际上需要打印数组的值。当然,这假设数组包含通过 toString 很好地格式化的对象(或者数组包含基元)。
回答by OMG Ponies
Are you writing to a file to persist the array contents? If so, serialize the object (array in this case). There's a link on how to deserialize the object so you can retrieve the value on the linked page.
您是否正在写入文件以保留数组内容?如果是,则序列化对象(在本例中为数组)。有一个关于如何反序列化对象的链接,以便您可以检索链接页面上的值。
回答by Matt
is the array perhaps a multiple dimension array? If so, use java.util.Arrays.deepToString
.
该数组可能是一个多维数组吗?如果是这样,请使用java.util.Arrays.deepToString
.
PrintWriter pr = new PrintWriter("file");
for (int i=0; i<views.length ; i++){
pr.println(Arrays.deepToString(views));
}
pr.close();
回答by waxwing
Most answers here focus on the fact that you need to iterate over the array instead of printing all of it in one go. But Arrays.toString(...) is supposed to return the same as Arrays.asList(...).toString(), which, incidentally, should be in the [1,2,3] form.
这里的大多数答案都集中在您需要遍历数组而不是一次性打印所有数组的事实上。但是 Arrays.toString(...) 应该返回与 Arrays.asList(...).toString() 相同的返回值,顺便说一下,它应该采用 [1,2,3] 形式。
I suggest therefore, as Matt does in his answer, that views
in fact a double-nested array.
因此,我建议,正如马特在他的回答中所做的那样views
,实际上是一个双嵌套数组。
The fixed code would in that case be:
在这种情况下,固定代码将是:
try {
PrintWriter pr = new PrintWriter("file");
for (int i=0; i<views.length ; i++){
pr.println(Arrays.toString(views[i]));
}
pr.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("No such file exists.");
}
}
回答by gshauger
PrintWriter pr = new PrintWriter("file");
for (int i=0; i<views.length ; i++){
pr.println("" + views[i]);
}
pr.close();
It doesn't get much easier than that.
没有比这更容易的了。