-
IO流详解
2019-08-12 16:24:00IO流详解一.IO流分类二.使用各种输入输出流对文件进行复制1.字节流1.1 FileInputStream和FileOutputStream1.2BufferedOutputStream和 BufferedInputStream1.3printStream2.字符流2.1OutputStreamWrite和 ...
在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(); }
-
io流详解
2019-02-25 16:29:11io流详解 参考文献 流:数据在两设备间的传输称为流,流的本质是数据传输 IO流的分类 根据处理数据类型的不同分为:字符流和字节流 根据数据流向不同分为:输入流和输出流 字符流和字节流(字符流本质其实是基于字节...io流详解
参考文献
流:数据在两设备间的传输称为流,流的本质是数据传输
IO流的分类
根据处理数据类型的不同分为:字符流和字节流
根据数据流向不同分为:输入流和输出流
字符流和字节流(字符流本质其实是基于字节流读取时,去查了指定的码表)
字节流和字符流的区别:
(1)读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。
(2)处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。(3)字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的;而字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件
结论:优先选用字节流。
输入流和输出流
对输入流只能进行读操作,对输出流只能进行写操作,程序中需要根据待传输数据的不同特性而使用不同的流。java流类图:
输入字节流InputStream
InputStream 是所有的输入字节流的父类,它是一个抽象类。
ByteArrayInputStream
StringBufferInputStream
FileInputStream 是三种基本的介质流,它们分别从Byte 数组、StringBuffer、和本地文件中读取数据
【案例】字节流读取文件终极版/** * 字节流 *读文件 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; /** *比如说要在temp目录下建立一个test.txt文件,在Windows下应该这么写: *File file1 = new File ("C:\tmp\test.txt"); *在Linux下则是这样的: *File file2 = new File ("/tmp/test.txt"); *如果要考虑跨平台,则最好是这么写: *File myFile = new File("C:" + File.separator + "tmp" + File.separator, "test.txt"); **/ File f=new File(fileName);//创建文件对象 InputStream in=new FileInputStream(f);//创建输入流对象 byte[] b=new byte[1024];//设定了字节数组大小 int count =0; int temp=0; while((temp=in.read())!=(-1)){//输入流对象读取不到对象的时候会返回-1 b[count++]=(byte)temp; } in.close();//关闭输入流 System.out.println(new String(b));//控制台输出文件内容 } }
输出字节流OutputStream
定义和结构说明:
IO 中输出字节流的继承图可见上图,可以看出:
OutputStream 是所有的输出字节流的父类,它是一个抽象类。
ByteArrayOutputStream、FileOutputStream是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。
【案例】向文件中写入字符串/** * 字节流 * 向文件中追加新内容: * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); OutputStream out =new FileOutputStream(f,true);//true表示追加模式,否则为覆盖 String str="Rollen"; //String str="\r\nRollen"; 可以换行 byte[] b=str.getBytes(); for (int i = 0; i < b.length; i++) { out.write(b[i]); } out.close(); } }
【复制文件,in和out结合】
/** * 文件的复制 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { if(args.length!=2){ System.out.println("命令行参数输入有误,请检查"); System.exit(1); } File file1=new File(args[0]); File file2=new File(args[1]); if(!file1.exists()){ System.out.println("被复制的文件不存在"); System.exit(1); } InputStream input=new FileInputStream(file1); OutputStream output=new FileOutputStream(file2); if((input!=null)&&(output!=null)){ int temp=0; while((temp=input.read())!=(-1)){ output.write(temp); } } input.close(); output.close(); } }
字符输入流Reader
定义和说明:
Reader 是所有的输入字符流的父类,它是一个抽象类。
CharReader、StringReader是两种基本的介质流,它们分别将Char 数组、String中读取数据
BufferedReader是一个装饰器,它和其子类负责装饰其它Reader 对象。
InputStreamReader 是一个连接字节流和字符流的桥梁,它将字节流转变为字符流。
FileReader可以说是一个达到此功能、常用的工具类,在其源代码中明显使用了将FileInputStream 转变为Reader 的方法。我们可以从这个类中得到一定的技巧。Reader 中各个类的用途和使用方法基本和InputStream 中的类使用一致。
【案例BufferedReader】import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * 使用缓冲区从键盘上读入内容 * */ public class BufferedReaderDemo{ public static void main(String[] args){ BufferedReader buf = new BufferedReader( newInputStreamReader(System.in)); String str = null; System.out.println("请输入内容"); try{ str = buf.readLine(); }catch(IOException e){ e.printStackTrace(); } System.out.println("你输入的内容是:" + str); } }
【案例Scanner】
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** *Scanner的小例子,从文件中读内容 * */ public class ScannerDemo{ public static void main(String[] args){ File file = new File("d:" + File.separator +"hello.txt"); Scanner sca = null; try{ sca = new Scanner(file); }catch(FileNotFoundException e){ e.printStackTrace(); } String str = sca.next(); System.out.println("从文件中读取的内容是:" + str); } }
字符输出流writer
Writer 是所有的输出字符流的父类,它是一个抽象类。
CharArrayWriter、StringWriter 是两种基本的介质流,它们分别向Char 数组、String 中写入数据。
OutputStreamWriter 是OutputStream 到Writer 转换的桥梁,它的子类FileWriter 其实就是一个实现此功能的具体类
【案例Scanner】向文件中写入数据/** * 字符流 * 写入数据 * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); Writer out =new FileWriter(f); String str="hello"; out.write(str); out.close(); } }
-
C#IO流详解
2017-07-14 16:31:53io流详解