zoukankan      html  css  js  c++  java
  • Java基础之(IO流)

      

    简介:

      流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

    一、File类

      java.io.File类:文件和目录路径名的抽象表示形式,与平台无关 File ,能新建、删除、重命名文件和目录。但 File 不能访问文件内容本身,如果需要访问文件内容本身,则需要使用输入/输出流。

      File对象可以作为参数传递给流的构造函数

      File类的常见构造方法:

      1、public File(String pathname) 以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。

      2、public File(String parent,String child) 以parent为父路径,child为子路径创建File对象。

    package main.dyh;
    
    
    import org.junit.Test;
    
    import java.io.File;
    import java.io.IOException;
    
    public class TestFile {
    
        //文件夹操作
        @Test
        public void fun1(){
            File file1 = new File("D:\aa");
            boolean mkdir = file1.mkdir();//创建文件夹,D盘之前是没有文件夹aa的
            File file2 = new File("D:\aa","bb");
            boolean mkdirs = file2.mkdirs();//创建文件夹,并在文件夹中创建子文件夹bb
    
            File file3 = new File("D:\");
            String[] list = file3.list();//查询D盘中所有目录和文件
            for (String s : list) {
                System.out.println(s);
            }
    
            File file4 = new File("D:\");
            File[] files = file4.listFiles();//查询D盘中所有文件和文件夹,并显示路径
            for (File file : files) {
                System.out.println(file);
            }
        }
    
        //文件操作
        @Test
        public void fun2() throws IOException {
            File file1 = new File("D:\a.txt");
            boolean newFile = file1.createNewFile();//创建文件,注意的是,如果把该文件建在一个文件夹中,该文件夹必须存在,或者报异常,如果之前有该文件,会覆盖
    
            //-------------------------------
            File file2 = new File("D:\a.txt");
            String name = file2.getName();//获得该文件的完整名称,包括后缀
            System.out.println(name);
    
            File file4 = new File("./a.txt");
            File absoluteFile = file4.getAbsoluteFile();
            System.out.println(absoluteFile);
    
    //     -------------------------------------------
            File filep = new File(".\test1.txt");
            File filea = new File("D:\workspace\test\test1.txt");
            System.out.println("-----默认相对路径:取得路径不同------");
            System.out.println(filep.getPath());
            System.out.println(filep.getAbsolutePath());
            System.out.println("-----默认绝对路径:取得路径相同------");
            System.out.println(filea.getPath());
            System.out.println(filea.getAbsolutePath());
    
    
    
    
            File file6 = new File("D:\a\a.txt");
            String parent = file6.getParent();//得到父目录
            System.out.println(parent);
    
            File file3 = new File("D:\a\a.txt");
            boolean nfile = file3.renameTo(new File("D:\a\b.txt"));
    
            File file7 = new File("D:\b.txt");
            boolean newFile1 = file7.createNewFile();
            boolean delete = file7.delete();//删除指定文件
        }
    
        /**
         * 文件检测
         */
        @Test
        public void fun3() {
            File file1 = new File("D:\a.txt");
            boolean exists = file1.exists();//文件是否存在
            System.out.println(exists);
    
            boolean bread = file1.canRead();//文件是否可读
            System.out.println(bread);
            boolean bwrite = file1.canWrite();//文件是否可写
            System.out.println(bwrite);
    
            boolean file = file1.isFile();
            System.out.println(file);
            boolean directory = file1.isDirectory();//是否是文件夹
            System.out.println(directory);
    
            long length = file1.length();
            System.out.println(length);
    
            long l = file1.lastModified();//返回此抽象路径名表示的文件最后一次被修改的时间
            System.out.println(l);
        }
    }
    View Code

    二、什么是IO流

      IO流用来处理设备之间的数据传输。 Java程序中,对于数据的输入/输出操作以”流(stream)” 的方式进行。

      java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。

      1、输入流与输出流概念

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

      输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中

      2、流的分类

      按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)。

      按数据流的流向不同分为:输入流,输出流。

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

      Java的IO流共涉及40多个类,实际上非常规则,都是从如下4个抽象基类派生的。 由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。

     

       处理流与节点流的区别:

      节点流:节点流可以从一个特定的数据源读写数据。

      处理流:处理流是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。

      3、IO流的体系

    三、IO流的读写操作

      1、InputStream 和 Reader 是所有输入流的基类。

      InputStream(典型实现:FileInputStream)

      int read()

      int read(byte[] b)

      int read(byte[] b, int off, int len)

      Reader(典型实现:FileReader)

      int read() int read(char [] c)

      int read(char [] c, int off, int len)

      程序中打开的文件 IO 资源不属于内存里的资源,垃圾回收机制无法回收该资源,所以应该显式关闭文件 IO 资源。

    package main.dyh;
    
    import org.junit.Test;
    
    import java.io.*;
    
    public class TestOutIn {
    
        @Test
        public void fun1() {
            FileReader fileReader = null;
            try {
                fileReader = new FileReader(new File("D:\a.txt"));
                char[] c = new char[1024];
                int len = 0;
                while ((len = fileReader.read(c)) != -1){
                    System.out.println(new String(c,0,len));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
        @Test
        public void fun2() {
            FileInputStream fs = null;
            try {
                fs = new FileInputStream(new File("D:\a.txt"));
                byte[] b = new byte[1024];
                int len = 0;
                while ((len = fs.read(b)) != -1){
                    System.out.println(new String(b,0,len));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
    }
    View Code

      2、OutputStream 和 Writer 也非常相似: (典型实现:FileOutputStream)

      void write(int b/int c);

      void write(byte[] b/char[] cbuf);

      void write(byte[] b/char[] buff, int off, int len);

      void flush();

      void close();

      需要先刷新,再关闭此流,因为字符流直接以字符作为操作单位,所以 Writer 可以用字符串来替换字符数组,即以 String 对象作为参数 :

      void write(String str);

      void write(String str, int off, int len);

    package main.dyh;
    
    import org.junit.Test;
    
    import java.io.*;
    
    public class TestInput {
    
        @Test
        public void fun1(){
            FileWriter fileWriter = null;
            try {
                fileWriter = new FileWriter(new File("D:\a.txt"));
                fileWriter.write("HelloWorld!!");
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        @Test
        public void fun2(){
            FileOutputStream fo = null;
            try {
                fo = new FileOutputStream(new File("D:\a.txt"));
                fo.write("woaibeijingtiananmen".getBytes());
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    fo.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    View Code

    四、处理流

      1、缓冲流

      为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组。

      根据数据操作单位可以把缓冲流分为:

      BufferedInputStream 和 BufferedOutputStream

      BufferedReader 和 BufferedWriter

      缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法 对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush()将会使内存中的数据立刻写出。

    package main.dyh;
    
    import org.junit.Test;
    
    import java.io.*;
    
    public class TestBuffer {
    
        @Test
        public void fun1(){
            BufferedOutputStream bo = null;
            BufferedInputStream bi = null;
            try {
                bo = new BufferedOutputStream(new FileOutputStream("D:\d.txt"));
                bi = new BufferedInputStream(new FileInputStream("D:\d.txt"));
    
               bo.write("zhonghuarenming".getBytes());//把数据写到硬盘上
    
               byte[] b = new byte[1024];
               int len = 0;
               while ((len = bi.read(b)) != -1){//把数据读到内存中
                   System.out.println(new String(b,0,len));
               }
               bo.flush();//刷新缓冲区
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    bi.close();
                    bo.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    View Code

      2、转换流

      转换流提供了在字节流和字符流之间的转换

      Java API提供了两个转换流: InputStreamReader和OutputStreamWriter 字节流中的数据都是字符时,转成字符流操作更高效。

      (1)、InputStreamReader 用于将字节流中读取到的字节按指定字符集解码成字符。

      需要和InputStream“套接”。

      构造方法 public InputStreamReader(InputStream in) public InputSreamReader(InputStream in,String charsetName)

      (2)、OutputStreamWriter 用于将要写入到字节流中的字符按指定字符集编码成字节。

      需要和OutputStream“套接”。

      构造方法 public OutputStreamWriter(OutputStream out) public OutputSreamWriter(OutputStream out,String charsetName)

    package main.dyh;
    
    import org.junit.Test;
    
    import java.io.*;
    
    public class TestInReader {
        @Test
        public void fun1(){
            FileInputStream fi = null;
            FileOutputStream fo = null;
            try {
                fi = new FileInputStream("D:\a.txt");
                fo = new FileOutputStream("D:\aa.txt");
    
                InputStreamReader ir = new InputStreamReader(fi,"GBK");//转换流
                OutputStreamWriter or = new OutputStreamWriter(fo,"GBK");
    
                BufferedReader br = new BufferedReader(ir);//缓冲流
                BufferedWriter bw = new BufferedWriter(or);
    
                bw.write("你好世界!!!");
    
                char[] chars = new char[1024];
                int len = 0;
                while ((len = br.read(chars)) != -1){
                    System.out.println(new String(chars,0,len));
                }
                bw.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
    
                try {
                    fo.close();
                    fi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    View Code

      

      3、字符编码

      编码表的由来 计算机只能识别二进制数据,早期由来是电信号。为了方便应用计算机,让它可以识别各个国家的文字。就将各个国家的文字用数字来表示,并一一对应,形成一张表。这就是编码表。

      常见的编码表 ASCII:美国标准信息交换码。 用一个字节的7位可以表示。

      ISO8859-1:拉丁码表。欧洲码表 用一个字节的8位表示。

      GB2312:中国的中文编码表。

      GBK:中国的中文编码表升级,融合了更多的中文文字符号。

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

      UTF-8:最多用三个字节来表示一个字符。

      编码:字符串—>字节数组

      解码:字节数组—>字符串

        转换流的编码应用

      可以将字符按指定编码格式存储。

      可以对文本数据按指定编码格式来解读。

       指定编码表的动作由构造器完成。

  • 相关阅读:
    Aspose.Words三 创建表格
    Aspose.words一 DOM结构
    Aspose.words四 bookmark
    Aspose.Words五 MergeField
    为什么使用Reazor
    C#实现发送给QQ邮件
    T4模板之基础篇
    avalon子孙元素属性监听
    C#设置WebBrowser IE浏览器版本
    【GoLang】GoLang GOPATH 工程管理 最佳实践
  • 原文地址:https://www.cnblogs.com/soft2018/p/10317148.html
Copyright © 2011-2022 走看看