Java第二十四天学习笔记~IO流(文件切割、对象的序列化、反序列化、编码表等)

文件切割

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
	

public class SplitFileDemo {

	private static final int SIZE = 1024*1024;
	public static void main(String[] args) throws Exception  {
		// TODO Auto-generated method stub
		File file=new File("c:0.jpg");
		splitFile(file);
	}
	public static void splitFile(File file) throws IOException {
		
		//用读取流关联源文件
		FileInputStream fis=new FileInputStream(file);
		//定义一个1M的缓冲区
		byte[] buf=new byte[SIZE];
		
		//创建目的
		FileOutputStream fos=null;
		
		int len=0;
		int count=1;
		
		File dir=new File("c:\\partfiles");
		if(!dir.exists())
			dir.mkdirs();
		
		while((len=fis.read(buf))!=-1) {
			fos=new FileOutputStream(new File(dir,(count++)+".part"));
		}
		fos.close();
		fis.close();
	}

}

文件合并

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

public class MergeFile {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		File dir=new File("c:\\partfiles");
		mergeFile(dir);
	}
	public static void mergeFile(File dir) throws IOException  {
		
		ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();
		for(int x=1;x<=3;x++) {
			al.add(new FileInputStream(x+".part"));
		}
		Enumeration<FileInputStream> en=Collections.enumeration(al);
		SequenceInputStream sis=new SequenceInputStream(en);
		
		FileOutputStream fos=new FileOutputStream(new File(dir,"0.jpg"));
		
		byte[] buf=new byte[1024];
		
		int len=0;
		while((len=sis.read(buf))!=-1) {
			fos.write(buf, 0, len);
		}
		fos.close();
		sis.close();
	}
		
	

}

对象的序列化

ObjiectOutputStream:将对象排队存到硬盘上,被序列化的对象必须实现Serializable接口

对象的反序列化

ObjiectInputStream:把对象从堆内存存到硬盘上

序列化接口Serializable

用于给被序列化的类加入ID号

判断类和对象是否为同一个版本

关键字--translent

暂时的,非瞬态的

非静态数据不想被序列化可以使用这个关键字

RandomAccrssFile

特点:

  1. 该对象既能读又能写
  2. 该对象内部维护了一个byte数组,并通过指针可以操作数组中的元素
  3. 可以通过getFilePointer方法获取指针的位置和通过seek方法设置指针的位置
  4. 其实该对象就是将字节输入流和输出流进行了封装
  5. 该对象的源或者目的只能是文件,通过构造函数就可看出

管道流

PipedInputStream和PipedOutputStream

输入,         输出, 可以直接进行连接通过结合线程使用

input.connect(output );连接使用

基本数据类型操作--DateStream

DateInputStream和DateOutputStream

操作字节数组

(内存)ByteArrayInputStream和 ByteArrayOutputStream

操作字符数组

CharArrayReader和CharArrayWriter

操作字符串

StringReader和StringWriter

编码表

编码-----解码

字符串------》字节数组  :编码

字节数组----》字符串:解码

import java.io.UnsupportedEncodingException;

public class EncodeDemo {

	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
		String str="你好";
		//编码
		byte[] buf=str.getBytes("UTF-8");
		//解码
		String s1=new String(buf,"UTF-8");
		
		System.out.println("s1="+s1);
				
	}
	private static void printBytes(byte[] buf) {
		for(byte b:buf) {
			System.out.println(b+"");
		}
	}

}

猜你喜欢

转载自blog.csdn.net/crazyhulu/article/details/85753282