序列化和Io其他流

DataOutputStream和DataInputStream
都属于数据流:可以针对Java基本数据类型的数据进行读写操作


Read方法读数据  在输入流对象里  read后面跟类型eg:
readByte();readInt()
write方法写数据 在输出流对象写  write后也跟数据类型
eg: writeByte(100); writeChar('A') 


ByteArrayInputStream和ByteArrayOutStream  
内存操作流:针对内存的数据进行操作,程序结束,内存数据消失.
针对小文件进行操作.
ByteArrayOutputStream方法:
public byte[] toByteArray()创建一个新分配的 byte 数组
void reset():重置内存操作输出流
public ByteArrayOutputStream():创建默认的缓冲区大小的内存操作输出流
并没有具体的关闭流对象,所以此流对象可以不关闭
ByteArrayInputStream:内存操作输入流方法:
public ByteArrayInputStream(byte[] buf):参数数一个字节数组(缓冲数组)
 
byte[] buffer = baos.toByteArray() ;
ByteArrayInputStream bais = new ByteArrayInputStream(buffer) ;
不用关闭流,无具体对象


字节打印流:PrintStream
字符打印流:PrintWriter
打印流特点:
1.在复制文件,打印流不能操作数据源,只能操作目的地(输出数据)
2.有自动刷新功能
3.可以直接对文本文件进行操作
构造方法: PrintWriter pr= new PrintWriter("first.txt") ;
写数据:pr.write(”内容”);   pr.flush()刷新   
将当前项目下的一个文件中的内容复制到当前项目下另一个文件中
1.
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new FileReader("first.txt")) ;
BufferedWriter bw = new BufferedWriter(new FileWriter("six.txt")) ;
//使用BufferedReader中的特有功能读写操作
String line = null ;
while((line=br.readLine())!=null){
bw.write(line) ;
bw.newLine() ;
bw.flush() ;
}
bw.close() ;
br.close()


2. BufferedReader br = new BufferedReader(new FileReader("first.txt")) ;
PrintWriter pw = new PrintWriter(new FileWriter("six.txt"), true) ;
String line = null ;
while((line=br.readLine())!=null){
pw.println(line) ;
}
pw.close() ;
br.close() ;
}




PrintWriter:有自动刷新功能:
public PrintWriter(Writer out,boolean autoFlush)  第二个参数为true时,
启动自动刷新:
加入新的功能:prinrln():换行 
Println()相当于:        
pr.write(“”);  pr.newLine(); pr.flush();
方法:
public void println(String x):打印字符串,并且终止该行


键盘录入的两种方式:
1. Scanner类
Scanner sc=new Scanner(System.in)
//创建一个字节输入流对象
InputStream is = System.in ; //标准输入流
一次读取一行使用BufferReader的readLine();
2. 使用IO流的形式进行录入数据
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


int a = Integer.parseInt(s) 
引用类型.parselnt()   转换数据


标准输入和输出流
System类中有两个字段:
in:----->InputStream is = System.in ;
out----->PrintStream ps = System.out ;
//字节打印流
PrintStream ps = System.out ;




使用BufferedReader完成了录入数据:
使该流封装字符转换输入流,然后使用字符转换输入流封装字节流
newLine()是BufferedWriter类中的方法




