简体   繁体   English

用Java将数组保存到文本文件

[英]Saving an Array to a Text file in Java

I'm trying to print the values of an int array to a local file. 我正在尝试将int数组的值打印到本地文件。 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 ) 但是,我似乎找不到一种以标准格式( 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? 我可能做错了什么?

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.");
}

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

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. 有关如何反序列化对象的链接,因此您可以在链接的页面上检索值。

The toString() method on an array gives the "[I@1befab0" String. 数组上的toString()方法给出“ [I @ 1befab0””字符串。
So you need to loop over the array. 因此,您需要遍历数组。

You can split the problem in two: 您可以将问题分为两部分:

  1. for building the String (you can check the value in debug) 用于构建字符串(您可以在调试中检查值)

     StringBuilder b = new StringBuilder(); for (Object o : views) { b.append(String.valueOf(o)); } 
  2. for writing the String to a file 用于将字符串写入文件
    ... ...

When you print out the array, it calls the array's toString() function. 当您打印出数组时,它将调用数组的toString()函数。 So, what you are seeing is the reference value of the "views" array for as many values exist in the array. 因此,您所看到的是“视图”数组的引用值,因为该数组中存在许多值。

As Jon pointed out, you actually need to print the values of the array. 正如Jon所指出的,您实际上需要打印数组的值。 Of course, this assumes that the array contains objects that are nicely formatted via toString (or that the array holds primitives). 当然,这假定数组包含通过toString很好地格式化的对象(或者数组包含原语)。

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.");
    }
}
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. 没有比这容易的多了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM