IO流主要分为input 流和output 流,对于不同的操作方式分为字节流操作和字符流操作,以下为java中主要用来IO流操作的类
OutputStream(字节流)
FileOutputStream(File file)
(默认都是将流覆盖原始的文件)FileOutputStream(String name)
(默认都是将流覆盖原始的文件)FileOutputStream(File file, boolean append) FileOutputStream(String name, boolean append)
close()
flush()
write(byte[] b)
write(byte[] b, int off, int len)
String string = "helloworld";
OutputStream os=null;
try {
os = new FileOutputStream("D:\\0XYZ\\a.txt");
byte [] buffer = string.getBytes();
os.write(buffer,0,string.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream(字节流)
FileInputStream(File file)
传入一个String类型FileInputStream(String name)
close()
read(byte[] b, int off, int len)
返回的是一个读取的长度,当最后读完后再进行读取时返回-1 InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream("C:\\a.txt");
os = new FileOutputStream("D:\\a.txt");
byte [] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes,0,bytes.length))!=-1){
os.write(bytes,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
基本思想就是减少磁盘的访问次数,一次性的先从磁盘中获取一部分数据到内存中。程序后续如果能从
缓存中获取数据,就不会真正访问磁盘,以提高读取效率。
用到的两个类BufferedInputStream和BufferedOutputStream
构造方法:
可以指定缓存区大小,如果不指定就是8192=8kb
关闭流,直接关闭缓存流,内部的资源流也会关闭
具体使用:
InputStream is = null;
OutputStream os = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
is = new FileInputStream("C:\\a.txt");
os = new FileOutputStream("D:\\a.txt");
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(os);
byte [] bytes = new byte[1024];
int len = -1;
while((len = bis.read(bytes,0,bytes.length))!=-1){
bos.write(bytes,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
因为读取和写入的编码格式不一致,如果采用字节流进行文件操作,在打开后会存在乱码问题
常见的字符编码:
传入的类型都是InputStream类型,也支持传入缓冲流,在构造方法中支持传入字符编码格式
使用第四种构造方法中文编码格式:GB2311 GBK utf8 都可以作为字符串传入
读取方法:
read(char[] cbuf, int offset, int length)
可以自动按照文字的字节读取,不会乱码
类似InputStreamReader
写入方法:
write(char[] cbuf, int off, int len)
将C盘下以GBK编码的a.txt文件复制到D盘下,并以utf8格式写入,原理是利用了二进制流加缓存流然后转换成字符流进行操作
InputStream is = null;
OutputStream os = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
is = new FileInputStream("C:\\a.txt");
os = new FileOutputStream("D:\\a.txt");
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(os);
isr = new InputStreamReader(bis,"GBK");
osw = new OutputStreamWriter(bos,"utf8");
char [] buffer = new char[1024];
int len = -1;
while((len = isr.read(buffer,0,buffer.length))!=-1){
osw.write(buffer,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
重要方法是存在读取和写入一行的操作,可以先将字节流转换成字符流,然后加入缓存机制,优势是可以进行读一行和写一行的操作,在写入和读取的时候更灵活。
将C盘下的一个以gbk编码的aaa.txt文件以utf8格式写入到D盘下的bbb.txt文件
InputStream is = null;
OutputStream os = null;
InputStreamReader isr = null;
OutputStreamWriter osw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
is = new FileInputStream("C:\\aaa.txt");
os = new FileOutputStream("D:\\bbb.txt");
isr = new InputStreamReader(is,"gbk");
osw = new OutputStreamWriter(os,"utf8");
br =new BufferedReader(isr);
bw = new BufferedWriter(osw);
String str = null;
//这里使用!=而不用equals,是因为当str最后为null时会报空指针异常,程序崩溃
while ((str = br.readLine())!=null){
System.out.println(str);
bw.write(str);
bw.newLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
相当于:
相当于:
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("C:\\b.txt");
fw = new FileWriter("D:\\b.txt");
char[] buffer = new char[1024];
int len = -1;
while ((len = fr.read(buffer,0,buffer.length))!=-1){
fw.write(buffer,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}