Java IO操作(各種流和CommonsIO組件)

1、編碼與解碼
從字符到字節==>編碼(encode)

從字節到字符==>解碼(decode)

2、File類
一般使用字節/符緩衝流包含字節/符流進行文件的讀寫。

System.out.println(System.getProperty("user.dir"));//查看當前目錄
File file = new File("123.txt");//默認會在當前目錄下建立
file.createNewFile();//創建文件
System.out.println(file.exists());//判斷文件是否存在
file.isFile();//是否是文件
file.delete();//刪除文件

//創建目錄
File file2 = new File("D:/123");
file2.mkdirs();//返回一個boolean類型的值。
file2.isDirectory();//是否是目錄

3、字節流(一般用於音頻、視頻、word、excle)==>InputStream/OutputStream類
3.1文件字節流(通過java—>OS—>文件)
四步建立:創建源、選擇流、操作、關閉。

//讀文件
//(1)創建源
File file = new File("123.txt");
//(2)選擇流
InputStream input = null;
try {
	input = new FileInputStream(file);	
	//(3)操作
	byte[] car = new byte[1024];//緩衝容器,實現分段讀取提高效率
	int len = -1;//接受長度
	while ((len = input.read(car)) !=-1) {
		//new String(car,0,len,"UTF-8")就是將字節數組內car,
		//從索引的第0位取len長度後組成字符串,切組成後的字符串按照utf8的字符集編碼。
		String str = new String(car,0,len);
		System.out.println(str);			
	}		
} catch (FileNotFoundException e) {
	e.printStackTrace();
}finally {
	//(4)關閉
	if (input!=null) {
		input.close();
	}		
}
//寫文件
File file = new File("123.txt");
OutputStream output = null;
try {
	output = new FileOutputStream(file,true);//有true表示追加寫入,默認false
	String str = "world";
	byte[] car = str.getBytes();//字符串轉字節
	output.write(car, 0, car.length);
	output.flush();	//刷新流,養成習慣寫
} catch (FileNotFoundException e) {
	e.printStackTrace();
}finally {
	if (output!=null) {
		output.close();
	}
}

3.2字節數組流(java—>內存中的字節數組,所以gc會處理,不用close關閉),不是文件操作。一般底層用,瞭解

//讀操作,相似代碼,不同點創建源,即(1)處不同
byte[] str = "world".getBytes();
InputStream inputArray = new ByteArrayInputStream(str);
//寫操作不同點
OutputStream outputArray = new ByteArrayOutputStream();
byte[] word = "world".getBytes();
outputArray.write();
outputArray.flush();
byte[] out = outputArray.toByteArray();//接收內容
//最後要用

4、字符流(一般用於文本文件)==>Reader/Writer類

//讀文件
File file = new File("123.txt");
Reader reader = null;
try {
	reader = new FileReader(file);
	char[] car = new char[1024];//與字符流不同的地方
	int len = -1;
	while ((len = reader.read(car)) !=-1) {
		String str = new String(car,0,len);
		System.out.println(str);			
	}		
} catch (FileNotFoundException e) {
	e.printStackTrace();
}finally {
	if (reader!=null) {
		reader.close();
	}		
}
File file = new File("123.txt");
Writer writer = null;
try {
	writer = new FileWriter(file,true);
	//寫法一
	String str = "我的world";
	writer.write(str);
	writer.flush();	
	//寫法二
	writer.append("world").append("world");
	writer.flush();	
} catch (FileNotFoundException e) {
	e.printStackTrace();
}finally {
	if (writer!=null) {
		writer.close();
	}
}

5、裝飾器模式()
在博客中有詳細解釋。

6、字節緩衝流(提高字節流性能)
注意:關閉是關閉最外面的流即可。

//直接套住字節流,讀寫與字節流相同
File file = new File("123.txt");	
BufferedInputStream bf = new BufferedInputStream(new FileInputStream(file));

7、字符緩衝流

//讀文件
File file = new File("123.txt");
BufferedReader bf = null;
try {
	bf = new BufferedReader(new FileReader(file));
	String line = null;
	while ((line = bf.readLine())!=null) {
		System.out.println(line);
	}
} catch (FileNotFoundException e) {
	e.printStackTrace();
}finally {
	if (bf!=null) {
		bf.close();
	}
}
//寫文件
//同字符流寫入相似,區別換行符可以不用\r\n,用bf.readLine();

8、轉換流(字節流轉字符流,可以指定字符集,一般鍵盤輸入輸出、和一些系統底層傳輸的數據都是字節流,轉換字符流方便操作)

InputStreamReader input = new InputStreamReader(System.in);
OutputStreamWriter output = new OutputStreamWriter(System.out);
//一般使用的時候用字符緩衝流BufferedReader包裹,如:
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

//可指定字符集
InputStreamReader input = new InputStreamReader(System.in,“utf-8);

9、數據流(DataInputStream和DataOutputStream)
數據流將“基本數據類型與字符串類型”作爲數據源,從而允許程序以與機器無關的方式從底層輸入輸出流中操作Java基本數據類型與字符串類型。

10、對象流(ObjectInputStream/ObjectOutputStream)
對某個對象進行讀寫操作,之前的流並不能讀取對象(字符串除外)。

11、打印流(PrintStream)
將功能添加到另一個輸出流,方便的打印各種數據值。

12、CommonsIO組件
1、首先選擇對應版本的組件下載http://commons.apache.org/proper/commons-io/
2、解壓後,commons-io-2.7.jar、commons-io-2.7-sources.jar兩個jar包(後一個是源碼),拷貝到項目下(一般建一個lib文件)
3、右鍵commons-io-2.7.jar,選擇Build Path,add添加構建

//讀文件
//字符讀
String string = FileUtils.readFileToString(file, "utf-8");
System.out.println(string);
//字節讀
byte[] datas = FileUtils.readFileToByteArray(file);
String string2 = new String(datas,0,datas.length);
System.out.println(string2);
//逐行讀取
List<String> string3 = FileUtils.readLines(file,"utf-8");
for (String str : string3) {
	System.out.println(str);
}

//寫文件
//字符寫
FileUtils.write(file,"我的world","utf-8",true);
//字節寫
FileUtils.writeByteArrayToFile(file, "我的world".getBytes("utf-8"), true);
//寫出列表
List<String> strings = new ArrayList<String>();
strings.add("我");
strings.add("的");
strings.add("world");
FileUtils.writeLines(file,strings,"-",true);//第三個參數不加,默認換行

//文件複製
FileUtils.copyFile(new File("1.png"), new File("1_複製.png"));
//拷貝網上的圖片/文件
FileUtils.copyURLToFile(new URL("http://1.png"),new File("photo.png"));
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章