Java复习之知识点整理(十二)----字节流、字符流、转换流、缓冲流、skip、归档、解归档

一、IO流
--------------------------------------------------------------
1.处理设备之间的数据传输

二、字节流(InputStream,OutputStream)
-------------------------------------------------------------
1.InputStream和OutputStream 都是抽象类,使用的话,请使用其具体子类 FileInputStream 和 FileOutputStream

@Test
	//测试字节输入流
	public void tsFileInputStream() throws Exception
	{
		//long start = System.currentTimeMillis();
		FileInputStream fis = new FileInputStream("D:\\a.txt");
		
		byte [] buffer = new byte [1022];
		
		int len = 0;
		
		while((len = fis.read(buffer)) != -1)
		{
			String s = new String(buffer,0,len,"utf-8");
			System.out.println(s);
		}

		fis.close();
		
		//System.out.println(System.currentTimeMillis() - start);
	
	}



	@Test
	//测试字节输出流
	public void tsFileOutputStream() throws Exception
	{
		FileOutputStream fos = new FileOutputStream("D:\\a.txt");
		
		//long start = System.currentTimeMillis();
		
		byte [] buffer = new byte [1023];
		Arrays.fill(buffer, (byte) 97);
		
		fos.write(buffer);
		
		fos.write("\ud585\u4e2d".getBytes("utf-8"));
		
		fos.close();
		
//		for(int i = 0 ; i < 1024 * 1024 ; i ++)
//		{
//			fos.write(buffer);
//		}
	
		fos.close();
		
		//System.out.println(System.currentTimeMillis() - start);
	}


@Test
	//测试拷贝文件:使用字节输入输出流
	public void tsCopyFile() throws Exception
	{
		//创建文件输入流
		FileInputStream fis = new FileInputStream("d:\\d.png");
		//创建文件输出流
		FileOutputStream fos = new FileOutputStream("d:\\1.jpg");
		//缓冲区(搬运工)
		byte [] buffer = new byte[1024];
		//记录位置
		int len = 0;
		//copy...		
		while((len = fis.read(buffer)) != -1)
		{
			fos.write(buffer,0,len);
		}
		//关闭流
		fis.close();
		fos.close();
		System.out.println("复制完成!");
	}




三、字符流(Reader,Writer)
----------------------------------------------------
1.Reader 和 Writer 都是抽象类,使用的话,请使用其具体子类 FileReader和FileWriter
2.会以平台默认的编码就行编码和解码操作,如果需要指定编码,请使用转换流 InputStreamReader 和 OutputStreamWriter

@Test
	//测试字符输入流
	public void tsFileReader() throws Exception
	{
		FileReader reader = new FileReader("D:\\a.txt");
		char [] cbuf = new char [1024];
		int len = 0;
		while((len = reader.read(cbuf)) != -1)
		{
			System.out.println(new String(cbuf,0,len));
		}
		reader.close();
	}
 
 
//标准的流操作,以字符输入流为例
	@Test
	public void tsStandardStream()
	{
		FileReader fr = null;
		try {
			
			fr = new FileReader("D:\\a.txt");
			char [] cbuf = new char[1024];
			int len = 0;
			while((len = fr.read(cbuf)) != -1)
			{
				System.out.println(new String(cbuf,0,len));
			}						
		} 
		catch (Exception e) {
			
		}
		finally
		{
			if(fr != null)
			{
				try {
					fr.close();
				} catch (IOException e) {					
					e.printStackTrace();
				}
			}
		}
	}	


