java IO學習筆記------(4)對象流&打印流&文件分割(隨機流)&文件的合併(序列流)

java IO學習筆記------對象流&打印流&文件分割(隨機流)&文件的合併(序列流)

對象流-ObjectOutputStream&ObjectInputStream

       序列化               反序列化
   ObjectOutputStream    ObjectInputStream

先寫出後讀取(先序列化在反序列化)

不僅能操作原始數據(如數據流)還能操作對象

不是所有的對象都能序列化(Serializable)

注意:讀寫的順序要一致

對象流練習

package Io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * 對象流
 * 1.寫出後讀取
 * 2.讀取的順序與寫出保持一致
 * 3.不是所有的對象都可以序列化 Serializable
 * 
 * ObjectOutputStream
 * ObiectInputStream
 * @author 賭徒
 *
 */
public class ObjectT8 {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		//寫出 --序列化
		
		ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("data.txt")));//文件流需要關閉
		//操作數據類型+數據
		oos.writeUTF("張三");
		oos.writeInt(18);
		oos.writeBoolean(false);
		oos.writeChar('a');
		//對象
		oos.writeObject("李四");
		oos.writeObject(new Date());
		Employee emp=new Employee("王二",666);
		oos.writeObject(emp);
		oos.flush();
		oos.close();
		//讀取 -->反序列化
		ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream("data.txt")));
		//順序與寫出一致
		
		String name= ois.readUTF(); 
		int age = ois.readInt();
		boolean flag = ois.readBoolean();
		char ch = ois.readChar();
		System.out.println(name);
		System.out.println(age);
		System.out.println(flag);
		System.out.println(ch);
		//對象的數據還原  
		Object str = ois.readObject();
		Object date = ois.readObject();
		Object employee = ois.readObject();
		if(str instanceof String) {
			String strObj = (String) str;
			System.out.println(strObj);
		}
		if(date instanceof Date) {
			Date dateObj = (Date) date;
			System.out.println(dateObj);
		}
		if(employee instanceof Employee) {
			Employee empObj = (Employee) employee;
			System.out.println(empObj.getName()+"-->"+empObj.getSalary());
		}
	}

}
//封裝數據
class Employee implements java.io.Serializable{
	private transient String name; //該數據不需要序列化
	private double salary;
	public Employee() {
	}
	public Employee(String name, double salary) {
		this.name = name;
		this.salary = salary;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
}

打印流-PrintStream

構造方法:
    PrintStream(OutputStream out,true)可自動刷新

新增方法:
     append()
     println()

打印流練習

package Io;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

/**打印流
 * PrintStream
 * @author 賭徒
 *
 */
public class PrintT9 {

