1. 程式人生 > >Java基礎之IO操作(二)

Java基礎之IO操作(二)

Java基礎之IO操作(二)

一、BufferedInputStream

public static void main(String[] args) {
File src = new File("abc.txt");
//2、選擇流
InputStream  is =null;
try {
	is =new BufferedInputStream(new FileInputStream(src));
	//3、操作 (分段讀取)
	byte[] flush = new byte[1024]; //緩衝容器
	int len = -1; //接收長度
	while((len=is.read(flush))!=-1) {
		//位元組陣列-->字串 (解碼)
		String str = new String(flush,0,len);
		System.out.println(str);
	}		
		
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}finally {
	//4、釋放資源
	try {
		if(null!=is) {
			is.close();
		}
	} catch (IOException e) {
		e.printStackTrace();
	}			
}
}

二、BufferedOutputStream

public static void main(String[] args) {
//1、建立源
File dest = new File("dest.txt");
//2、選擇流
OutputStream os =null;
try {
	os =new BufferedOutputStream( new FileOutputStream(dest));
	//3、操作(寫出)
	String msg ="IO is so easy\r\n";
	byte[] datas =msg.getBytes(); // 字串-->位元組陣列(編碼)
	os.write(datas,0,datas.length);
	os.flush();
}catch(FileNotFoundException e) {		
	e.printStackTrace();
}catch (IOException e) {
	e.printStackTrace();
}finally{
	//4、釋放資源
	try {
		if (null != os) {
			os.close();
		} 
	} catch (Exception e) {
	}
}
}

三、BufferedReader

public static void main(String[] args) {
//1、建立源
File src = new File("abc.txt");
//2、選擇流
BufferedReader  reader =null;
try {
	reader =new BufferedReader(new FileReader(src));
	//3、操作 (分段讀取)
	String line =null;
	while((line=reader.readLine())!=null) {
		//字元陣列-->字串
		System.out.println(line);
	}		
		
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}finally {
	//4、釋放資源
	try {
		if(null!=reader) {
			reader.close();
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
}
}

四、BufferedWriter

public static void main(String[] args) {
//1、建立源
File dest = new File("dest.txt");
//2、選擇流
BufferedWriter writer =null;
try {
	writer = new BufferedWriter(new FileWriter(dest));
	//3、操作(寫出)			
	writer.append("IO is so easy");
	writer.newLine();
	writer.append("尚學堂歡迎你");
	writer.flush();
}catch(FileNotFoundException e) {		
	e.printStackTrace();
}catch (IOException e) {
	e.printStackTrace();
}finally{
	//4、釋放資源
	try {
		if (null != writer) {
			writer.close();
		} 
	} catch (Exception e) {
	}
}
}

五、轉換流

public static void main(String[] args) {
//操作System.in 和System.out,可以指定編碼格式進行轉化
try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	BufferedWriter writer =new BufferedWriter(new OutputStreamWriter(System.out));){
	//迴圈獲取鍵盤的輸入(exit退出),輸出此內容
	String msg ="";
	while(!msg.equals("exit")) {
		msg = reader.readLine(); //迴圈讀取
		writer.write(msg); //迴圈寫出
		writer.newLine();
		writer.flush(); //強制重新整理
	}
	}catch(IOException e) {
		System.out.println("操作異常");
	}	
}

六、資料操作流

public static void main(String[] args) throws IOException {
	//寫出
	ByteArrayOutputStream baos =new ByteArrayOutputStream();
	DataOutputStream dos =new DataOutputStream(new BufferedOutputStream(baos));
	//操作資料型別 +資料
	dos.writeUTF("編碼辛酸淚");
	dos.writeInt(18);
	dos.writeBoolean(false);
	dos.writeChar('a');
	dos.flush();
	byte[] datas =baos.toByteArray();
	System.out.println(datas.length);
	//讀取
	DataInputStream dis =new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
	//順序與寫出一致
	String msg = dis.readUTF(); 
	int age = dis.readInt();
	boolean flag = dis.readBoolean();
	char ch = dis.readChar();
	System.out.println(flag);
}

七、物件操作流

public class ObjectTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//寫出 -->序列化
	ByteArrayOutputStream baos =new ByteArrayOutputStream();
	ObjectOutputStream oos =new ObjectOutputStream(new BufferedOutputStream(baos));
	//操作資料型別 +資料
	oos.writeUTF("編碼辛酸淚");
	oos.writeInt(18);
	oos.writeBoolean(false);
	oos.writeChar('a');
	//物件
	oos.writeObject("誰解其中味");
	oos.writeObject(new Date());
	Employee emp =new Employee("馬雲",400);
	oos.writeObject(emp);
	oos.flush();
	byte[] datas =baos.toByteArray();
	System.out.println(datas.length);
	//讀取 -->反序列化
	ObjectInputStream ois =new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
	//順序與寫出一致
	String msg = ois.readUTF(); 
	int age = ois.readInt();
	boolean flag = ois.readBoolean();
	char ch = ois.readChar();
	System.out.println(flag);
	//物件的資料還原  
	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());
	}
		
}

}
//javabean 封裝資料
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;
	}
	
}