java IO學習筆記------(2)文件字符流&字節數組流

java IO流學習筆記------(2)文件字符流&字節數組流

文件字符流FileReader&FileWriter

FileReader :通過字符的方式讀取文件,僅適合字符文件
FileWriter :通過字節的方式寫出或追加數據到文件中,僅適合字符文件

部分方法同文件字節流(read(),write())

其他方法:
FileWriter:append()寫入
name.append(“你好”);
name.append(“你好,”).append(“朋友”);

文件字符輸入流練習

package Io;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author 賭徒
 * 文件字符輸入流練習
 *
 */
public class FileReaderT2 {
	public static void main(String[] args) throws IOException {
		//源頭
		File srcfile=new File("data.txt");
		
		//選擇流
		FileReader fr=new FileReader(srcfile);
		
		//操作
		char[] c=new char[1024];
		int len=-1;
		while((len=fr.read(c))!=-1) {
			String string=new String(c,0,len);
			System.out.println(string);
		}
		//釋放
		fr.close();
	}
}

文件字符輸出流練習

package Io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author 賭徒
 * 文件字符輸出流練習
 *
 */
public class FileWriterT2 {
	public static void main(String[] args) throws IOException {
		//源頭
		File srcFile=new File("data.txt");
		//選擇流
		FileWriter fw=new FileWriter(srcFile);//可以加true參數決定是否附加寫入
		//操作
		fw.append("你好--");
		fw.append("你好,").append("朋友");
//             另種方法
//		String string="你好呀!";
//		char[] c=string.toCharArray();
//		fw.write(string);

		//刷新
		fw.flush();
		//釋放
		fw.close();
	
	}
}

文件字符流綜合練習

package Io;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author 賭徒
 * 文件字符流綜合練習
 *
 */
public class copy2 {
	public static void main(String[] args) throws IOException {
		//源頭
		File srcFile=new File("data.txt");
		//目的地
		File srcFile1=new File("copy2.txt");
		//選擇流
		FileReader fr=new FileReader(srcFile);
		FileWriter fw=new FileWriter(srcFile1);//可以加true參數決定是否附加寫入
		//操作
		char[]c=new char[1024];
		int len=01;
		while((len=fr.read(c))!=-1) {
			fw.write(c,0,len);
		}
		fw.flush();

		//釋放----先打開的後關閉
		fw.close();
		fr.close();
	
	}

}

字節數組流ByteArrayInputStream&ByteArrayOutputStream

ByteArrayInputStream 包含一個內部緩衝區,其中包含可以從流中讀取的字節
ByteArrayOutputStream 該類實現了將數據寫入字節數組的輸出流當數據寫入緩衝區時,緩衝區自動增長
ByteArrayInputStream​(byte[] buf) buf作爲其緩衝區數組

size( )         返回緩衝區大小
toBytearray()      創建一個新分配的字節數組

部分方法同文件字節流(read(),write())

注意:
字節數組流無需關閉

字節數組輸入綜合練習

package reIo;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * @author 賭徒
 * 字節數組輸入綜合練習
 *
 */
public class ByteArrayInputStreamT3 {
	public static void main(String[] args) throws IOException {
		//源頭
		byte[] b="你好".getBytes();
		//選擇流
		ByteArrayInputStream bis=new ByteArrayInputStream(b);
		//操作
		
		int len=-11;
		while((len=bis.read(b))!=-1) {
			String string=new String(b,0,len);
			System.out.println(string);
		}
		
		//釋放--可以省略
		
		bis.close();	
	}
}

字節數組輸出綜合練習

package reIo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author 賭徒
 * 字節數組輸出綜合練習
 *
 */
public class ByteArrayOutputStreamT3 {
	public static void main(String[] args) throws IOException {
		//源頭
		byte[] b=null;
		//選擇流
		ByteArrayOutputStream bos=new ByteArrayOutputStream();
		//操作
		String string="你好";
		byte[] b1=string.getBytes();
		bos.write(b1,0,b1.length);
		bos.flush();
		b=bos.toByteArray();
		System.err.println(new String(b));
		
		//釋放--可以省略
		bos.close();
	
	}

}

字節數組流綜合練習

package Io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author 賭徒
 * 圖片寫到字節數組中,字節數組寫出到文件
 *
 */
public class copy3 {
	public static void main(String[] args) throws IOException {
		bytearrayTOpicture(pictureTObytearray("1.png"), "2.png");
		
	}
	//圖片寫到字節數組
	public static byte[] pictureTObytearray(String fileString) throws IOException {
		//源頭
		byte[] b = null;
		//流
		FileInputStream fis=new FileInputStream(fileString);
		ByteArrayOutputStream bos=new ByteArrayOutputStream();
		//操作
		int len=-1;
		byte[] b1 = new byte[1024];
		
		while((len=fis.read(b1))!=-1) {
			bos.write(b1,0,len);
		}
		b=bos.toByteArray();
		bos.flush();
		//釋放
		bos.close();
		fis.close();
		return b;
		
	}
	//字節數組到圖片
	public static void bytearrayTOpicture(byte[] element,String fileString1 ) throws IOException {
		
		
		//流
		ByteArrayInputStream bis=new ByteArrayInputStream(element);
		FileOutputStream fos=new FileOutputStream(fileString1);
		
		//操作
		int len=-1;
		byte[] flush=new byte [1024];
		while((len=bis.read(flush))!=-1) {
			fos.write(flush,0,len);
		}
		fos.flush();
		//釋放
		fos.close();
		bis.close();
		
		
	}

	

}

其他章節:

java IO流學習筆記------(1)文件字節流

java IO流學習筆記------(3)字節緩衝流&字符緩衝流&轉換流&數據流
java IO學習筆記------(4)對象流&打印流&文件分割(隨機流)&文件的合併(序列流)

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