我在java中有一个示例程序在eclipse中运行完全正常,但是当我尝试使用java在linux中编译它时给我错误。请帮忙。我在代码结束后也附加了错误跟踪:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
public class Main {
public String toStringMethod(TestObject test) {
String serializedObject = "";
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(test);
so.flush();
serializedObject = bo.toString();
} catch (Exception e) {
System.out.println(e);
}
return serializedObject;
}
public TestObject toObjectMethod(String string) {
String serializedObject = string;
TestObject obj = null;
try {
byte b[] = serializedObject.getBytes();
ByteArrayInputStream bi = new ByteArrayInputStream(b);
ObjectInputStream si = new ObjectInputStream(bi);
obj = (TestObject) si.readObject();
} catch (Exception e) {
System.out.println(e);
}
return obj;
}
public static void main(String[] args) {
Main mn = new Main();
TestObject object = new TestObject();
object.setString(10);
String str = mn.toStringMethod(object);
TestObject newObject = mn.toObjectMethod(str);
newObject.printHello();
}
}
class TestObject implements Serializable{
char chars[];
String str;
public String setString(int numberOfBytes) {
numberOfBytes = numberOfBytes/2;
chars = new char[numberOfBytes];
Arrays.fill(chars, 'a');
str = new String(chars);
return str;
}
public String getString() {
return str;
}
public void printHello() {
System.out.println("hello tester");
}
}
错误跟踪是: java.io.StreamCorruptedException:无效的流标题:EFBFBDEF
答案 0 :(得分:2)
因为字符集。在Windows中,默认字符集是windows-1252。但Linux默认字符集是UTF-8。
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.Arrays;
public class Main {
public String toStringMethod(TestObject test) {
String serializedObject = "";
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(test);
so.flush();
so.close();
serializedObject = bo.toString("windows-1252");
System.out.println(serializedObject);
} catch (Exception e) {
System.out.println(e);
}
return serializedObject;
}
public TestObject toObjectMethod(String string) {
String serializedObject = string;
TestObject obj = null;
try {
byte b[] = serializedObject.getBytes("windows-1252");
ByteArrayInputStream bi = new ByteArrayInputStream(b);
ObjectInputStream si = new ObjectInputStream(bi);
obj = (TestObject) si.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public static void main(String[] args) {
Main mn = new Main();
TestObject object = new TestObject();
object.setString(10);
String str = mn.toStringMethod(object);
TestObject newObject = mn.toObjectMethod(str);
newObject.printHello();
}
}
class TestObject implements Serializable{
char chars[];
String str;
public String setString(int numberOfBytes) {
numberOfBytes = numberOfBytes/2;
chars = new char[numberOfBytes];
Arrays.fill(chars, 'a');
str = new String(chars);
return str;
}
public String getString() {
return str;
}
public void printHello() {
System.out.println("hello tester");
}
}
如果更改字符集,则其工作正常。