简体   繁体   English

如何使用JFileChooser保存/加载ArrayList数组?

[英]How do I use JFileChooser to save/load an array of ArrayList?

I have the following code: 我有以下代码:

ArrayList<String> [] hotelArray = (ArrayList<String>[ ]) new ArrayList[5];
for (int i = 0; i < 5; i++) {
    hotelArray[i] = new ArrayList<String>();
}

hotelArray[0].add("NY hotel1");
hotelArray[0].add("NY hotel2");
hotelArray[0].add("NY hotel3");
hotelArray[2].add("Japan Hotel");
    hotelArray[2].add("Japan Hotel2");
hotelArray[3].add("Nova Hotel");
hotelArray[4].add("Tokyo hotel");

Now I want to know, how do I save hotelArray to a single file, using JFileChooser. 现在,我想知道如何使用JFileChooser将hotelArray保存到单个文件中。 Also how do I read file using JFileChooser ? 另外,我如何使用JFileChooser读取文件? When I am reading, can I restrict to just reading say hotelArray[0] elements? 当我阅读时,是否可以只阅读诸如hotelArray [0]的元素?

In the beginning the JFileChooser does nothing else than pick a filename and give you the file behind the name. 在开始时,JFileChooser除了选择文件名并为您提供文件名之外,什么也不做。 What you need first is a method to write a file (just write down the filename in your code and change that part later). 您首先需要一种写入文件的方法(只需在代码中写下文件名,然后再更改该部分)。 Read this one page tutorial on how to write some text into a file . 阅读此一页教程,了解如何将一些文本写入文件 And here is a little peace of code, that shows you how to read a file line by line. 这里有一点代码安全性,它向您展示了如何逐行读取文件

Now you only have to iterate through your ArrayList and write each list entry as a new line into your file. 现在,您只需要遍历ArrayList并将每个列表条目作为新行写入文件中。 The same way you can read it back from the file into the ArrayList 您可以将其从文件读回ArrayList的方法相同

JFileChooser returns you a File object. JFileChooser返回一个File对象。 And then you are free to do everything with it. 然后,您可以自由地执行所有操作。 Have look at manual first. 先看看手册

JFileChooser either selects a file from the disk to be read, or selects / creates a file to be written. JFileChooser从磁盘上选择一个要读取的文件,或者选择/创建一个要写入的文件。

The file format it up to you. 该文件由您决定。 You have to convert the Java object(s) to bytes that can be written to and read from the disk. 您必须将Java对象转换为可以写入磁盘和从磁盘读取的字节。

Some people would say to serialize the Java object(s). 有人会说要序列化Java对象。 However, this is an inflexible method. 但是,这是一种不灵活的方法。 If you add or take away any fields from your Java object(s), your serialization changes. 如果您从Java对象添加或删除任何字段,则序列化会更改。

You can write your Java object(s) to a Java Properties file. 您可以将Java对象写入Java 属性文件。 This is a good choice if you have a limited amount of data to write to the disk. 如果要写入磁盘的数据量有限,这是一个不错的选择。

You can write your Java object(s) in XML. 您可以用XML编写Java对象。 This is a good choice if you want to maintain a hierarchical relationship with your Java object(s). 如果要与Java对象保持层次关系,这是一个不错的选择。

You can write your Java object(s) to a relational database like SQLite . 您可以将Java对象写入SQLite之类的关系数据库。 This is a good choice if you have many relational Java object(s). 如果您有许多关系Java对象,这是一个不错的选择。

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

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