21天精通java基礎之Day15IO流(二)

Day15IO流(二):


  使用FileReader、FileWriter可以實現文本文件的複製。

  對於非文本文件(視頻文件、音頻文件、圖片),只能使用字節流。

實例:

@Test
	public void testFileReaderWriter1(){
		//1.輸入流對應的文件src一定要存在,否則跑異常。輸出流對應的文件dest可以不存在,執行過程會自動創建。
		File file1 = new File("hello.txt");
		File file2 = new File("hello3.txt");
		FileReader fr = null;
		FileWriter fw = null;
		
		try{
			fr = new FileReader(file1);
			fw = new FileWriter(file2);
			
			char[] c = new char[20];
			int len;
			while((len = fr.read(c)) != -1)
			{
				fw.write(c, 0, len);
			}
		}
			catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}finally{
				if(fw != null){
					try {
						fw.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
				if(fr != null){
					try {
						fr.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
					
		}
		

    緩衝流:複製圖片

 BufferedInputStream,BufferedOutputStream

 實例:

	@Test
	public void TestBuffered1(){
	
		//1.提供讀入寫出的文件
		File file1 = new File("1.jpg");
		File file2 = new File("2.jpg");
		
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			//2.創建相應的節點流
			FileInputStream fis = new FileInputStream(file1);
			FileOutputStream fos = new FileOutputStream(file2);
			//3.將創建的節點流的對象作爲形參傳遞給緩衝流的構造器中
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			//4.文件複製
			byte[] b = new byte[1024];
			int len;
			while((len = bis.read(b)) != -1){
				bos.write(b,0,len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//關閉流
		if(bos != null){
			try {
				bos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}if(bis != null){
			try {
				bis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		}
	}

緩衝流實例:

BufferedReader、BufferedWriter

	@Test
	public void testBuffered2(){
		File file1 = new File("hello.txt");
		File file2 = new File("hello2.txt");
		
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			FileReader fr = new FileReader(file1);
			FileWriter fw = new FileWriter(file2);
			
			br = new BufferedReader(fr);
			bw = new BufferedWriter(fw);
			
			String str ;
			while((str = br.readLine()) != null){
				bw.write(str + "\n");
				//bw.newLine();
				bw.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			}			
		}
		
	}

轉換流:InputStreamReader OutputStreamWriter

編碼:字符串---》字節數組

解碼:字節數組---》字符串


標準的輸入輸出流:

  標準的輸入流:System.in

  標準的輸出流:System.out

題目:

  從鍵盤輸入字符串,要求將讀取到的整行字符串轉換成大寫輸出,然後繼續進行輸入操作,直到當輸入“e”,或者輸入“exit”時,退出程序。

@Test
	public void test(){
		BufferedReader br = null;
		try {
			InputStream  is = System.in;
			InputStreamReader isr = new InputStreamReader(is);
			br = new BufferedReader(isr);
			
			while(true){
				System.out.println("請輸入字符串:");
				String str;
				str = br.readLine();
				if(str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")){
					System.out.println("結束!");
					break;
				}
				String upper = str.toUpperCase();
				System.out.println(upper);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			
		}

  數據流:

  用來處理基本的數據類型、String、字節數組的數據:DataInputStream DataOutputStream.

    RandomAccessFile:支持隨機訪問

  1.既可以充當一個輸入流,又可以充當一個輸出流。

  2.支持從文件的開頭讀取、寫入。

  3.支持從任意位置的讀取、寫入(插入)。

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