当开发中使用 try…catch…
try {
bw = new BufferedWriter(new OutputStreamWriter(
System.out));
bw.write("JAVA") ;
bw.newLine()  ;
bw.flush() ;
} catch (IOException e) {
e.printStackTrace();
finally{
if( bw!=null){  //开发中需要进行非空判断
try {
bw.close() ;
} catch (IOException e) {
e.printStackTrace();
}
}


RandomAccessFile:随机访问流:此类的实例支持对随机访问文件的读取和写入


RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw") ;
第一个参数指定路径,第二个参数指定模式
rw 可读可写  
    
   方法: //public long getFilePointer():返回此文件中的当前偏移量.
   
   SequenceInputStream:合并流(读数据):表示其他输入流的逻辑串联
   
将两个文件合并到一个文件里面


public class FileDemo3 {
public static void main(String[] args) throws IOException {
//封装两个文件
FileInputStream s1 = new FileInputStream(
"four.txt");
FileInputStream s2 = new FileInputStream("five.txt");
     //创建合并流对象
SequenceInputStream stream = new SequenceInputStream(s1,s2);
BufferedOutputStream bfo = new BufferedOutputStream(
new FileOutputStream("eleven.txt"));
//一次遍历一个字节数组
byte[] bys = new byte[1024] ;
int len = 0 ;
while((len = stream.read(bys))!=-1){
//写数据
bfo.write(bys, 0, len) ;
bfo.flush() ;
}
stream.close();
bfo.close();
}
}


SequenceInputStream的另一种构造方法:复制多个文件


public SequenceInputStream(Enumeration<? extends InputStream> e)


Vector集合中的特有功能:




public static void main(String[] args) throws IOException {
//创建一个Vector集合,泛型数据类型为<Input>
Vector<InputStream> v=new Vector<InputStream>();
InputStream s1 = new FileInputStream("first.txt") ;
InputStream s2 = new FileInputStream("second.txt") ;
InputStream s3 = new FileInputStream("five.txt") ;  
v.add(s1);
v.add(s2);
v.add(s3);
//调用特有功能 public Enumeration<E> elements()
Enumeration<InputStream> en = v.elements() ;
//创建合并流对象
SequenceInputStream sis = new SequenceInputStream(en) ;
//封装目的地
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("threesum.txt"));
byte[] bys = new byte[1024] ;
int len = 0 ;
while((len=sis.read(bys))!=-1){
//写数据
bos.write(bys, 0, len) ;
bos.flush() ;
}
bos.close() ;
sis.close() ;
}
}




序列化流: 将对象像流的方式或者网络传输中的数据写数据
流数据:ObjectOutputStream
反序列化: 将流数据或者网络传输中的流数据读取出来
流数据---->还原成对象: ObjectInputStream


ObjectOutputStream中的成员方法:
public final void writeObject(Object obj):将obj对象写入到当前的序列化流中
ObjectInputStream中的成员方法:
public final Object readObject():从当前反序列化流中读取一个对象
反序列化: 将当前流数据--->对象
Eg:反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"oos.txt"));
//使用反序列化:将流数据--->对象
Object obj = ois.readObject() ;


//创建一个序列化流对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"oos.txt"));
//写数据
writeObject(Object obj)


Serializeable:序列化接口
类通过实现 java.io.Serializable 接口以启用其序列化功能
未实现此接口的类无法使其任何状态序列化或反序列化
在API中
序列化或者是反序列化!如果一个接口中没有字段,没有构造方法,没有成员方法,叫做标记接口


如果一个类实现了标记接口:Serializable,那么对应的该类加载的时候,会产生一个ID
序列化和反序列化版本号不一致,会出现InvalidClassException异常


解决这个异常两种方案:
1. 要么改动当前某个类中的数据之后,然后重新序列化和反序列化
//不符合实际
在实际开发中,可能直接读数据:将流数据--->还原成对象
2. 发现黄色警告线,点击它:生成一个固定ID


序列化和反序列化:
1)将对象--->流数据或者流数据--->对象,该对象所在的类要实现一个标记接口:serializable 多线程有一个关键字:同步机制(synchronized)
2)序列化和反序列化生产的版本Id不一致的时候,会出现异常,所以使用生产随机ID或者固定ID解决
3)transient:修饰的变量不会被序列化...



Properties:属性集合类, 该类继承自Hashtable<K,V>,该类属于Map集合
Properties 类表示了一个持久的属性集。
Properties 可保存在流中或从流中加载, 属性列表中每个键及其对应值都是一个字符串
在它用作配置文件去使用
构造方法:
public Properties():创建一个空属性列表
eg: Properties<String,String> prop = new Properties<String,String>() ;
添加方法等和map集合一样.


Properties有自己的遍历和添加元素的功能
功能:
public Object setProperty(String key,String value)
public Set<String> stringPropertyNames():
获取当前属性列表中所有的键的集合,键值都是String类型
public String getProperty(String key)
用指定的键在此属性列表中搜索属性值

 

 

Properties 可保存在流中或从流中加载。
方法: public void load(Reader reader)
将文件中的数据加载到属性集合中
方法: public void store(Writer writer,String comments)
将属性集合中的数据保存到文件中:,
第二个参数:comments:对当前属性列表 的描述


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值