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"));
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章