• Io流


    / 创建一个文件对象
            File file = new File("e:/" + fileName + ".txt"); // 文件不存在
            if (file.exists()) {// 判断文件是否存在
                System.out.println("该文件已经存在!");
            } else { // 文件不存在
                boolean flag = file.createNewFile(); // 创建文件
                if (flag) {
                    System.out.println("创建成功!");
                    // 显示文件的详情
                    showFile("e:/" + fileName + ".txt");
                } else {
                    System.out.println("创建失败!");
                }
            }
    //创建一个目录(dir)
         System.out.println("请您输入创建的目录名称: 默认在E:/");
            String dir = input.next();
            // 创建文件对象
            File file = new File("e:/" + dir);
            boolean flag = file.mkdirs(); // 可以同时创建多个文件夹
            if (flag) {
                System.out.println("创建成功");
            } else {
                System.out.println("创建失败");
            }

    //同时创建多个文件夹
    System.out.println("请您输入创建的目录名称: 默认在E:/");
            String dir = input.next();
            // 创建文件对象
            File file = new File("e:/" + dir);
            boolean flag = file.mkdirs(); // 可以同时创建多个文件夹
            if (flag) {
                System.out.println("创建成功");
            } else {
                System.out.println("创建失败");
            }
            showFirstMenu();
        }
    复制代码

       02.获取文件的大小,路径,文件类型

    // 创建文件对象
            File file = new File(fileName);
            System.out.println("文件的名称:" + file.getName());
            System.out.println("文件的大小:" + file.length());
            System.out.println("文件的路径" + file.getAbsolutePath());

         03.删除文件或者目录

    复制代码
            // 创建文件对象
            File file = new File("e:/" + deleteFileName + ".txt");
            if (file.exists()) { // 文件存在就删除
                boolean flag = file.delete();
                if (flag) {
                    System.out.println("删除成功====!");
                } else {
                    System.out.println("删除失败====!");
                }
            } else {
                System.out.println("您要删除的文件不存在,请重新选择!");
            }
    复制代码

       04.获取目录下所有的文件列表

    复制代码
         /**
         * 查询目录下所有的文件列表
         * @throws IOException 
         */
        private static void showFiles() throws IOException {
            System.out.println("请输入目录的名称:默认E:/");
            String dirName = input.next();
            // 创建文件对象
            File file = new File("e:/" + dirName);
            File[] files = file.listFiles();
            int dirNums = 0;
            int fileNums = 0;
            for (File f : files) {
                if (f.isDirectory()) { // 证明是目录
                    dirNums++;
                }
                if (f.isFile()) { // 证明是文件
                    fileNums++;
                }
            }
            System.out.println("目录" + dirName + "下面有几个文件夹?" + dirNums);
            System.out.println("目录" + dirName + "下面有几个文件?" + fileNums);
            showFirstMenu();
    复制代码

       05.重命名文件/目录

    复制代码
        /**
         *  修改文件名称
         */
        private static void renameFile() {
            System.out.println("请输入原文件的名称:");
            String oldName = input.next();
            System.out.println("请输入新文件的名称:");
            String newName = input.next();
            // 创建文件对象
            File oldFile = new File("e:/" + oldName + ".txt");
            File newFile = new File("e:/" + newName + ".txt");
            if (oldFile.renameTo(newFile)) {
                System.out.println("重命名成功!");
            } else {
                System.out.println("重命名失败!");
            }
    
        }
    复制代码

    2.IO  inputStream   outputStream    相对于内存

      所有的输入流都有一个read()   读取数据

      所有的输出流都有一个write()   写入数据!而且每个输出流对象的构造  方法都有第二个参数===》是否覆盖之前的内容    (第二个参数为boolean类型,默认为false,新内容会覆盖原内容;若为true在原内容后追加新内容)

      我们所谓的输入输出流 都是单向的!

      01.字节流

        基类   InputStream     OutputStream

      

    复制代码
    /**
     * 字节  输入流的操作
     *        1  byte  =  8  bit
     *      stream.read()
     *      从流中 读取一个8位的字节,然后返回0-255之间的一个整数!
     *      如果流读取到了最后,会返回-1
     *     
     *      输出流的操作   
     *      OutputStream 本身没有设置缓存区
     *      它的子类中有   比如   BufferedOutputStream   PrintStream
     *  
     */
    public class InputOutputStreamDemo {
    
        public static void main(String[] args) {
            InputStream is = null;
            OutputStream os = null;
            try {
                // 创建输入流对象
                is = new FileInputStream("E:/heihei.txt");
                // 创建输出流对象
                os = new FileOutputStream("E:/heihei.txt", true);
                // 写入内容
                os.write("654321".getBytes());
                os.flush();// 清理缓冲区 没有效果
                System.out.println("可读取的字节数:" + is.available());
                // 获取文件中的内容
                int temp = 0;
                while ((temp = is.read()) != -1) { // -1代表文件没有内容
                    System.out.print((char) temp); // 中文乱码
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally { // 释放资源
                try {
                    is.close();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
    复制代码

      02. 字符流 

        基类 Reader  Writer

                   缓冲的基类  BudfferedReader      BufferedWriter

    复制代码
    //Reader  Writer
    public static void main(String[] args) {
            Reader reader = null;
            Writer writer = null;
            try {
                // 创建对应输出流对象
                writer = new FileWriter("E:/heihei.txt");
                // 写入内容
                writer.write("大家辛苦了zhende?");
                writer.close();
                // 创建对应输入流对象
                reader = new FileReader("E:/heihei.txt");
                // 创建一个char类型的数组 来保存 输入流中的数据
                char[] data = new char[1024];
                int temp = 0;
                StringBuffer sb = new StringBuffer();
                while ((temp = reader.read(data)) != -1) {
                    sb.append(data);
                }
                System.out.println(sb.toString()); // 输出数据
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally { // 释放资源
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    //BufferedReader BufferedWriter
    public static void main(String[] args) { Reader in = null; Writer out = null; BufferedReader br = null; BufferedWriter bw = null; try { // 创建输出流对象 out = new FileWriter("E:/heihei.txt", true); bw = new BufferedWriter(out); bw.write("第7行77777"); bw.newLine();// 文件中换行 bw.flush(); // 清理缓冲区 bw.write("第8行88888"); bw.write("第9行99999"); bw.close(); // 关闭输出流 out.close(); in = new FileReader("E:/heihei.txt"); // 创建缓冲区输入流对象 br = new BufferedReader(in); // 创建字符串保存数据 StringBuffer sb = new StringBuffer(); String line = null; // 逐行读取 while ((line = br.readLine()) != null) { sb.append(line + " "); } // 输出内容 System.out.println("文件的内容是:" + sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
    复制代码

      03. 二进制流  (图片,视频,音频......)

               基类  DataInputStream    DdataOutputStream

    复制代码
    public static void main(String[] args) {
            InputStream in = null;
            OutputStream out = null;
            DataInputStream dis = null;
            DataOutputStream dos = null;
            try {
                // 创建输入流对象
                in = new FileInputStream("E:/u1/cat.jpg");
                dis = new DataInputStream(in);
                // 创建输出流对象
                out = new FileOutputStream("E:/maomi.jpg");
                dos = new DataOutputStream(out);
                // 创建中间变量
                int temp = 0;
                while ((temp = dis.read()) != -1) {
                    // 输出到新的位置
                    dos.write(temp);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally { // 释放资源
                try {
                    dos.close();
                    dis.close();
                    out.close();
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
  • 相关阅读:
    sell02 展现层编写
    sell01 环境搭建、编写持久层并进行测试
    SpringBoot04 日志框架之Logback
    SpringBoot04 项目热部署详解
    SpringBoot03 项目热部署
    Angular14 利用Angular2实现文件上传的前端、利用springBoot实现文件上传的后台、跨域问题
    Flask17 Flask_Script插件的使用
    Angular13 Angular2发送PUT请求在后台接收不到参数
    PostMan安装
    unix网络编程环境配置程序运行
  • 原文地址:https://www.cnblogs.com/WillimTUrner/p/8782959.html
Copyright © 2020-2023  润新知