Java 文件的奇数输出

Java 文件的奇数输出,java,android,fileinputstream,serializable,Java,Android,Fileinputstream,Serializable,我在读取文件时得到的输入有问题 该文件是在另一个活动中创建的,非常简单: ArrayList stuff = new ArrayList(); stuff.add("1,2,3"); try{ String saveFile = "saveGamesTest1.csv"; FileOutputStream saveGames = openFileOutput(saveFile, getApplicationContext().MODE_APPEND); ObjectOutputStr

我在读取文件时得到的输入有问题

该文件是在另一个活动中创建的,非常简单:

ArrayList stuff = new ArrayList();
stuff.add("1,2,3");

try{
String saveFile = "saveGamesTest1.csv";
FileOutputStream saveGames = openFileOutput(saveFile, getApplicationContext().MODE_APPEND);    

ObjectOutputStream save = new ObjectOutputStream(saveGames);

save.writeObject(stuff);    
save.close(); }
在另一个活动中,通过

try {
            FileInputStream fileIn=openFileInput("saveGamesTest1.csv");
            InputStreamReader InputRead = new InputStreamReader(fileIn);

            Scanner s = new Scanner(InputRead).useDelimiter(",");
            System.out.println(s.next());
            System.out.println(s.next());
            System.out.println(s.next());

        }
我期待(并希望)能像这样得到一个结果

1
2
三,

然而,我得到的结果是:

/存储/模拟/0/Android/data/ys.test/files/saveGamesTest1.csv����sr��java.util.ArrayListx����A.���我��sizexp������W������T��1
2
3x

我做错了什么?
.

编辑
我尝试按以下建议序列化,如下所示:

public class Save implements java.io.Serializable {
        public String name;
        public String address;
        public transient int SSN;
        public int number;

    }


    public void save(){

        Save e = new Save();
        e.name = "Reyan Ali";
        e.address = "Phokka Kuan, Ambehta Peer";
        e.SSN = 11122333;
        e.number = 101;

        try {
            String saveFile = "save.ser";
            FileOutputStream saveGames = openFileOutput(saveFile, getApplicationContext().MODE_APPEND);
            ObjectOutputStream out = new ObjectOutputStream(saveGames);
            out.writeObject(e);
            out.close();
            saveGames.close();
            System.out.printf("Serialized data is saved in save.csv");
        }
        catch(IOException i) {
            i.printStackTrace();
            out.println("Save exception gepakt");
        }
    }  

然而,
out.writeObject(e)
给出一个错误,表示这是不可序列化的

您没有将对象存储为csv,而是作为序列化java对象,您必须作为对象而不是csv文件读取

看看这里 在序列化对象部分时

你必须使用

FileInputStream in = null;
ObjectInputStream ois = null;
ArrayList stuff2 = null;
try {
    in = openFileInput("saveGamesTest1.csv");
    ois = new ObjectInputStream(in);
    stuff2 = (ArrayList) ois.readObject();
} catch(IOException e) {...}
catch(ClassNotFoundException c) {...}
finally {
    if (ois != null) {
        ois.close();
    }
    if (in != null) {
        in.close();
    }
}
如果您想要一个csv文件,您必须构建它,例如,通过迭代数组,在文件中逐个写入值,并添加分隔符或遵循以下步骤

编辑: Java 7中序列化对象(此处为示例中的列表)并反序列化的优雅方式:

public class Main {

public static void main(String[] args) {
    List<Integer> lists = new ArrayList<>();
    List<Integer> readList = null;
    String filename = "save.dat";
    lists.add(1);
    lists.add(2);
    lists.add(3);

    //serialize
    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
        oos.writeObject(lists);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //don't need to close because ObjectOutputStream implement AutoCloseable interface

    //deserialize
    try (ObjectInputStream oos = new ObjectInputStream(new FileInputStream(filename))) {
        readList = (List<Integer>) oos.readObject();
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }
    //don't need to close because ObjectInputStream implement AutoCloseable interface


    //test
    if(!lists.equals(readList)) {
        System.err.println("error list saved is not the same as the one read");
    }
}

}
公共类主{
公共静态void main(字符串[]args){
List lists=新建ArrayList();
List readList=null;
字符串filename=“save.dat”;
增加(1);
增加(2);
增加(3);
//连载
try(ObjectOutputStream oos=newobjectoutputstream(newfileoutputstream(filename))){
oos.writeObject(列表);
}捕获(IOE异常){
e、 printStackTrace();
}
//不需要关闭,因为ObjectOutputStream实现了自动关闭接口
//反序列化
try(ObjectInputStream oos=newobjectinputstream(newfileinputstream(filename))){
readList=(List)oos.readObject();
}捕获(IOException | ClassNotFoundException e){
e、 printStackTrace();
}
//不需要关闭,因为ObjectInputStream实现了自动关闭接口
//试验
如果(!lists.equals(readList)){
System.err.println(“保存的错误列表与读取的错误列表不同”);
}
}
}

JavaScript!=我试着用你给我的。但它会导致“out.writeObject(e);”处出错说它不是serializable你的对象实现serializable了吗?你第一次实现“stuff”工作了吗?如果是这样的话,你只需要读而不是写你的作品,所以我会试试你的建议。在你写这篇文章的时候,我用Serializable编辑了我的第一篇文章,但它不起作用谢谢你的帮助,我终于让它起作用了!