我有这段代码,只是为了尝试将其写入文件。但是当我编译它时,它没有显示任何错误,但我的文件中的文本是不可读的,一些 Unicode 代码等......我使用 Eclipse IDE。这可能是什么原因?
public static void main(String[] args) {
String s = "Hello world!";
int i = 143141141;
try
{
//create new file with an ObjectOutputStream
FileOutputStream out = new FileOutputStream("test.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
//write something in a file
oout.writeObject(s);
oout.writeObject(i);
//close the stream
oout.close();
//create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("test.txt"));
//read and print what we wrote before
System.out.println("" + (String) ois.readObject());
System.out.println("" + ois.readObject());
}
catch(Exception ex)
{
ex.printStackTrace();
}
}