File.io读取文件(六)
2017-04-27 14:10
218 查看
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
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 ObjectSerializeDemo {
public static void main(String[] args) {
String path="D:/aa.txt";
serialeTo(path);
seriale(path);
}
//反序列化
public static void seriale(String srcpath){
try(
ObjectInputStream bis=new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(
new File(srcpath))));
){
Object obj=bis.readObject();
Student st=null;
if(obj instanceof Student){
st=(Student)obj;
}
//bis.close();
System.out.println(st.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//序列化
public static void serialeTo(String destpath){
//创建数据源
Student st=new Student("杨幂",33);
File fos=new File(destpath);//无论保存什么格式类型的文件,都是乱码,这里
try(//jdk1.7,资源关闭新特性
ObjectOutputStream dos=new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(fos)));
){
dos.writeObject(st);
dos.flush();//刷新管道数据
//dos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//要实现对象反序列化/序列化必须实现Serializable接口
class Student implements java.io.Serializable{
private String name;
private transient int age;//不需要序列化,就用transient修饰
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
import java.io.BufferedOutputStream;
import java.io.File;
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 ObjectSerializeDemo {
public static void main(String[] args) {
String path="D:/aa.txt";
serialeTo(path);
seriale(path);
}
//反序列化
public static void seriale(String srcpath){
try(
ObjectInputStream bis=new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(
new File(srcpath))));
){
Object obj=bis.readObject();
Student st=null;
if(obj instanceof Student){
st=(Student)obj;
}
//bis.close();
System.out.println(st.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//序列化
public static void serialeTo(String destpath){
//创建数据源
Student st=new Student("杨幂",33);
File fos=new File(destpath);//无论保存什么格式类型的文件,都是乱码,这里
try(//jdk1.7,资源关闭新特性
ObjectOutputStream dos=new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(fos)));
){
dos.writeObject(st);
dos.flush();//刷新管道数据
//dos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//要实现对象反序列化/序列化必须实现Serializable接口
class Student implements java.io.Serializable{
private String name;
private transient int age;//不需要序列化,就用transient修饰
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
相关文章推荐
- Spark 读取hdfs上的文件 错误:Caused by: java.io.IOException: Filesystem closed的处理
- 第一章:读取文件一行IO::File
- Java IO 文件读取问题记录(class.getResourceAsStream 和 new FileInputStream)
- C#IO 文件读取笔记一(File类和Directory类相关属性,FileInfo类和DirectoryInfo类)
- Python3 pandas read_csv 读取txt文件报错:IOError: Initializing from file failed
- File.io读取文件(二)
- Java,IO之FileInputStream和FileReader读取文件(疯狂java讲义)
- File.io读取文件(三)
- Java 流(Stream)、文件(File)和IO:读取本地文件;java中的目录
- Python webdriver 读取本地csv文件中数据 提示:IOError: [Errno 2] No such file or directory
- (转载) Java IO 之 FileReader 读取文件打印在控制台(处理汉字乱码bug)
- File.io读取文件(一)
- Java IO 之 FileReader 读取文件打印在控制台(处理汉字乱码bug)
- File.io读取文件(五)
- 初学Java,IO之使用FileInputStream和FileReader读取文件(四十一)
- IO操作-随机读取文件RandomAccessFile
- 读取文件的例子 IO
- FileGetcontents 读取远程文件
- FileReader读取文件内容!
- 转载:C# IO读取文件问题:正由另一进程使用