我有文件,我需要记录序列化对象.我打开ObjectOutputStream文件写作.如果我没有在文件中写入任何内容,则文件内容将被删除.我不希望在制作时删除内容ObjectOutputStream.
我的代码(我使用Guice),
@Provides
@ArticleSerializationOutputStream
public ObjectOutputStream getArticleObjectOutputStream(Config config) {
ObjectOutputStream out = null;
String fileName = config.getConfigValue(ARTICLE_SNAPSHOT);
try {
out = new ObjectOutputStream(new FileOutputStream(new File(fileName)));
} catch (IOException e) {
String errorMessage = String.format(IO_EXCEPTION_PROBLEM, fileName);
addError(errorMessage);
}
return out;
}
Run Code Online (Sandbox Code Playgroud) 我有这个发送字符串或整数,但如果我想发送一个字符串数组我应该使用什么?
// A string ("Hello, World").
out.write("Hello, World".getBytes());
// An integer (123).
out.write(ByteBuffer.allocate(4).putInt(123).array());
Run Code Online (Sandbox Code Playgroud)
提前致谢
我想读取我输出到.dat文件的多个对象(我自己的类Term),但我总是得到一个nullPointException或EOFException.
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(masterFile));
Object o = null;
while(( o = inputStream.readObject()) != null){
Term t = (Term)o;
System.out.println("I found a term");
}
Run Code Online (Sandbox Code Playgroud) 免责声明我的问题与以下两个链接不同
public class AppendableObjectOutputStream extends ObjectOutputStream {
public AppendableObjectOutputStream(OutputStream out) throws IOException {
super(out);
}
@Override
protected void writeStreamHeader() throws IOException {}
}
Run Code Online (Sandbox Code Playgroud)
如果我像在对象上写入可附加流那样继续,请关闭流。然后再次打开流,关闭另一个对象,依此类推。这样我就可以正确读取多个对象。
fileOutputStream = new FileOutputStream("abc.dat",true);
outputBuffer = new BufferedOutputStream(fileOutputStream);
objectStream = new AppendableObjectOutputStream(outputBuffer);
BucketUpdate b1 = new BucketUpdate("getAllProducts1",null,"1",null);
BucketUpdate b2 = new BucketUpdate("getAllProducts2",null,"2",null);
BucketUpdate b3 = new BucketUpdate("getAllProducts3",null,"3",null);
objectStream.writeObject(b1);
objectStream.writeObject(b2);
objectStream.writeObject(b3);
objectStream.close();
Run Code Online (Sandbox Code Playgroud)我试图通过套接字连接发送定制的对象.该类实现了serializable,但构造函数仍然NotSerializableException在尝试将对象写入套接字时抛出.我将在下面发布相关代码:
public class serilizableArrayHolder implements Serializable {
private ArrayList<Client> array= null;
public serilizableArrayHolder(ArrayList<Client> array) {
this.array=array;
}
public ArrayList<Client> getArray() {
return array;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我定制的课程.现在我试图从服务器发送一个arraylist到客户端,但我会在稍后阶段添加其他信息.send方法发布在我的服务器类下面,发布如下:
public void sendData(Socket clientSocket){
ObjectOutputStream out;
try {
serilizableArrayHolder temp = new serilizableArrayHolder(clientCollection);
out = new ObjectOutputStream(clientSocket.getOutputStream());
out.writeObject(temp); <---This line generates the error.
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我从服务器发送的方法.clientCollection是我试图发送的arrayList.
整个Client类:
public class Client implements Runnable, Serializable{
public Thread thread = new Thread(this);
private Socket s;
private …Run Code Online (Sandbox Code Playgroud) 我有一个使用SSLSocket连接到服务器的客户端.接下来,我尝试创建一个OOSObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream());
如果一切都在服务器端运行良好,这很好.但是,我想在客户端尝试创建ObjectOutputStream,但如果在60秒内没有发生,请记录错误并继续处理.我没有看到任何超时选项.如何做到这一点的任何例子?
SSLSocket sslsocket;
try {
System.setProperty("javax.net.ssl.trustStore", <myKeystore>);
System.setProperty("javax.net.ssl.trustStorePassword", <myPW>);
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
sslsocket = (SSLSocket) sslsocketfactory.createSocket(InetAddress.getLocalHost(), <myPort>);
} catch (Throwable e) {
logger.logError(e.getMessage());
return null;
}
// This is where it hangs forever
ObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream());
oos.flush(); // never gets here
oos.writeObject(orders);
ObjectInputStream ois = new ObjectInputStream(sslsocket.getInputStream());
Run Code Online (Sandbox Code Playgroud) 将对象写入ByteArrayOutputStream时,我得到了一些奇怪的结果.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(baos);
os.writeObject(null);
byte[] objectBytes = baos.toByteArray();
int objectSize = objectBytes.length;
Run Code Online (Sandbox Code Playgroud)
所以我给ByteArrayOutputStream写了一个null,然后当我从这个流中检索字节而不是找到0个字节时,我发现有5个.字节的值如下 -
如果我更改os.writeObject(null)为os.writeObject("A")我得到8个字节,这些是 -
那么这里发生了什么,如果我写0字节,我希望在检索字节数组时找到字节.然后我看到它增加了5个字节.因此,当我写"A"时,我希望它在字节数组中返回6个字节,但它返回8.这里发生了什么?
我有类:Client,Server和Background正在使用Player类.我真的不明白为什么我的Client类使用ObjectInputStream/ ObjectOutputStream不能正常工作.
我做得不好?哪里是我的错?
package Shooter2Dv27082013;
public class Player implements Serializable{
....
public int x=10;
public int y=10;
.... }
package Shooter2Dv27082013;
public class Background extends JPanel implements ActionListener, Serializable {
public int countCollisions=0;
private int time = 20; // 0.02s
Timer mainTimer = new Timer(time, this);
....
Player p = new Player(); ... }
Run Code Online (Sandbox Code Playgroud)
现在Client类:
package Shooter2Dv27082013;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] ar) {
JFrame frame = new …Run Code Online (Sandbox Code Playgroud) java sockets serialization objectoutputstream objectinputstream
我正在尝试编写一个列表,其中包含一个文本文件中的点列表.我设法编写了一个方法来保存一个点到文本文件.但是,我在输出中得到了一些额外的字符
try {
FileOutputStream fileOut = new FileOutputStream(path);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
for(int i = 0; i < pointList.size(); i++){
String s = parseString( pointList.get(i));
out.writeObject(s);
}
out.close();
fileOut.close();
} catch (IOException i) {
i.printStackTrace();
}
private static String parseString(Point P){
String point = String.valueOf(P.getX()) + "," + String.valueOf(P.getY()) ;
System.out.println("String: " +point);
return point;
}
Run Code Online (Sandbox Code Playgroud)
这是点列表
List<Point> pointList = new ArrayList();
pointList.add(new Point(100,200));
pointList.add(new Point(300,500));
pointList.add(new Point(400,200));
pointList.add(new Point(100,500));
pointList.add(new Point(400,200![enter image description here][1]));
Run Code Online (Sandbox Code Playgroud)
这是write方法的输出

我似乎不明白如何摆脱输出第一行的字符和每行的t
谢谢你的帮助
下面的代码将我的对象和 byte[] 写入文件,sigBytes 是我的 byte[]
ObjectOutputStream outputOS = new ObjectOutputStream(new FileOutputStream(outputFile));
outputOS.writeInt(sigBytes.length);
outputOS.write(sigBytes);
outputOS.writeObject(text);
outputOS.close();
Run Code Online (Sandbox Code Playgroud)
然后当我执行下面的代码时,我得到一个 java.io.OptionalDataException
ObjectInputStream inputIS = new ObjectInputStream(new FileInputStream(INPUT));
int length = inputIS.readInt();
byte[] sigBytes = new byte[length];
inputIS.read(sigBytes, 0, length);
String text = (String) inputIS.readObject();
Run Code Online (Sandbox Code Playgroud)
在我得到的错误下面String text = (String) inputIS.readObject():
java.io.OptionalDataException at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1305) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371) at encryption3.Encryption3.decrypt(Encryption3.java:34) at encryption3 .Encryption3.main(Encryption3.java:53)
编辑我不能让错误以最小的方式重复如下???我真的很累了..
public static void doThings() {
try {
File file = new File("C:/edges/input.ext");
String text = "Hello";
file.createNewFile();
byte[] sigBytes = (text).getBytes();
ObjectOutputStream …Run Code Online (Sandbox Code Playgroud)