在Java中,将这种通过不同输入输出设备(键盘没存,显示器,网络等)之间的数据传输抽象表示为“流”,程序允许通过流的方式与输入输出设备进行数据传输。Java中的“流”都位于java.io包中,称为(输入输出)流。
一.IO流分类
二.使用各种输入输出流对文件进行复制
1.字节流
1.1 FileInputStream和FileOutputStream
FileInputStream:字节读取流,用字节流读取文件。
FileOutputStream:字节写入流,用字节流写入文件。
代码如下
使用FileInputStream和FileOutputStream复制文件内容
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("D:\\11.txt");
//在FileOutputStream的第二个参数输入true时,文件中原有数据将不会被清空,而是在原有数据后面追加新的数据
FileOutputStream fos = new FileOutputStream("D:\\111.txt",true);
//一次读取一个字节数据
int by;
while ((by=fis.read())!=-1){
fos.write(by);
}
//先释放写,再释放读,因为写完了肯定就读完了
fos.close();
fis.close();
}
1.2BufferedOutputStream和 BufferedInputStream
BufferedOutputStream:缓存写入字节流,相当于一个临时的缓冲区,可以提高写入效率,减少了对文件的操作次数。它的构造方法接收OutputStream类型的参数作为对象。
BufferedInputStream:缓存读取字节流,相当于一个临时的缓冲区,可以提高读取效率,减少了对文件的操作次数。它的构造方法接收InputStream类型的参数作为对象。
代码如下
使用BufferedOutputStream和 BufferedInputStream写入hello和world到文件,然后读取文件内容,输出到打印台
public static void main(String[] args) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\1.txt"));
bos.write("hello\r\n".getBytes()); //\r\n是windous下的换行符
bos.write("world\r\n".getBytes());
bos.close();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\1.txt"));
byte[] bytes=new byte[8192]; //定义一个字节数组,作为缓冲区,长度一般为1024的倍数
int len;
while ((len=bis.read(bytes))!=-1){
System.out.println(new String(bytes,0,len)); //从第一个字节开始,向文件中写入len个字节
}
bis.close();
}
1.3printStream
printStream:以字节流的方式写入内容
代码如下
向文件中写入内容
public static void main(String[] args) throws IOException {
PrintStream ps = new PrintStream("D:\\2.txt");
ps.write(97); //以字节形式写入文件
ps.print(97); //直接写入括号内内容
ps.println(); //换行
ps.print(98);
ps.close();
2.字符流
2.1OutputStreamWrite和 InputStreamReader
2.1.1编码和解码
一般编码和解码类型一致
代码如下
public static void main(String[] args) throws UnsupportedEncodingException {
String s="中国";
//编码
byte[] bytes = s.getBytes("GBK");//[-42, -48, -71, -6] 一个字符占两位
byte[] bytes2 = s.getBytes("UTF-8");//[-28, -72, -83, -27, -101, -67] 一个字符占三位
System.out.println(Arrays.toString(bytes));
System.out.println(Arrays.toString(bytes2));
//解码
String gbk = new String(bytes, "GBK"); //编码和解码一致 :中国
String gbk2 = new String(bytes, "UTF-8");//编码和解码不一致 :�й�
String gbk3 = new String(bytes2, "UTF-8");//编码和解码一致 :中国
String gbk4 = new String(bytes2, "GBK");//编码和解码不一致 :涓浗
System.out.println(gbk);
System.out.println(gbk2);
System.out.println(gbk3);
System.out.println(gbk4);
}
2.1.2OutputStreamWrite和 InputStreamReader示列
OutputStreamWrite:转换流,字节流转换为字符流进行输入,构造方法中应传入一个OutputStream类型字节流作为参数传入。
InputStreamReader:转换流,字节流转换为字符流进行读取,构造方法中应传入一个InputStream类型字节流作为参数传入。
代码如下
将指定中文字符写入文件,然后读取出来打印输出到控制台
public static void main(String[] args) throws IOException {
//第二个参数是给定指定字符集编码器
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\1.txt"),"UTF-8");
osw.write("中国");
osw.close();
//第二个参数是给定指定字符集编码器
InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\1.txt"), "utf-8");
//一次读取一个字符数据
int ch;
while ((ch=isr.read())!=-1){
System.out.println((char)ch);
}
isr.close();
}
2.2BufferedReader和BufferedWriter
BufferedReader:缓存写入字符流,相当于一个临时的缓冲区,可以提高写入效率,减少了对文件的操作次数。它的构造方法接收FileReader类型的参数作为对象。
BufferedWriter:缓存读取字符流,相当于一个临时的缓冲区,可以提高读取效率,减少了对文件的操作次数。它的构造方法接收FileWriter类型的参数作为对象。
代码如下
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("D:\\3.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\2.txt"));
// //一次读一个字符
// int ch;
// while ((ch=br.read())!=-1){
// System.out.println((char)ch);
// }
// //一次读一个字符数组
// char[] chars=new char[1024];
// int len;
// while ((len=br.read(chars))!=-1){
// System.out.println(new String(chars,0,len));
// }
//一次读取一排字符
String line;
while ((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
2.3FileReader和FileWriter
FileReader:从文件中读取。
FileWriter:输入到文件。
代码如下
此处进行文件复制,示列不使用throws进行抛出时,使用try…catch…finally进行处理时的处理方法
private static void method2() {
FileReader sr =null;
FileWriter fw =null;
try {
sr= new FileReader("D:\\j1.txt");
fw= new FileWriter("D:\\4.txt");
char[] chars=new char[1024];
int len;
while ((len=sr.read())!=-1){
fw.write(len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
sr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.4PrintWriter
PrintWriter:字符打印流
代码如下
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter("D:\\2.txt");
//写入
pw.write("hello");
pw.write("\r\n");
pw.write("world");
pw.write("\r\n");
//刷新
pw.flush();
pw.println("hello"); //写入时会自动换行
pw.println("world");
pw.flush();
pw.close();
//第二个参数为true时候会自动刷新
PrintWriter pw1 = new PrintWriter(new FileWriter("D:\\03.txt"),true);
pw1.println("hello");
pw1.println("world");
pw1.close();
}
三.各输入输出流速度比较
public static void main(String[] args) throws IOException{
long startTim = System.currentTimeMillis();
//复制视频
method1(); //73023
// method2(); //103
// method3(); //306
// method4(); //50
long endTim=System.currentTimeMillis();
System.out.println(endTim-startTim);
}
//字节缓冲流一次读写一个字节数组
private static void method4() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\001.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\002.avi"));
byte[] bytes=new byte[1024];
int len;
while ((len=bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bis.close();
bos.close();
}
//字节缓冲流一次读写一个字节
private static void method3() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\001.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\002.avi"));
int len;
while ((len = bis.read()) != -1) {
bos.write(len);
}
bis.close();
bos.close();
}
//基本字节流一次读写一个字节数组
private static void method2() throws IOException{
FileInputStream fis = new FileInputStream("D:\\001.avi");
FileOutputStream fos = new FileOutputStream("D:\\002.avi");
byte[] bytes=new byte[1024];
int len;
while ((len = fis.read(bytes) )!= -1) {
fos.write(bytes,0,len);
}
fis.close();
fos.close();
}
//基本字节流一次读写一个字节
private static void method1() throws IOException {
FileInputStream fis = new FileInputStream("D:\\001.avi");
FileOutputStream fos = new FileOutputStream("D:\\002.avi");
int len;
while ((len=fis.read())!=-1){
fos.write(len);
}
fis.close();
fos.close();
}