如何在数组中进行对象序列化? [英] How do I do object serialization in an array?

查看:83
本文介绍了如何在数组中进行对象序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java 18错误

FileInputStream t = new FileInputStream(D:\\ abc.txt)



我尝试过:



包装序列号;



import java.io.Serializable;



public class serial implements Serializable {

public String name;

public String地址;

public String PhoneNo;



}



主要班级

包序列;



import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;



public class main {



public static void main (String [] args)抛出IOException,ClassNotFoundException {

// TODO自动生成的方法stub

serial [] X = new serial [10];

for(int i = 0; i< x.length; i ++)>

{



if(i == 1)

{

X [i] = new serial();

X [i] .name =ajay;

X [i] .address =delhi;

X [i] .PhoneNo = 78748;





}

其他

{

X [i] = new serial();

X [i] .name =abc;

X [i] .address = xyz;

X [i] .PhoneNo =1646;



}



}





FileOutputStream p = new FileOutputStream(D:\\ abc.txt);

ObjectOutputStream pp = new ObjectOutputStream(p);

pp.writeObject(X);



FileInputStream t = new FileInputStream(D:\\ abc.txt);



ObjectInputStream tt = new ObjectInputStream(t);

serial [] ll =(serial [])tt.readObject();

for(int i = 0; i< ll.length; i ++)>

{

System.out.println(ll [i] .name);

System.out.println(ll [i] .address);

System.out.println(ll [i] .PhoneNo);



}



}



}

java 18 error on the line inputstream
FileInputStream t = new FileInputStream("D:\\abc.txt")

What I have tried:

package serial;

import java.io.Serializable;

public class serial implements Serializable{
public String name;
public String address;
public String PhoneNo;

}

"main class"
package serial;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class main {

public static void main(String[] args) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
serial [] X= new serial[10];
for(int i=0;i<x.length;i++)>
{

if(i==1)
{
X[i]=new serial();
X[i].name ="ajay";
X[i].address = "delhi";
X[i].PhoneNo = "78748";


}
else
{
X[i]=new serial();
X[i].name ="abc";
X[i].address = "xyz";
X[i].PhoneNo = "1646";

}

}


FileOutputStream p = new FileOutputStream ("D:\\abc.txt");
ObjectOutputStream pp= new ObjectOutputStream (p);
pp.writeObject(X);

FileInputStream t = new FileInputStream("D:\\abc.txt");

ObjectInputStream tt = new ObjectInputStream(t);
serial[] ll = (serial[]) tt.readObject();
for(int i=0;i<ll.length;i++)>
{
System.out.println(ll[i].name);
System.out.println(ll[i].address);
System.out.println(ll[i].PhoneNo);

}

}

}

推荐答案

您正在尝试在关闭输出流之前打开输入流,因此该文件不存在于任何可用的形式中。在打开输入之前,只需添加

You are trying to open the input stream before you have closed the output stream, so the file does not exist in any usable form. Just add
p.close()




before you open the input.


这篇关于如何在数组中进行对象序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