注:FileReader继承InputStreamReader类,InputStreamReader实现Reader接口,其余同理。windows
对于文件内容的操做主要分为两大类数组
分别是:框架
字符流函数
字节流测试
其中,字符流有两个抽象类:Writer Reader优化
其对应子类FileWriter和FileReader可实现文件的读写操做编码
BufferedWriter和BufferedReader可以提供缓冲区功能,用以提升效率spa
一样,字节流也有两个抽象类:InputStream OutputStream指针
其对应子类有FileInputStream和FileOutputStream实现文件读写code
BufferedInputStream和BufferedOutputStream提供缓冲区功能
字符流和字节流的主要区别:
1.字节流读取的时候,读到一个字节就返回一个字节; 字符流使用了字节流读到一个或多个字节(中文对应的字节数是两个,在UTF-8码表中是3个字节)时。先去查指定的编码表,将查到的字符返回。
2.字节流能够处理全部类型数据,如:图片,MP3,AVI视频文件,而字符流只能处理字符数据。只要是处理纯文本数据,就要优先考虑使用字符流,除此以外都用字节流。
注意:
读写流使用完都要用.close方法关闭,防止占用资源,可是在关闭以前防止空值最好作空值判断再关闭;进行写操做时想当即生效用write.flush()刷新。
注意写文件路径时盘符后面用\\或者/,如:c:\\demo.txt或者c:/demo.txt
BufferedReader特有方法:readLine(),将行标记以前的数据做为字符串返回,读到结尾时返回null,读取行是将读取的字符临时存储产生的效果。
具体操做方法以下:
FileReader(字符流的读取)
FileReader r = new FileReader(path); //方式一:读取单个字符的方式 //每读取一次,向下移动一个字符单位,返回读取的字节数 int temp1 = r.read(); System.out.println((char)temp1); int temp2 = r.read(); System.out.println((char)temp2); //方式二:循环读取 //read()方法读到文件末尾会返回-1 /* while (true) { int temp = r.read(); if (temp == -1) { break; } System.out.print((char)temp); } */ //方式三:循环读取的简化操做 //单个字符读取,当temp不等于-1的时候打印字符 /*int temp = 0; while ((temp = r.read()) != -1) { System.out.print((char)temp); } */ //方式四:读入到字符数组 /* char[] buf = new char[1024]; int temp = r.read(buf); //将数组转化为字符串打印,后面参数的意思是 //若是字符数组未满,转化成字符串打印后尾部也许会出现其余字符 //所以,读取的字符有多少个,就转化多少为字符串 System.out.println(new String(buf,0,temp)); */ //方式五:读入到字符数组的优化 //因为有时候文件太大,没法肯定须要定义的数组大小 //所以通常定义数组长度为1024,采用循环的方式读入 /* char[] buf = new char[1024]; int temp = 0; while((temp = r.read(buf)) != -1) { System.out.print(new String(buf,0,temp)); } */ FileWriter(字符流的写入) String path="E:\\demo.txt"; //因为IO操做会抛出异常,所以在try语句块的外部定义FileWriter的引用 FileWriter w = null; try { //以path为路径建立一个新的FileWriter对象 //若是须要追加数据,而不是覆盖,则使用FileWriter(path,true)构造方法 w = new FileWriter(path); //将字符串写入到流中,\r\n表示换行想有好的 w.write("Nerxious is a good boy\r\n"); //若是想立刻看到写入效果,则须要调用w.flush()方法 w.flush(); } catch (IOException e) { e.printStackTrace(); } finally { //若是前面发生异常,那么是没法产生w对象的 //所以要作出判断,以避免发生空指针异常 if(w != null) { try { //关闭流资源,须要再次捕捉异常 w.close(); } catch (IOException e) { e.printStackTrace(); } } } 即FileWriter w = new FileWriter(path);//指定写的路径 w.write("Nerxious is a good boy\r\n");//写入具体内容 文本文件的复制 String doc=”...”; String copy=”...”: FileReader r= new FileReader(doc); FileWriter w = new FileWriter(copy); //方式一:单个字符写入 int temp = 0; while((temp = r.read()) != -1) { w.write(temp); } //方式二:字符数组方式写入 /* char[] buf = new char[1024]; int temp = 0; while ((temp = r.read(buf)) != -1) { w.write(new String(buf,0,temp)); } */ 利用字符流的缓冲区来进行文本文件的复制 FileReader r = new FileReader(doc); FileWriter w = new FileWriter(copy); //建立缓冲区对象 //将须要提升效率的FileReader和FileWriter对象放入其构造函数内 //固然,也可使用匿名对象的方式 br = new BufferedReader(new FileReader(doc)); BufferedReader br = new BufferedReader(r); BufferedWriter bw = new BufferedWriter(w); String line = null; //读取行,直到返回null //readLine()方法只返回换行符以前的数据 while((line = br.readLine()) != null) { //使用BufferWriter对象的写入方法 bw.write(line); //写完文件内容以后换行 //newLine()方法依据平台而定 //windows下的换行是\r\n //Linux下则是\n bw.newLine(); } 字节流: FileOutputStream(字符流的写入) FileOutputStream o = new FileOutputStream(path); String str = "Nerxious is a good boy\r\n"; byte[] buf = str.getBytes(); //也能够直接使用o.write("String".getBytes()); //由于字符串就是一个对象,能直接调用方法 o.write(buf); FileInputStream(字节流的读取) FileInputStream i = new FileInputStream(path); //方式一:单个字符读取 //须要注意的是,此处我用英文文本测试效果良好 //但中文就悲剧了,不过下面两个方法效果良好 int ch = 0; while((ch=i.read()) != -1){ System.out.print((char)ch); } //方式二:数组循环读取 /* byte[] buf = new byte[1024]; int len = 0; while((len = i.read(buf)) != -1) { System.out.println(new String(buf,0,len)); } */ //方式三:标准大小的数组读取 /* //定一个一个恰好大小的数组 //available()方法返回文件的字节数 //可是,若是文件过大,内存溢出,那就悲剧了 //因此,亲们要慎用!!!上面那个方法就不错 byte[] buf = new byte[i.available()]; i.read(buf); //由于数组大小恰好,因此转换为字符串时无需在构造函数中设置起始点 System.out.println(new String(buf)); */ 二进制文件(即非纯文本文件)的复制 Bin/copy为 文件路径,“E:\\demo.mp3”,此处示例文件为mp3文件 FileInputStream i = null; FileOutputStream o = null; try { i = new FileInputStream(bin); o = new FileOutputStream(copy); //循环的方式读入写出文件,从而完成复制 byte[] buf = new byte[1024]; int temp = 0; while((temp = i.read(buf)) != -1) { o.write(buf, 0, temp); } 利用字节流的缓冲区进行二进制文件的复制 FileInputStream i = null; FileOutputStream o = null; BufferedInputStream bi = null; BufferedOutputStream bo = null; try { i = new FileInputStream(bin); o = new FileOutputStream(copy); bi = new BufferedInputStream(i); bo = new BufferedOutputStream(o); byte[] buf = new byte[1024]; int temp = 0; while((temp = bi.read(buf)) != -1) { bo.write(buf,0,temp); } 文件的操做 递归列出目录下全部文件 File f = new File(path); //方式一:list() //返回一个包含指定目录下全部文件名的字符串数组 //若是不是一个目录则返回null String[] files = f.list(); for (String x : files) { System.out.println(x); } //方式二: if(f.isDirectory()){ File[] files = f.listFiles(); for(File x : files) { print(x); } } 二者都是返回目录下的全部文件名,可是第二种方式更实用,为递归列出文件作铺垫 列出根目录: //listRoots()是一个静态方法,返回文件数组 File[] files = File.listRoots(); //foreach循环打印File对象 for (File x : files) { System.out.println(x); } 本地环境是Linux,因此根目录只有一个 /,若是是Windows就能列出你的全部盘符 Scanner类: 从键盘读取 Scanner input = new Scanner(System.in); System.out.println("请输出一个整数:"); int i = input.nextInt(); System.out.println("你输入的整数是:" + i); 从字符串读取 Scanner input = new Scanner("hello\r\nworld\r\n"); //循环读取,hasNext()方法和集合框架里面的同样使 while(input.hasNext()) { //每次读取一行,别的读取方法见API,比较简单 String s = input.nextLine(); System.out.println(s); } 从文件读取: File f = new File(path); //从文件构造Scanner对象,有可能产生异常 Scanner input = new Scanner(f); while(input.hasNext()) { String s = input.nextLine(); System.out.println(s); } PrintWriter类 向文件写入内容 File file = new File(path); //此处构造函数还能够传其余对象,具体参考API文档 PrintWriter p = new PrintWriter(file); //向文件写入一行,此外还有print()和printf()方法 p.println("若是有一天我回到从前"); p.println("回到最原始的我"); p.println("你是否会以为我不错"); //刷新流 p.flush(); 与PrintWriter相似的还有一个PrintStream类,此处以PrintWriter举例是由于文本文件具备人为可读性,而二进制文件(字节模式)则须要使用专门的程序来读取.可能有人会问:FileOutputStream、 FileWriter都能写文件,那么为什么还须要PrintWriter和PrintStream类,若是细看API文档,能够知道前者单纯的字符写入流和字节写入流操做的方式大多用数组进行,对文件的细化处理很是不方便,而PrintWriter和PrintStream则很好的解决了这一问题,提供print()等方法,而且,PrintWriter和PrintStream对于不存在文件对象的状况下会直接建立,若是已有文件对象,它们则会把原有文件给覆盖掉,却没有增长方法。解决这问题也很简单,再看API文档,PrintWriter有一个构造方法PrintWriter(Writer out),也就是可以传入Writer对象,PrintStream有一个构造方法PrintStream(OutputStream out),也就是能传入OutputStream对象。所以,咱们这样写就能够了 new PrintWriter(new FileWriter(file,true)) new PrintStream(new FileOutputStream(file,true)) 既能增长数据,也能更高效的处理文件,见以下代码示范 File file = new File(path); //利用FileWriter方式构建PrintWriter对象,实现追加 PrintWriter p = new PrintWriter(new FileWriter(file,true)); p.println("尼玛 这一句就是追加的 看到没"); p.flush(); System类相关: //别忘了,OutputStream是全部字节写入流的父类 OutputStream out = System.out; //写入数据,只能是数组,因此用getBytes()方法 out.write("Hello,bitch!\r\n".getBytes()); System类中的读取 InputStream in = System.in; System.out.print("请输入文字: "); byte[] buf = new byte[1024]; int len = 0; //将输入的数据保证到数组中,len记录输入的长度 len = in.read(buf); //用字符串的方式打印数组中的数据 System.out.println("你的输入是: " + new String(buf,0,len)); 利用BufferedReader实现对键盘的读取 BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); System.out.print("请输入文本:"); String str = b.readLine(); System.out.println("你输入的是:" + str); //循环读取方式 /* while(true) { System.out.print("请输入文本:"); String str = b.readLine(); //若是输入over就结束循环 if("over".equals(str)) { break; } System.out.println("你输入的是:" + str);