zoukankan      html  css  js  c++  java
  • Java中io流的使用合集

    1.io流概念

    流是从一端到另一端,连接了源头和目的地。
    IO流可以理解为连接程序文件/数组/网络连接/数据库

    2.io流分类

    2.1按流向分类

    输入流
    输出流

    2.2按处理数据不同分类

    字节流:二进制,可以处理一切文件,包括:纯文本、doc、音频、视频等。
    字符流:文本文件,只能处理纯文本。

    2.3按功能不同分类

    节点流:包裹源头。
    处理流:增强功能,提高性能。

    2.4按操作方式分类

     2.5按操作对象分类

     3.操作流的步骤

    (1)创建源
    (2)选择流
    (3)操作流(读取|写出)
    (4)释放资源

    4.具体流的操作案例

     4.1文件的操作File

    创建file对象有三种方式:

    1)new File(String pathname)

    2)new File (String parent,String child)

    3)new File(File parent,String child) 

    File file=new File("C:\Users\eric.fang\Desktop","zxh_cat_movie.sql");

    File的常用方法:

    1.创建文件 file.createNewFile();

    2.创建文件夹 file.mkdir()  //只能创建一级目录

    3.创建多级文件夹file.mkdirs()

    4.删除文件或者空目录  file.delete()  

    File类的判断方法:

    绝对路径:从根上找的路径

    相对路径:相对某个点找的

    1.file.exists() 判断指定路径的文件夹或文件是否存在

    2.file.isAbsolute() 判断当前路径是否是绝对路径

    3.file.isDirectory() 判断是否是目录

    4.file.isFile() 判断当前路径是否是一个文件

    5.file.isHidden() 判断当前路径是否是隐藏文件

     File类的其他方法:

    1.file.getAbsoluteFile();   把文件变成绝对路径下创建的文件

    2.file.getAbsolutePath()   获取文件的绝对路径

    3.file.getParent()   获取当前路径的父级路径

    4.file.getParentFile()  获取当前路径的父级路径,以file的形式返回该父级路径

    5.file.getName()   获取文件或文件夹的名称

    6.file.getPath()    获取file对象中封装的路径

    7.file.lastModified()  以毫秒值返回最后修改时间

    8.file.length()  返回文件的字节数

    9.file.renameTo(File dest)  将当前file对象所指向的路径修改为指定file所指向的路径,相当于移动

    10.file.list()   获取到当前目录下所有的文件和文件夹

    11.file.listFiles()   获取到当前目录下所有的文件和文件夹,以file类型返回

     案例

    打印出某个文件夹下所有以.Java结尾文件的个数

        public static void main(String[] args) throws IOException {
            File file=new File("D:\idea_code");
            int count = getCount(file);
            System.out.println(count);
        }
        public static int getCount(File file) {
            int count = 0;
            //获取所有的子目录
            File[] files = file.listFiles();
            //遍历
            for (File newFile : files) {
                //如果是文件,获取文件名,判断是否.Java结尾
                if (newFile.isFile()) {
                    //如果是count++
                    if (newFile.getName().endsWith(".java")) {
                        count++;
                    }
                } else {
                    //如果是文件夹getCount
                    int count1 = getCount(newFile);
                    count += count1;
                }
            }
            return count;
        }

    4.2字节流与字符流

    4.2.1概述

    InputStream/OutputStream

    4.2.2InputStream

    InputStream 是输入流的顶层类,不过是一个抽象类

    fileInputStream类的使用

        public static void main(String[] args) {
            try {
                InputStream fis=new FileInputStream("C:\Users\eric.fang\Desktop\zxh_cat_movie.sql");
                byte[] b=new byte[1024];
                int len=0;
                while ((len=fis.read(b))!=-1) {
                    String s=new String(b,0,len);
                    System.out.println(s);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    4.2.3OutputStream

    OutputStream 是输出流的顶层类

    fileOutputStream类的使用

        public static void main(String[] args) {
            try {
                OutputStream os=new FileOutputStream("C:\Users\eric.fang\Desktop\zxh_cat_movie.sql");
                byte[] bytes="asadad".getBytes();
                os.write(bytes);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    输入流和输出流同时使用(边读边写)

        public static void main(String[] args) {
            InputStream is=null;
            OutputStream os=null;
            try {
                is=new FileInputStream("C:\Users\eric.fang\Desktop\zxh_cat_movie.sql");
                os=new FileOutputStream("C:\Users\eric.fang\Desktop\hello.txt");
                int len=0;
                byte[] b=new byte[1024];
                while ((len=is.read(b))!=-1){
                    os.write(b,0,len);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    4.2.4IO流之字符流操作

    输入流:Reader

    输出流:Writer

    4.2.5Reader

    Reader是输入流的顶层类,不过是一个抽象类

    fileReader类的使用

        public static void main(String[] args) {
            Reader reader=null;
            try {
                reader=new FileReader("C:\Users\eric.fang\Desktop\zxh_cat_movie.sql");
                int len=0;
                char[] c=new char[1024];
                while ((len=reader.read(c))!=-1){
                    String s=new String(c,0,len);
                    System.out.println(s);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    4.2.6Writer

    Writer是输出流的顶层类,不过是一个抽象类

    fileWriter类的使用

        public static void main(String[] args) {
            Reader reader=null;
            Writer writer=null;
            try {
                reader=new FileReader("C:\Users\eric.fang\Desktop\zxh_cat_movie.sql");
                writer=new FileWriter("C:\Users\eric.fang\Desktop\hello.txt");
                int len=0;
                char[] c=new char[1024];
                while ((len=reader.read(c))!=-1){
                    writer.write(c,0,len);
                }
                writer.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    reader.close();
                    writer.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    4.3标准输入流和标准输出流

    标准输入流:System.in

    标准输出流:System.out

    4.4标准打印流

    标准打印流:PrintStream

        public static void main(String[] args) {
            try {
                PrintStream ps=new PrintStream("C:\Users\eric.fang\Desktop\hello.txt");
                ps.print("hhhhhhhhh");
                ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    4.5转换流

    转换流:可以将字节流转换成字符流,不可逆转

    OutputStreamWriter

                OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("a.txt"));
                InputStreamReader isw=new InputStreamReader(new FileInputStream("a.txt"));

    4.6高效流

    高效字节流:BufferedInputStream BufferedOutputStream

    高效字符流:BufferedWriter  BufferedReader

    高效字符流可以一次性读取一行

        public static void main(String[] args) {
            BufferedWriter bw=null;
            BufferedReader br=null;
            try {
                bw=new BufferedWriter(new FileWriter("a.txt"));
                br=new BufferedReader(new FileReader("C:\Users\eric.fang\Desktop\zxh_cat_movie.sql"));
                String s=null;
                while ((s=br.readLine())!=null){
                    bw.write(s);
                    bw.newLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    br.close();
                    bw.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    4.7对象流

    序列化流 ObjectOutputStream  

    序列化:把对象持久化到本地

        public static void main(String[] args) {
            ObjectOutputStream oos=null;
            try {
                oos=new ObjectOutputStream(new FileOutputStream("a.txt"));
                oos.writeObject(new Student("tom",20));
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    oos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    反序列化流: ObjectInputStream

        public static void main(String[] args) {
            ObjectInputStream ois=null;
            try {
                ois=new ObjectInputStream(new FileInputStream("a.txt"));
                Student student = (Student)ois.readObject();
                System.out.println(student);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    ois.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    一点点学习,一丝丝进步。不懈怠,才不会被时代所淘汰!

  • 相关阅读:
    【应用】Linux内存调试工具:valgrind
    Python UnboundLocalError: local variable 'xxx' referenced before assignment 解决方法
    MYSQL连接时错误码2059解决办法
    Python encode()、decode()方法详解
    genymotion自动化使用
    论文数据集
    Native Apps、Web Apps和Hybrid Apps
    C# 中如何进行私有(private)函数测试
    Windows10 计划任务开始失败
    如果系统盘后面是恢复盘,如何扩充系统盘
  • 原文地址:https://www.cnblogs.com/fqh2020/p/14738497.html
Copyright © 2011-2022 走看看