从文本文件中读取对象

时间:2016-01-13 11:15:19

标签: java android

我有一个带有序列化对象的文本文件。该文件包含这样的数据 -

line[com.high.3449%]
line[com.high.58850?]
line[com.high.47646%]

我想读这个并在一个arraylist中逐个存储1。但是当我阅读它时,我只能读取第一行line[com.high.3449%]而不是一切。我使用以下逻辑来阅读 -

List<MyData> myobjects1 = new ArrayList<MyData>();
List<MyData> myobjects2 = new ArrayList<MyData>(); 
FileInputStream fin = new FileInputStream("/storage/200B-431F/Documents/MyData.txt");
ois = new ObjectInputStream(fin);
try {
    myobjects1 = (List<MyData>) ois.readObject();
    while (myobjects1 != null) {
        myobjects2.addAll(myobjects1);
        Log.d("hi", "second arraylist " + myobjects2);
    }
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} 

try {
    ois.close();
    server.sendData(myobjects2);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch(Exception e) {}

有人可以帮助我阅读所有数据并存储在arraylist myobjects2中吗?

2 个答案:

答案 0 :(得分:1)

当您回读数据时,问题似乎与您的循环有关。没有必要循环(它将永远循环)

而不是

myobjects1 = (List<MyData>) ois.readObject();
while (myobjects1 != null) {
   myobjects2.addAll(myobjects1);
   ...

你应该使用

myobjects1 = (List<MyData>) ois.readObject();
if (myobjects1 != null) {
   myobjects2.addAll(myobjects1);
}

如果你想遍历myobjects,你需要使用像

这样的东西
for (MyData myData : myobjects1) {
   myobjects2.add(myData);
}

添加我用来测试答案的代码。

package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class LoadFileObject {

    public static class MyData implements Serializable {
        private String line, content;

        public MyData(String line, String content) {
            setLine(line);
            setContent(content);
        }
        public String getLine() {
            return line;
        }
        public void setLine(String line) {
            this.line = line;
        }
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content= content;
        }
        public String toString() {
            return (line+content);
        }
    }

    @Test
    public void doWork() throws Exception {
        List<MyData> myobjects1 = new ArrayList<MyData>();
        myobjects1.add(new MyData("l1", "content1"));
        myobjects1.add(new MyData("l2", "content2"));

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("mydata.txt"));
        oos.writeObject(myobjects1);
        oos.close();

        List<MyData> myobjects2 = new ArrayList<MyData>(); 
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("mydata.txt"));
        myobjects2 = (List<MyData>) ois.readObject();
        System.out.println("read:" + myobjects2.size());
        for (MyData myData : myobjects2) {
            System.out.println("myData line:" + myData.getLine() + " content:" + myData.getContent());
        }

    }

}

答案 1 :(得分:0)

试试这个:

       FileInputStream fis = null;
                BufferedReader reader = null;

                try {
                    fis = new FileInputStream("/storage/200B-431F/Documents/MyData.txt/CPU.txt");
                    reader = new BufferedReader(new InputStreamReader(fis));

                    System.out.println("Reading File line by line using BufferedReader");

                    String line = reader.readLine();
                    while(line != null){
                        System.out.println(line);
                        line = reader.readLine();
                        // here line variable will hold the data for each line of your text file
                        // i.e you can add the string to the arrayList here
                          myObjects2.add(line);
                    }           

                } catch (FileNotFoundException ex) {
                            } 
                 catch (IOException ex) {

                 }