I have a sample programme in java which runs totally fine in eclipse but gives me error when i try to compile it in linux using java. please help. I have attached the error trace also after the end of code:
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");
}
}
The error trace is : java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
Its because of character set. In windows default character set is windows-1252. But Linux default character set is 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");
}
}
If you change the character set its working fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.