IO流

IO流按使用方式可以簡單的分爲4種:

1.字符字節流

2.緩衝字符字節流

3.轉換流

4.數據流

 

字節流例子:

 運行前

運行後

 

字符流例子:

運行前

運行後

 

緩衝流先將數據讀入到緩衝區中,目的是提高程序讀出的性能。緩衝流也分爲字節緩衝流和字符緩衝流。

示例:

運行後:

 

 

轉換流,操作字符文件時有可能因爲編碼不一樣產生亂碼。字節流向字符流轉換:

 運行後:

 

 

數據流:方便將程序中數據存入文件。

運行後:

 

 

貼上面的測試代碼

public static void main(String[] args) {
		/*
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		//字節流字節文件。視頻,音頻,圖片,文檔。
		try {
			//輸入流
			fileInputStream = new FileInputStream("E:\\Document\\temp\\甜點.jpg");
			//輸出流
			fileOutputStream = new FileOutputStream("E:\\Document\\temp\\newFood.jpg");
			//輸入輸出流小車,加速讀寫。
			byte []car = new byte[1024];
			
			int length = 0;
			while((length=fileInputStream.read(car))!=-1) {
				fileOutputStream.write(car,0,length);
			}
			fileOutputStream.flush();
			System.out.println("完畢");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(fileInputStream!=null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		*/
		/*
		//字符流:操作字符流文件(記事本)	
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		
		try {
			fileReader = new FileReader("E:\\Document\\temp\\test.txt");//輸入流
			fileWriter = new FileWriter("E:\\Document\\temp\\new.txt");//輸出流
			//小車
			char []car = new char[1024];
			
			int length = 0;
			while((length=fileReader.read(car))!=-1) {
				fileWriter.write(car,0,length);
			}
			fileWriter.flush();//清空緩存
			System.out.println("完畢");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(fileReader!=null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		*/
		/*
		//緩衝流
		BufferedInputStream bufferedInputStream = null;
		BufferedOutputStream bufferedOutputStream = null;
		try {
			
			bufferedInputStream = new  BufferedInputStream(new FileInputStream("E:\\Document\\temp\\甜點.jpg"));
			bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("E:\\Document\\temp\\不好吃.jpg"));
			
			byte []car = new byte[1024];
			
			int length = 0;
			while((length=bufferedInputStream.read(car))!=-1) {
				bufferedOutputStream.write(car,0,length);
			}
			bufferedOutputStream.flush();
			System.out.println("完畢");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(bufferedInputStream!=null) {
				try {
					bufferedInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		*/
		/*
		//轉換流,操作字符文件 時有可能因爲編碼不一樣產生亂嗎,字節流向字符流轉換
		InputStreamReader inputStreamReader = null;
		OutputStreamWriter outputStreamWriter = null;
		try {
			inputStreamReader = new InputStreamReader(new FileInputStream("E:\\Document\\temp\\test.txt"), "UTF-8");
			outputStreamWriter = new OutputStreamWriter(new FileOutputStream("E:\\Document\\temp\\真香.txt"), "UTF-8");
			char []car = new char[1024];
			
			int length = 0;
			while((length=inputStreamReader.read(car))!=-1) {
				outputStreamWriter.write(car,0,length);
			}
			outputStreamWriter.flush();
			System.out.println("完畢");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(inputStreamReader!=null) {
				try {
					inputStreamReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}	
		*/
				
		double price = 7.8;
		//數據流:方便將程序中數據存入文件。
		try {
			DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("E:\\Document\\temp\\data.temp"));
			dataOutputStream.writeDouble(price);//輸出到硬盤
			dataOutputStream.flush();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		try {
			DataInputStream dataInputStream = new DataInputStream(new FileInputStream("E:\\Document\\temp\\data.temp"));
			double p = dataInputStream.readDouble();//讀取剛纔輸出的數據
			System.out.println(p);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

 

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