gpt4 book ai didi

java - 文件的奇怪输出

转载 作者:行者123 更新时间:2023-12-02 13:11:57 25 4
gpt4 key购买 nike

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

该文件是在另一个 Activity 中制作的,非常简单:

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

在其他 Activity 中,它正在通过

读取
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
3

但是,我得到的结果是这样的:

/storage/emulated/0/Android/data/ys.test/files/saveGamesTest1.csv����sr��java.util.ArrayListx����a���I��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 文件读取

看这里https://www.tutorialspoint.com/java/java_serialization.htm在序列化对象部分

你必须使用

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 文件,您必须通过迭代数组并在文件中一一写入值并添加分隔符来构建它,或者按照此操作 How to serialize object to CSV file?

编辑: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");
}
}

}

关于java - 文件的奇怪输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43916411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com