	public static void main(String[] args) throws FileNotFoundException {
		//打印流System.out
		PrintStream ps=System.out;
		ps.println("打印流");
		ps.println(true);
		
		
		ps=new PrintStream(new BufferedOutputStream(new FileOutputStream("printtest.txt")),true);//內部自帶刷新
		ps.println("打印流");
		ps.println(true);
		
		//重定向輸出端
		System.setOut(ps);
		System.out.println("123");
	}

}

文件分割(隨機流)-文件的合併(序列流)RandomAccessFile&SequenceInputStream

注意點: 支持讀和寫 有模式參數 r(讀) rw(寫)

RandomAccessFile(new File(),模式)

seek(long pos); 隨機讀取

代碼測例:

package Io;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomTest2 {

	public static void main(String[] args) throws IOException {
		RandomAccessFile raf=new RandomAccessFile(new File("src/io/ex1.java"), "r");
		//分多少塊
		File srcFile=new File("src/io/ex1.java");
		//總長度
		long len=srcFile.length();
		//每塊大小
		int blockSize=1024;
		//塊數:多少塊
		int size = (int) Math.ceil(len*1.0/blockSize);
		System.out.println(size);
		//起始位置和實際大小
		int beginpos=0;
		int actualSize=(int) (blockSize>len?len:blockSize);
		
		for (int i = 0; i < size; i++) {
			beginpos = i*blockSize;
			if (i==size-1) {//最後一塊 
				
				actualSize=(int) len;
			}else {
				actualSize = blockSize;
				len-=actualSize;
			}
			System.out.println(i+"-->"+beginpos+"-->"+actualSize);
			split(size, beginpos, actualSize);
		}
		
		raf.close();

	}
	//指定起始位置,讀取下面所有內容
	public static void t1() throws IOException {
		RandomAccessFile raf=new RandomAccessFile(new File("src/io/ex1.java"), "r");
		//隨機讀取
		raf.seek(2);
		//讀取
		//操作
		byte[] flush=new byte[1024];//緩衝數組
		int len=-1;//接收長度
		while((len=raf.read(flush))!=-1) {
			System.out.println(new String(flush,0,len));
		}

		raf.close();
	}
	//指定第i塊的起始位置和實際長度
	public static void split(int i,int beginpos,int actualSize) throws IOException {

		RandomAccessFile raf=new RandomAccessFile(new File("src/io/ex1.java"), "r");
		
		//隨機讀取
		raf.seek(beginpos);
		//讀取
		//操作
		byte[] flush=new byte[1024];//緩衝數組
		int len=-1;//接收長度
		while((len=raf.read(flush))!=-1) {
			
			if (actualSize>len) {//獲取本次讀取所有內容
				System.out.println(new String(flush,0,len));
				actualSize-=len;
			}else {
				System.out.println(new String(flush,0,actualSize));
			    break;
			}
		}

		raf.close();

	
		
	}
	//分塊思想:指定起始位置和讀取長度
	public static void t2() throws IOException {

		RandomAccessFile raf=new RandomAccessFile(new File("src/io/ex1.java"), "r");
		//起始位置
		int beginpos=2;
		//讀取長度
		int actualSize=1026;
		//隨機讀取
		raf.seek(beginpos);
		//讀取
		//操作
		byte[] flush=new byte[1024];//緩衝數組
		int len=-1;//接收長度
		while((len=raf.read(flush))!=-1) {
			
			if (actualSize>len) {//獲取本次讀取所有內容
				System.out.println(new String(flush,0,len));
				actualSize-=len;
			}else {
				System.out.println(new String(flush,0,actualSize));
			    break;
			}
		}
	
		
		raf.close();

	
		
	}

}

綜合練習:

package Io;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomTest2 {

	public static void main(String[] args) throws IOException {
		RandomAccessFile raf=new RandomAccessFile(new File("src/io/ex1.java"), "r");
		//分多少塊
		File srcFile=new File("src/io/ex1.java");
		//總長度
		long len=srcFile.length();
		//每塊大小
		int blockSize=1024;
		//塊數:多少塊
		int size = (int) Math.ceil(len*1.0/blockSize);
		System.out.println(size);
		//起始位置和實際大小
		int beginpos=0;
		int actualSize=(int) (blockSize>len?len:blockSize);
		
		for (int i = 0; i < size; i++) {
			beginpos = i*blockSize;
			if (i==size-1) {//最後一塊 
				
				actualSize=(int) len;
			}else {
				actualSize = blockSize;
				len-=actualSize;
			}
			System.out.println(i+"-->"+beginpos+"-->"+actualSize);
			split(size, beginpos, actualSize);
		}
		
		raf.close();

	}
	
	//指定第i塊的起始位置和實際長度
	public static void split(int i,int beginpos,int actualSize) throws IOException {

		RandomAccessFile raf=new RandomAccessFile(new File("src/io/ex1.java"), "r");
		
		//隨機讀取
		raf.seek(beginpos);
		//讀取
		//操作
		byte[] flush=new byte[1024];//緩衝數組
		int len=-1;//接收長度
		while((len=raf.read(flush))!=-1) {
			
			if (actualSize>len) {//獲取本次讀取所有內容
				raf1.write(flush,0,len);
				actualSize-=len;
			}else {
				raf1.write(flush,0,actualSize);
			    break;
			}
		}

		raf.close();
	}
	

}

綜合練習:

package reIo;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class SplitFile {
	//源頭
	private File src;
	//目的地(文件夾)
	private String destDir;
	//所有分割後的文件存儲路徑
	private List<String> destPaths;
	//每塊大小
	private int blockSize;
	//塊數: 多少塊
	private int size;
	
	public SplitFile(String srcPath,String destDir) {
		this(srcPath,destDir,1024);
	}
	public SplitFile(String srcPath,String destDir,int blockSize) {
		this.src =new File(srcPath);
		this.destDir =destDir;
		this.blockSize =blockSize;
		this.destPaths =new ArrayList<String>();
		
		//初始化
		 init();
	}
	//初始化
	private void init() {
		//總長度
		long len = this.src.length();		
		//塊數: 多少塊
		this.size =(int) Math.ceil(len*1.0/blockSize);
		//路徑
		for(int i=0;i<size;i++) {
			this.destPaths.add(this.destDir +"/"+i+"-"+this.src.getName());
		}
	}
	/**
	 * 分割
	 * 1、計算每一塊的起始位置及大小
	 * 2、分割
	 */
	public void split() throws IOException {
		//總長度
		long len = src.length();		
		//起始位置和實際大小
		int beginPos = 0;
		int actualSize = (int)(blockSize>len?len:blockSize); 
		for(int i=0;i<size;i++) {
			beginPos = i*blockSize;
			if(i==size-1) { //最後一塊
				actualSize = (int)len;
			}else {
				actualSize = blockSize;
				len -=actualSize; //剩餘量
			}
			splitDetail(i,beginPos,actualSize);
		}
	}	
	/**
	 * 指定第i塊的起始位置 和實際長度
	 */
	private  void splitDetail(int i,int beginPos,int actualSize ) throws IOException {
		RandomAccessFile raf =new RandomAccessFile(this.src,"r");
		RandomAccessFile raf2 =new RandomAccessFile(this.destPaths.get(i),"rw");
		//隨機讀取 
		raf.seek(beginPos);
		//讀取
		//3、操作 (分段讀取)
		byte[] flush = new byte[1024]; //緩衝容器
		int len = -1; //接收長度
		while((len=raf.read(flush))!=-1) {			
			if(actualSize>len) { //獲取本次讀取的所有內容
				raf2.write(flush, 0, len);
				actualSize -=len;
			}else { 
				raf2.write(flush, 0, actualSize);
				break;
			}
		}			
		raf2.close();
		raf.close();
	}	
	/**
	 * 文件的合併
	 */
	public void merge(String destPath) throws IOException {
		//輸出流
		OutputStream os =new BufferedOutputStream( new FileOutputStream(destPath,true));	
		Vector<InputStream> vi=new Vector<InputStream>();
		SequenceInputStream sis =null;
		//輸入流
		for(int i=0;i<destPaths.size();i++) {
			vi.add(new BufferedInputStream(new FileInputStream(destPaths.get(i))));											
		}
		sis =new SequenceInputStream(vi.elements());
		//拷貝
		//3、操作 (分段讀取)
		byte[] flush = new byte[1024]; //緩衝容器
		int len = -1; //接收長度
		while((len=sis.read(flush))!=-1) {
			os.write(flush,0,len); //分段寫出
		}			
		os.flush();	
		sis.close();
		os.close();
	}
	public static void main(String[] args) throws IOException {
		SplitFile sf = new SplitFile("SplitFile.java","dest") ;
		sf.split();
		sf.merge("abc.java");
	}
}

其他章節:

java IO學習筆記------(1)文件字節流
java IO學習筆記------(2)文件字符流&字節數組流
java IO學習筆記------(3)字節緩衝流&字符緩衝流&轉換流&數據流

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章