码迷,mamicode.com
首页 > 编程语言 > 详细

学习java第17天个人总结

时间:2017-04-24 22:57:42      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:9.png   实现   创建   stream   存储   写入   leo   文本   java   

Day17个人总结

一、字节输入输出流

1、字节输入流

超类:FileInputStream

创建流对象

FileInputStream fis = new FileInputStream(file);

其中构造方法中的(file)可以是String字符串的路径比如:”D:\\a\\b.txt”,或者是File对象

2、方法的使用:

构造方法

 技术分享

 

reader()方法(非常重要)

 技术分享

技术分享

技术分享

 

 

 

以上的close方法用于关闭流释放资源

3、使用while循环遍历数据过程

public class Test1 {

    public static void main(String[] args) {

       FileInputStream fis = null;

       int len = 0;

       try {

           fis = new FileInputStream("D:\\a\\a.txt");

           //读取文件

           byte[] bs = new byte[10];

           int num1 = 0;

           while((num1 = fis.read(bs)) != -1){

              System.out.println(Arrays.toString(bs));

           }

       } catch (FileNotFoundException e) {

           e.printStackTrace();

       } catch (IOException e) {

           e.printStackTrace();

       }

    }

}

4、字节输出流

超类:FileOnputStream

创建对象

FileOnputStream fos = new FileOnputStream(“D:\\a\\a.txt”);

通过以上创建对象,(“D:\\a\\a.txt”)中的a.txt如果存在将会使用存在的文件,如果不存在将会自动创建。

5、方法的使用:

构造方法:

技术分享

 

方法摘要:

 

 技术分享

 

flush()方法

flush()方法用于刷新缓存区将缓存区内容强制写入文件

6、使用while循环将数据写入到文件

public class IntOutFilee {

    public static void main(String[] args) {

       //初始化

       FileInputStream fir = null;

       FileOutputStream fos = null;

       //读取文件

       try {

           //创建读写文件对象

           fir = new FileInputStream("D:\\aaaa\\a.txt");

           fos = new FileOutputStream("D:\\aaaa\\b.txt");

           //创建byte[] 工作中常用  要么偶数要么1024的倍数

           byte[] b = new byte[10];

           int count = 0;//记录读取的个数

           //while循环读取文件

           try {

              while((count = fir.read(b)) != -1){

                  //写入文件

                  fos.write(b,0,count);

                  //强制清楚缓冲文件

                  fos.flush();

              }

           } catch (IOException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

           }

       } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }finally{

           if(fir != null){

              try {

                  fir.close();

              } catch (IOException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

              }

           }

           if(fos != null){

              try {

                  fos.close();

              } catch (IOException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

              }

           }

       }

    }

}

注:以上是通过使用字节输入输出流做文件的复制操作过程

二、字符输入输出流

1、字符输入流

超类:Reader

创建对象:

File f1 = new File("D:\\a\\a.txt");

FileReader fr1 = null;

fr1 = new FileReader(f1);

2、构造方法的使用:

 技术分享

 

3、方法摘要:

 技术分享

 

和字节输入流不同的是字符输入流在读取文件使用read方法的时候使用的是char[]数组,而在字节输入流中使用的byte[]数组,使用方法使用字符输入流的使用如下:

public class Test1 {

    public static void main(String[] args) {

      

       FileReader fr1 = null;

       try {

           fr1 = new FileReader("C:/a/a.txt");

           //读取文件

           char[] chars = new char[1024];//工作中常用

           int num1 = 0;

           while((num1 = fr1.read(chars)) != -1){

              System.out.println(num1);

           }

       } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }     

    }

}

4、字符输出流

构造方法:

 技术分享

 

方法使用如下:

 技术分享

 

使用如下:

  //2)使用FileWriter和FileReader实现文件的剪切功能

    public static void FileCut(){

       //获取当前时间

       long l1 = System.currentTimeMillis();

       File f1 = new File("D:\\a\\a.txt");

       //创建文件流对象

       FileWriter fw1 = null;

       FileReader fr1 = null;

       if(!f1.exists()){

           System.out.println("文件不存在!!!!");

       }else{

           //获取当前时间

           try {

              //读取文件

              fr1 = new FileReader(f1);

              fw1 = new FileWriter("D:\\b\\b.txt");

              //定义个char数组用来存储每次读取的个数

              char[] chars = new char[1204];

              int num1 = 0;

              while((num1 = fr1.read(chars)) != -1){

                  //写入D盘b目录先的a.txt

                  fw1.write(chars, 0, num1);

              }

              //将内容强制刷新到文件中

              fw1.flush();

           } catch (FileNotFoundException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

           } catch (IOException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

           }finally {

              if(fw1 != null){

                  try {

                     fw1.close();

                  } catch (IOException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

                  }

              }

              if(fr1 != null){

                  try {

                     fr1.close();

                  } catch (IOException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

                  }

              }

           }

           //删除原文件

           f1.delete();

           //获取当前时间

           long l2 = System.currentTimeMillis();

           System.out.println("使用文件字符流剪切文件总共耗时:"+(l2-l1));

       }

 

    }

 

三、缓冲流

缓冲流是不直接与文件接触的,使用方式跟节点流和字符流类似。

1、输入缓冲流:

主要体现在创建对象的时候有些区别,输入流有一个readLine() 方法,用于读取一行文本数据,这个功能相对之前的就比较有优势,各方法如下:

 技术分享

 

 

字节缓冲流创建对象如下:

BufferedInputStream reader = null;

reader = new BufferedInputStream (new FileReader("D:\\a.txt"));

字符缓冲流创建对象如下:

BufferedReader reader = null;

reader = new BufferedReader(new FileReader("D:\\a.txt"));

 

2、输出缓冲流

各方法使用如下:

 技术分享

 

3、缓冲流和处理流

内部增加了一个8kb的缓冲区在复制和读取文件的时候相对比字符节点流和字符节点流的速度更快,有效的提高了效率。

 

学习java第17天个人总结

标签:9.png   实现   创建   stream   存储   写入   leo   文本   java   

原文地址:http://www.cnblogs.com/lilinxiong/p/6758959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!