zoukankan      html  css  js  c++  java
  • IO流

    java.io.File类:文件和路径名的抽象表示形式,与平台无关

    File能新建、删除、重命名文件和文件夹,单file不能访问文件的内容,如果需要访问文件的内容,则要使用输入、输出流

    file可以作为参数传递给流的构造函数

     

    File的常用构造函数:

     

    File的常用方法:

    查看文件的属性:

     

     验证文件、文件夹是否存在:

    获取文件、文件的信息

     

     与文件相关的操作

     

    与目录相关的操作

     

     

    java io的原理:

    IO流用来处理设备之间的数据传输

    在java程序中,对于数据的输入、输出操作以流的方式进行

    java.io包下提供了各种的类和接口,以获取不同种类的数据,并通过标准的方法进行输入、输出操作

     

    输入InputStream、Reader:读取外部的数据(磁盘、光盘等外部设备的数据)到程序(内存中)

    输出OutputStream、Writer:将程序(内存)中的数据输出到磁盘、光盘等外部设备

     

    流的分类::

    按操作数据单位的不同分为:字节流(8个字节)、字符流(16个字节)

    按数据的流向分为:输入流、输出流

    按流的角色不同:节点流,处理流

     

     

     InputSteam和Reader是输入流的基类

    InputStream

     1 public void fileInputStream() throws Exception {
     2 
     3         // 1.创建一个File类的对象。
     4         File file = new File("hello.txt");
     5 
     6         // 2.创建一个FileInputStream类的对象
     7         FileInputStream fis = new FileInputStream(file);
     8 
     9         // 3.调用FileInputStream的方法,实现file文件的读取。
    10         /*
    11          * read():读取文件的一个字节。当执行到文件结尾时,返回-1
    12          */
    13         // int b = fis.read();
    14         // while(b != -1){
    15         // System.out.print((char)b);
    16         // b = fis.read();
    17         // }
    18 
    19 
    20         int b;
    21         while ((b = fis.read()) != -1) {
    22             System.out.print((char) b);
    23         }
    24 
    25         // 4.关闭相应的流
    26         fis.close();
    27     }

     

    public void  fileOutputStream() {
            // 1.创建一个File对象,表明要写入的文件位置。
            // 输出的物理文件可以不存在,当执行过程中,若不存在,会自动的创建。若存在,会将原有的文件覆盖
            File file = new File("hello2.txt");
    
            // 2.创建一个FileOutputStream的对象,将file的对象作为形参传递给FileOutputStream的构造器中
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(file);
                // 3.写入的操作
                fos.write(new String("I love China!").getBytes());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 4.关闭输出流
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
     1 public void copy() {
     2         // 1.提供读入、写出的文件
     3         File file1 = new File("");
     4         File file2 = new File("");
     5         // 2.提供相应的流
     6         FileInputStream fis = null;
     7         FileOutputStream fos = null;
     8         try {
     9             fis = new FileInputStream(file1);
    10             fos = new FileOutputStream(file2);
    11             // 3.实现文件的复制
    12             byte[] b = new byte[20];
    13             int len;
    14             while ((len = fis.read(b)) != -1) {
    15                 // fos.write(b);//错误的写法两种: fos.write(b,0,b.length);
    16                 fos.write(b, 0, len);
    17             }
    18         } catch (Exception e) {
    19             e.printStackTrace();
    20         } finally {
    21             if (fos != null) {
    22                 try {
    23                     fos.close();
    24                 } catch (IOException e) {
    25                     e.printStackTrace();
    26                 }
    27             }
    28             if (fis != null) {
    29                 try {
    30                     fis.close();
    31                 } catch (IOException e) {
    32                     e.printStackTrace();
    33                 }
    34             }
    35 
    36         }
    37     }

     

     

     

     

    Reader

     

     1 public void fileReader(){
     2         FileReader fr = null;
     3         try {
     4             File file = new File("");
     5             fr = new FileReader(file);
     6             char[] c = new char[24];
     7             int len;
     8             while((len = fr.read(c)) != -1){
     9                 String str = new String(c, 0, len);
    10                 System.out.print(str);
    11             }
    12         }catch (IOException e) {
    13             // TODO Auto-generated catch block
    14             e.printStackTrace();
    15         }finally{
    16             if(fr != null){
    17                 try {
    18                     fr.close();
    19                 } catch (IOException e) {
    20                     // TODO Auto-generated catch block
    21                     e.printStackTrace();
    22                 }
    23             }
    24         }
    25         
    26     }

     

     OutputStream

     

     

    因为字符流直接以字符进行操作单位,所以Writer可以用字符串来替换字符数组,即以String作为参数

     

     1 public void  fileReaderWriter(){
     2         //1.输入流对应的文件src一定要存在,否则抛异常。输出流对应的文件dest可以不存在,执行过程中会自动创建
     3         FileReader fr = null;
     4         FileWriter fw = null;
     5         try{
     6             //不能实现非文本文件的复制
     7 //            File src = new File("");
     8 //            File dest = new File("");
     9             File src = new File("");
    10             File dest = new File("");
    11             //2.
    12             fr = new FileReader(src);
    13             fw = new FileWriter(dest);
    14             //3.
    15             char[] c = new char[24];
    16             int len;
    17             while((len = fr.read(c)) != -1){
    18                 fw.write(c, 0, len);
    19             }
    20         }catch(Exception e){
    21             e.printStackTrace();
    22         }finally{
    23             if(fw != null){
    24                 try {
    25                     fw.close();
    26                 } catch (IOException e) {
    27                     // TODO Auto-generated catch block
    28                     e.printStackTrace();
    29                 }
    30             }
    31             if(fr != null){
    32                 try {
    33                     fr.close();
    34                 } catch (IOException e) {
    35                     // TODO Auto-generated catch block
    36                     e.printStackTrace();
    37                 }
    38             }
    39         }
    40     }

     

     1         /*
     2      * 实现字节流与字符流之间的转换:
     3      * 转换流:InputStreamReader  OutputStreamWriter
     4      * 编码:字符串  --->字节数组
     5      * 解码:字节数组--->字符串
     6      */
     7     @Test
     8     public void test1(){
     9         BufferedReader br = null;
    10         BufferedWriter bw = null;
    11         try {
    12             //解码
    13             File file = new File("dbcp.txt");
    14             FileInputStream fis = new FileInputStream(file);
    15             InputStreamReader isr = new InputStreamReader(fis, "GBK");
    16             br = new BufferedReader(isr);
    17             //编码
    18             File file1 = new File("dbcp4.txt");
    19             FileOutputStream fos = new FileOutputStream(file1);
    20             OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
    21             bw = new BufferedWriter(osw);
    22             String str;
    23             while((str = br.readLine()) != null){
    24                 bw.write(str);
    25                 bw.newLine();
    26                 bw.flush();
    27             }
    28         }catch (IOException e) {
    29             e.printStackTrace();
    30         }finally{
    31             if(bw != null){
    32                 try {
    33                     bw.close();
    34                 } catch (IOException e) {
    35                     e.printStackTrace();
    36                 }
    37                 
    38             }
    39             if(br != null){
    40                 try {
    41                     br.close();
    42                 } catch (IOException e) {
    43                     e.printStackTrace();
    44                 }
    45                 
    46             }
    47         }
    48         
    49     }        

    常见的字符编码:

    ASCII:美国标准的信息交换码,用一个字节的7位表示

    ISO-8859-1:拉丁码表,欧洲码表,用一个字节的8位表示

    GB2312:中国的中文编码表

    GBK:中国的中文编码表

    Unicode:国际标准码,融合了多种文字,所有文字都用两个字节来表示,java语言使用的就是这个编码

    UTF-8:最多使用3个字节来表示一个字符

    转换流的编码使用:

    可以将字符按指定格式的编码进行存储

    可以对字符按指定格式的编码进行解读,有构造函数完成

    标准的输入、输出流:

    System.in和System.out分别代表了系统的标准输入和输出设备

    System.in的类型是InputStream,System.out的类型是PrintStream

    通过System.setIn(), System.setOut()方法对默认设备进行更改

     1 public void demo(){
     2         BufferedReader br = null;
     3         try {
     4             InputStream is = System.in;
     5             InputStreamReader isr = new InputStreamReader(is);
     6             br = new BufferedReader(isr);
     7             String str;
     8             while(true){
     9                 System.out.println("请输入字符串:");
    10                 str = br.readLine();
    11                 if(str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")){
    12                     break;
    13                 }
    14                 String str1 = str.toUpperCase();
    15                 System.out.println(str1);
    16             }
    17         } catch (IOException e) {
    18             e.printStackTrace();
    19         }finally{
    20             if(br != null){
    21                 try {
    22                     br.close();
    23                 } catch (IOException e) {
    24                     // TODO Auto-generated catch block
    25                     e.printStackTrace();
    26                 }
    27                 
    28             }
    29         }
    30     }



     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    跟我学android—01.SplashActivity
    eclipse android : A project with that name already exists in the workspace
    冬吴相对论锦言佳句--0005.薄伽梵歌与“印度式管理”
    [转载]Jquery Form插件表单参数
    Form表单学习网站
    [转载]JQuery获取元素文档大小、偏移和位置和滚动条位置的方法集合
    jquery捕捉文本域输入事件
    [转载]js 遍历数组对象
    C#跳出循环的几种方法的区别
    [转载]MVC3缓存:使用页面缓存
  • 原文地址:https://www.cnblogs.com/lzb0803/p/8954962.html
Copyright © 2011-2022 走看看