@Test
	//测试字符输出流
	public void tsFileWriter()
	{
		FileWriter w = null;
		try {
			w = new FileWriter("D:\\ts.txt", false);
			for (int i = 0; i < 100; i++) {
				
				w.write("tom" + i + ",");
			}	
			
			w.append("sss");
		} 
		catch (Exception e) {
			
		}
		finally
		{
			if(w != null)
			{
				try {
					w.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}



四、转换流 (InputStreamReader / OutputStreamWriter)
--------------------------------------------------------------------------------
1.以谁结尾就是什么流。所以转换流本质上是字符流,是字节转成字符的流。
2.转换流在构造的时候,可以指定 需要转成字符流的字节流和转换所需要的编码。

@Test
	//测试转换流:InputStreamReader
	public void tsInputStreamReader() throws Exception
	{
		//字符流
		InputStream is = new FileInputStream("D:\\a.txt");
		//指定编码转换流
		InputStreamReader isr = new InputStreamReader(is,"utf-8");
		
		int len = 0;
		char [] cbuf = new char [ 1024];
		while((len = isr.read(cbuf)) != -1)
		{
			System.out.println(new String(cbuf,0,len));
		}
		isr.close();
		is.close();
		
	}





五、缓冲流(BufferedInputStream / BufferedOutputStream / BufferedReader / BufferedWriter)
-----------------------------------------------------------------------------
@Test
	//测试字符缓冲输入流
	public void tsBufferedReader()
	{		
		InputStream is = null; 
		Reader r = null;		
		BufferedReader br = null;
		
		try {
			is = new FileInputStream("D:\\ts.txt");
			r = new InputStreamReader(is , "utf-8");;
			br = new BufferedReader(r);
			String line = "";
			while((line = br.readLine()) != null)
			{
				System.out.println(line);
			}		
		} 
		catch (Exception e) {

		}
		finally
		{
			try {
				
				if( br != null)
				{
					br.close();
				}
				if(r != null)
				{
					r.close();
				}
				if(is != null)
				{
					is.close();
				}
				
			} catch (Exception e2) {
				
			}			
		}
		
	}




六、skip()
----------------------------------------------------------------
@Test
	//测试流的跳跃功能
	public void tsSkipStream() throws Exception
	{
		FileReader fr = new FileReader("D:\\ts.txt");
		System.out.println((char)fr.read());
		fr.skip(1);
		System.out.println((char)fr.read());
		fr.close();
	}
	




七、实现文件的归档和解归档以及读取归档文件的所有文件名
------------------------------------------------------------------------------------------------

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;

/**
 * @author Mr.GE
 * @date 创建时间:2018-7-3 下午4:29:54
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class ArchiveXar {

	static int UnArchiveFlag = 0;
	
	@Test
	public void ts01()
	{		
		//archiveXar(new String [] {"a.txt", "b.jpg", "c.mp3"}, "D:\\", "E:\\a.xar");
		//unArchiveXar("E:\\a.xar","E:\\");
	}
	
	//归档
	public void archiveXar(String[] names, String srcPath, String desPath) {
		
		List<byte[]> list = new ArrayList<byte[]>();
				
		for (int i = 0; i < names.length; i++) {
			// 路径
			String path = names[i];
			
			// 路径字节
			byte[] pathBs = path.getBytes();
			// 路径长度的字节数组
			byte[] pathLenBs = intToBytes(pathBs.length);
			
			//文件字节
			byte[] fileBs = getFileBytes(srcPath + "\\" + path);
			System.out.println(fileBs.length);
			//文件长度字节
			byte[] fileLenBs = intToBytes(fileBs.length);
						
			list.add(pathLenBs);
			list.add(pathBs);
			list.add(fileLenBs);
			list.add(fileBs);			
		}		
		writeBytesToXarFile(list, desPath);		
	}
	
	//解档
	public void unArchiveXar(String xarPath , String des)
	{	
		byte [] bs = getFileBytes(xarPath);
		List<String> name = unXar(bs,true,des);	
		for (int i = 0; i < name.size(); i++) {
			System.out.println(name.get(i));
		}
	}
	
	
	public List<String> unXar(byte [] bs , boolean isBuildFile , String buildPath)
	{
		UnArchiveFlag = 0;
		List<String> nameList = new ArrayList<String>();
		while(UnArchiveFlag < bs.length)
		{
			//名称数量字节数组
			byte [] bs01 = Arrays.copyOfRange(bs, UnArchiveFlag, 4 + UnArchiveFlag);
			UnArchiveFlag += 4;
			int len01 = bytesToInt(bs01);
			
			//文件名称
			byte [] bs02 = Arrays.copyOfRange(bs, UnArchiveFlag, UnArchiveFlag + len01);
			String name = new String(bs02);
			nameList.add(name);
			UnArchiveFlag += len01;
			
			//文件内容数量
			byte [] bs03 = Arrays.copyOfRange(bs, UnArchiveFlag, 4 + UnArchiveFlag);
			UnArchiveFlag += 4;
			int len02 = bytesToInt(bs03);
			
			
			//创建文件
			if(isBuildFile)
			{				
				byte [] bs04 = Arrays.copyOfRange(bs, UnArchiveFlag , UnArchiveFlag + len02);
				writeBytes(bs04, buildPath + "\\" + name);						
			}	
			UnArchiveFlag += len02;
		}
		return nameList;
		
	}
	

	public byte[] intToBytes(int i) {
		
		byte b0 = (byte)(i >> 24);
		byte b1 = (byte)(i >> 16);
		byte b2 = (byte)(i >> 8);
		byte b3 = (byte)i;	
		return new byte [] {b0,b1,b2,b3};
	}
	
	public int bytesToInt(byte[] bs)
	{
		int i0 = (int)(bs[0] << 24 );
		int i1 = (int)(bs[1] << 16 & 0x00ff0000);
		int i2 = (int)(bs[2] << 8 & 0x0000ff00);
		int i3 = (int)(bs[3] & 0x000000ff);		
		return (i0 | i1 | i2 | i3);	
	}

	// 获取文件的字节数组
	public byte[] getFileBytes(String name) {
		ArrayList<Byte> list = new ArrayList<Byte>();
		FileInputStream fis = null;
		BufferedInputStream bis = null;

		try {

			fis = new FileInputStream(name);
			bis = new BufferedInputStream(fis);

			byte[] bs = new byte[1024 * 8];
			int len = 0;
			try {
				while ((len = bis.read(bs)) != -1) {
					for (int i = 0; i < len; i++) {
						list.add(bs[i]);
					}
				}
			} catch (IOException e) {

				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
				if (bis != null) {
					bis.close();
				}

			} catch (Exception e2) {

			}
		}
		
		byte [] bs = new byte[list.size()];
		for (int i = 0; i < bs.length; i++) {
			bs[i] = list.get(i);
		}
		return bs;
	}

	//将字节数组的集合写入文件
	public void writeBytesToXarFile(List<byte[]> list, String des) {
		OutputStream ops = null;
		BufferedOutputStream bos = null;
		try {

			ops = new FileOutputStream(des);
			bos = new BufferedOutputStream(ops);
			
			for(int i = 0 ; i < list.size(); i++)
			{
				byte[] bs = list.get(i);
				bos.write(bs);
				bos.flush();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			try {
				if(ops != null)
				{
					ops.close();
				}
				
				if(bos != null)
				{
					bos.close();
				}
				
			} catch (Exception e2) {
				// TODO: handle exception
			}
			
		}

	}

	// 写字节
	public void writeBytes(byte[] bs, String des) {
		
		OutputStream ops = null;
		BufferedOutputStream bos = null;
		try {

			ops = new FileOutputStream(des);
			bos = new BufferedOutputStream(ops);		
			bos.write(bs);
			bos.flush();

		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			try {
				if(ops != null)
				{
					ops.close();
				}
				
				if(bos != null)
				{
					bos.close();
				}
				
			} catch (Exception e2) {
				// TODO: handle exception
			}
			
		}
	}
}



猜你喜欢

转载自blog.csdn.net/xcvbxv01/article/details/80903598