Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取从arraylist创建的文件_Java_Arraylist_Filereader_Filewriter - Fatal编程技术网

Java 读取从arraylist创建的文件

Java 读取从arraylist创建的文件,java,arraylist,filereader,filewriter,Java,Arraylist,Filereader,Filewriter,我写了一段代码将我的ArrayList保存到一个文件中,但我需要再次读取此文件,稍后重新使用相同的数组,但文件的写入方式很奇怪,有没有任何方法可以配置输出文件的写入方式?对不起,如果这个问题很愚蠢,这是我的第一个程序 保存ArrayList的代码: try { FileOutputStream fileOut = new FileOutputStream( "src//ServerInfo.txt"); ObjectOutputStream out =

我写了一段代码将我的ArrayList保存到一个文件中,但我需要再次读取此文件,稍后重新使用相同的数组,但文件的写入方式很奇怪,有没有任何方法可以配置输出文件的写入方式?对不起,如果这个问题很愚蠢,这是我的第一个程序

保存ArrayList的代码:

try {
      FileOutputStream fileOut = new FileOutputStream(
          "src//ServerInfo.txt");
      ObjectOutputStream out = new ObjectOutputStream(fileOut);
      out.writeObject(dataServer);
      out.close();
      fileOut.close();
} 

      catch (IOException i) {
      i.printStackTrace();
}
读取文件的代码:

try {
    File file = new File("src//ServerInfo.txt");
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);

    try {
        String s;
        while ((s = br.readLine()) != null) {
            dataServer.add(s);
        }
    } 
    finally {
        br.close();
    }
} 

catch (IOException ex) {
    ex.printStackTrace();
}
更改读或写代码都可以,我只需要一种方法来读取我编写的文件

输出文件llok如下所示:

¬í sr java.util.ArrayListxÒ™Ça I sizexp   w   t admint admint booklet@booklet.comt 
Administratorx
public void save(String fileName) throws FileNotFoundException {
    PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
    for (String element : dataServer)
        pw.println(element);
    pw.close();
}
它应该是什么样子:(这也是我第一次写的,在程序开始之前)


虽然您的读取代码对于所需的格式是合理的,但您正在以不同的格式编写文件。正如Dan Temple所指出的,对象流是Java默认序列化机制的一部分的双字节流。除了对象的内容之外,序列化对象还将包括诸如类类型和串行版本之类的内容。使用字符串以外的对象会使输出更加复杂

如果希望列表具有纯文本表示,请执行以下操作:

¬í sr java.util.ArrayListxÒ™Ça I sizexp   w   t admint admint booklet@booklet.comt 
Administratorx
public void save(String fileName) throws FileNotFoundException {
    PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
    for (String element : dataServer)
        pw.println(element);
    pw.close();
}

正如vefthym所提到的,这是一个几乎一字不差的从答案到的复制和粘贴。您可能希望向其中添加自己的异常处理,就像您在读取代码时所做的那样。

尝试查看每一行,以防出现任何错误

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
String line = null;
while ((line = reader.readLine()) != null) {

//AFTER READING 

split the line, then loop through the parts of the line. then add the element into a list.

问题是您试图将对象作为字符而不是“对象”来读取。有一种使用ObjectInputStream从文件中读取对象的简单方法(类似于用于写入对象的ObjectOutputStream)。因此,从文件中读取arraylist的代码如下:

ArrayList<String> input = new ArrayList<String>();
try{
      FileInputStream fin = new FileInputStream("src//ServerInfo.txt");
      ObjectInputStream ois = new ObjectInputStream(fin);
      input = (ArrayList) ois.readObject();
      ois.close();
      fin.close();
   }
catch(Exception e){
     e.getMessage();
}
for(int i=0;i<input.size();i++){
    System.out.println(input.get(i);
}
ArrayList input=new ArrayList();
试一试{
FileInputStream fin=新的FileInputStream(“src//ServerInfo.txt”);
ObjectInputStream ois=新ObjectInputStream(fin);
input=(ArrayList)ois.readObject();
ois.close();
fin.close();
}
捕获(例外e){
e、 getMessage();
}

对于(int i=0;i可能重复是的,您使用的是默认对象序列化,这就是为什么您会得到可怕的格式。请遵循@vefthym关于链接重复问题的建议,或者始终可以使用任何好的JSON库将其转换为JSON字符串。这基本上是一种序列化形式,但JSON i如果您希望将该文件传递给其他用户,它将得到广泛的使用和支持。请确保您使用的是链接副本中的@Amir Kost答案,而不是接受的答案(这会起作用,但无法利用序列化的优势)。严格来说不是重复。OP可能需要解释对象流是什么,以及如何正确写入文件。我可以问一下代码中的“Club:clubs”代表什么吗?是我的数组名吗?抱歉,我这是一个粗心的复制粘贴工作。我已经编辑了答案。注释
for(Type name:iterable)
是在Java 1.5中引入的。它允许您迭代任何
Iterable
或数组。名称和类型是您在循环中处理的每个元素的名称和类型。最初
clubs
Club
元素的集合。@GiovanniMarcolla。您应该通过单击复选标记来选择此答案如果它对你有用的话,就放在它旁边。