第十二章 java流与文件操作 第三讲

来源:互联网 发布:java中正则表达式语法 编辑:程序博客网 时间:2024/04/30 14:52
 

第十二章  java流与文件操作  第三讲

  三   字节流的应用  InputStream  OutputStream

      1. 字节流和字符流的区别:

    (1)字节流可以操作任何数据,字符流只能操作字符类型的数据(文本信息)

(2)字符流使用的是字符数组,字节流使用的是字节数组

 

2 . OutputStream 输出流的的应用

       FileOutputStream fos = new FileOutoutStream(“dome.txt”);//输出流的创建

       int n=12;

       byte[] arr = new byte[65,66,67,68];

       fis.write(12);//向指定的位置写入

    fis.write(arr);

    fis.write(arr,1,2);  //   1代表从指定的位置读取   2代表读取的长度

 

3.InputStream 输入流的应用

       FileInputStream fis = new FileInputStream(“rea/data.txt”); //从指定的位置读取

              //可以读取一个字节

              int n = fis.read();

System.out.printn(n);

              //可以读取多个字节  

byte[] arr = new byte[10];

int len = fis.read(arr);

for(int n=0;n<len;n++){

       System.out.println(arr[i]);

}

fis.colse();

//对读取的多个字符进行判断

int n=0;

While((n=fis.read())!= -1){

       System.out.printn(n);

                     n = fis.read();

}

 

补充:(1)在InputStream中有一个方法:available ()   作用是判断有多少个字节可读

         可用文件字节输入流的对象调用并返回 System.out.println(fis.available());

  (2)在用数组时,如果写这个函数,可以往数组中输入无线的数据。

                     例:byte arr = new byte[fis.available()];

 

 4.根据字节流可以操作图片处理的原因,可以将图片复制,以下是实例的部分代码:

 

              //复制操作---从源文件中读取,写入到目标文件中

       FileInputStream fis = null;

       FileOutputStream fos = null;

       BufferedInputStream bis = null;

       BufferedOutputStream bos = null;

       try {

              ////实例对象

           fis = new FileInputStream("res/34.jpg");

           bis = new BufferedInputStream(fis);

           fos = new FileOutputStream("res/35.jpg");

           bos = new BufferedOutputStream(fos);

           //读取

           int n=0;

           while((n=bis.read())!=-1){

              bos.write(n);

           }

       总结:图片的复制其实也是字符或字节的复制,原理是一样的。

 

5.从键盘上输入数据并保存到文件中-----System.in    它是IntputStream的对象

    System.in对应的流是字节读取流,所以在接收后要进行流的转换

 

       // 从键盘输入,并使用缓存

       InputStreamReader isr = new InputStreamReader(System.in);

           // 注:InputStreamReader----是转换流,参数是对象

        BufferedReader br = new BufferedRerder(isr);

       // 放到指定的文件中

       FileWrite fw = new FileWrite(“dome.txt”);

       BufferWriter bw = new BufferWriter(fw);

       // 对输入的数据和添加到文件中的数据进行连接

       int n;

       n = br.read(n);

       n = bw.write(br);

 

       从键盘输入并输出

         int n = System.in.read();

         System.out.println((char)n);

 

6.      转换流的使用

//把字符数据写入到磁盘文件中

       FileOutputStream fop = new FileOutputStream("res/dome2.txt");

       //把字节流转换为字符流,转换流为:OutputStream

       OutputStreamWriter osw = new OutputStreamWriter(fop);

       //前两个等价于:FileWriter fw = new FileWriter();

       //向指定文件中写入数据

       osw.write("hello");

       osw.write("中国");

       osw.flush();

       osw.close();

      

       //把数据从指定的磁盘文件中输出

       FileInputStream fis = new FileInputStream("res/dome2.txt");

       InputStreamReader isr = new InputStreamReader(fis);

       char cb[] = new char[10];

       int len=isr.read(cb);

    System.out.println(new String(cb,0,len));

补充:FileOutputStream fop = new FileOutputStream("res/dome2.txt"); OutputStreamWriter osw =new OutputStreamWriter(fop);

以上两行代码等价于:

 new FileWriter fw = new FileWriter(“res/dome2.txt”);

 不同的是因为上者可以改变编码方式,改变编码的实例为:

   OutputStreamWriter osw = new OutputStreamWriter(fop,’UTF-8’);

   注:系统默认的字符编码为:JBK,只有当输入的和输出的字符编码不一样的时候,才不会出现乱码,如果不用则会出现乱码。

原创粉丝点击