javaIO流2
JavaIO流2
缓冲流
提高读、写效率
原因:内部提供缓冲区
BufferedInputStream BufferedOutputStream 的使用
在 FileInputStream和 FileOutputStream的基础上微微改动
复制非文本文件
//造流
FileInputStream fis = new FileInputStream(comefile);
FileOutputStream fos = new FileOutputStream(gofile);
//造缓冲流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[10];
int len = bis.read(buffer);
while (len != -1){
bos.write(buffer,0,len);
}
//bos.flush:清空缓冲区
BufferedReader BufferedWriter 的使用
//复制文本文件
BufferedReader br = new BufferedReader(new FileReader(new File("文件名1")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("文件名2")))
//方式一:只用char[]数组
char[]cbuf = new char[1024];
int len = br.read(cbuf);
while (len != -1){
bw.write(cbuf,0,len);
bw.flush
}
//方式二:使用String
String date = br.readLine();//一行一行的读取
while(date != null){
bw.write(date+"\n")//输出一行date里不包括换行符
或是: bw.newline;
date = br.readLine();
}
转换流
字符流,字节流之间的转换
InputStreamRrader:将一个字节的输入流转化为字符的输入流
OutputStreamWriter:将一个字符的输出流转化为字节的输出流
InputStreamReader isr = null;
OutputStreamWriter osr = null;
try {
File comefile = new File("src\\hello");
File gofile = new File("src\\hello_gbk");
FileInputStream fis = new FileInputStream(comefile);
FileOutputStream fos = new FileOutputStream(gofile);
isr = new InputStreamReader(fis);//解码
osr = new OutputStreamWriter(fos,"gbk");//编码
char[] cubf = new char[20];
int len;
while((len = isr.read(cubf)) != -1){
osr.write(cubf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
}
try {
if(osr != null)
osr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(isr != null)
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
字符集
将x拼在一起就是二进制了