JavaSE:16、Java IO

学习 资源1

学习资源 2

1、文件字节流


    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {
         //try-with-resource语法,自动close
            try(FileInputStream file = new FileInputStream("E:/text.txt")) {
                System.out.println("");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

读入


    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {
         //try-with-resource语法,自动close
            try(FileInputStream file = new FileInputStream("E:/text.txt")) {
           int i;
            while((i=file.read())!=-1) {  //读一个字符,没有返回-1
                System.out.print((char) i);
            }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {
         //try-with-resource语法,自动close
            try(FileInputStream file = new FileInputStream("E:/text.txt")) {
          byte[] b = new byte[3];
          while(file.read(b)!=-1)   //一次读入三个字符
          {
              System.out.println(new String(b));
          }
             //   Hel
                //   lo
               // Wor
                 //    ldr
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {
         //try-with-resource语法,自动close
            try(FileInputStream file = new FileInputStream("E:/text.txt")) {
          byte[] b = new byte[file.available()];//返回有多少个字节可读
          file.read(b);
              System.out.println(new String(b));
             //Hello World
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

输出


    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {
         //覆盖文件原有内容
            try(FileOutputStream file = new FileOutputStream("E:/text.txt")) {
            file.write("hello".getBytes());//字符串转为byte数组才行
            file.write("world".getBytes(),2,3);
            //hellorld;
            file.flush(); //刷新
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {
         //在文件后面接着写

            try(FileOutputStream file = new FileOutputStream("E:/text.txt",true)) {
            file.write("hello".getBytes());//字符串转为byte数组才行
            file.write("world".getBytes(),2,3);
            //hellorldhellorld
            file.flush(); //刷新
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

文件拷贝


    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {

            try(FileOutputStream outputStream = new FileOutputStream("E:/textout.txt");
                FileInputStream inputStream = new FileInputStream("E:/text.txt")) {
                byte[] bytes = new byte[10];    //使用长度为10的byte[]做传输媒介
                int tmp;   //存储本地读取字节数
                while ((tmp = inputStream.read(bytes)) != -1) {   //直到读取完成为止
                    outputStream.write(bytes, 0, tmp);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

2、文件字符流

读纯文本文件,一次读一个字符,中英文均可

读入


    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {

            try(FileReader reader = new FileReader(new File("src/in.txt"))) {
            System.out.println(reader.read());//你
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

输出


    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {

            try(FileWriter  writer = new FileWriter("src/our.txt");) {
            writer.write("Hello World");//不用转为byte数组
                writer.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

拷贝文件


    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {

            try(FileWriter  writer = new FileWriter("src/out.txt");
               FileReader reader = new FileReader("src/in.txt")) {
             char[]  c=new char[100];
             int len;
             while((len=reader.read(c))!=-1)
                 writer.write(c,0,len);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

3、文件对象


    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {
           File file=new File("src/in.txt");
           System.out.println(file.exists());//true
           System.out.println(file.isFile());//true
           System.out.println(file.getAbsolutePath());//该文件的绝对路径

        }
    }

    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args) throws IOException {
           File file=new File("filetest");
           file.mkdir();//创建文件夹
           file=new File("filetest/test.txt");
           file.createNewFile();//创建文件
        }
    }

文件拷贝+进度条


    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args) throws IOException {
           File file=new File("src/in.txt");
           try(FileInputStream in=new FileInputStream(file);
            FileOutputStream out=new FileOutputStream("src/out.txt")) {
            byte[]  by=new byte[20];
            long len=file.length();
            long sum=0;  int chalen;
            while((chalen=in.read(by))!=-1)
            {
                sum+=chalen;
                out.write(by,0,chalen);
                System.out.println("已拷贝:"+(double)sum/len*100+"%");
//                已拷贝:12.658227848101266%
//                    已拷贝:25.31645569620253%
//                    已拷贝:37.9746835443038%
//                    已拷贝:50.63291139240506%
//                    已拷贝:63.29113924050633%
//                    已拷贝:75.9493670886076%
//                    已拷贝:88.60759493670885%
//                    已拷贝:100.0%
            }
           }catch (Exception e)
           {
               e.getStackTrace();
           }
        }
    }

4、缓冲流

缓冲字节流


    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {
           try(BufferedInputStream in=new BufferedInputStream(new FileInputStream("src/in.txt"));)
           {
               in.mark(0);
               System.out.print((char)in.read());
               System.out.print((char)in.read());
               System.out.println((char)in.read());//hel
               in.reset(); //从mark处开始读
                System.out.print((char)in.read());
               System.out.print((char)in.read());
               System.out.print((char)in.read());//hel
           }
           catch(Exception e)
           {
               e.getStackTrace();
           }
        }
    }

    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {
           try(BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("src/out.txt"));)
           {
               out.write("Hello World!".getBytes());;
           }
           catch(Exception e)
           {
               e.getStackTrace();
           }
        }
    }

缓冲字符流


    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {
           try(BufferedReader in=new BufferedReader(new FileReader("src/in.txt"));)
           {
               System.out.println(in.readLine());//helloworld
           }
           catch(Exception e)
           {
               e.getStackTrace();
           }
        }
    }

    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {
           try(BufferedWriter out=new BufferedWriter(new FileWriter("src/out.txt"));)
           {
               out.write("hello");
               out.newLine();
               out.write("world");
//               hello 
//               world
           }
           catch(Exception e)
           {
               e.getStackTrace();
           }
        }
    }

5、转换流


    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {

            try(OutputStreamWriter out=new OutputStreamWriter(new FileOutputStream("src/out.txt"));)
            {
                out.write("你好");
            }
            catch(Exception e)
            {
                e.getStackTrace();
            }
        }
    }

    import java.io.*;
    import java.util.*;

    public class Main {
        public static  void main(String[] args)  {

            try(InputStreamReader in=new InputStreamReader(new FileInputStream("src/in.txt"));)
            {
                System.out.println((char)in.read());//你
                System.out.println((char)in.read());//好
            }
            catch(Exception e)
            {
                e.getStackTrace();
            }
        }
    }

6、打印流


    import java.io.*;
    import java.util.*;

    public class Main {
        public static void main(String[] args) {

            try (PrintStream out = new PrintStream(new FileOutputStream("src/out.txt"));) {
                out.println("Hello World!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

7、数据流和对象流

数据流(使用情况少,了解)


    import java.io.*;
    import java.util.*;

    public class Main {
        public static void main(String[] args) {

            try (DataInputStream in = new DataInputStream(new FileInputStream("src/in.txt"));) {
                System.out.println((char)in.readByte());//2
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

对象流


    import java.io.*;
    import java.util.*;

    public class Main {
        public static void main(String[] args) {

            try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("src/out.txt"));
                ObjectInputStream in = new ObjectInputStream(new FileInputStream("src/out.txt"))) {
               List<String> list=new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));
              out.writeObject(list);
             //�� sr java.util.ArrayListx����a� I sizexp   w   t At Bt Ct Dt Ex
                Object o=in.readObject();
                System.out.println(o);
              //  [A, B, C, D, E]
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

对于自已定义的类需要实现序列化才能写读


    import java.io.*;
    import java.util.*;

    public class Main {
        public static void main(String[] args) {

            try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("src/out.txt"));
                ObjectInputStream in = new ObjectInputStream(new FileInputStream("src/out.txt"))) {
             Person person = new Person();
             person.name= "zhangsan";
             out.writeObject(person);
             Person person1 = (Person) in.readObject();
             System.out.println(person1.name);//zhangsan
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        static class Person implements Serializable {
            String name;
//transient  忽略该变量
        }
    }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/902049.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

vue写个表格,让它滚动起来,没有用datav,有的时候结合会出错,一种简单的方法,直接用animation

表格样式就先不说了哈&#xff0c;这些简单内容&#xff0c;如果粉丝朋友还有什么问题&#xff0c;可以私信 好啦&#xff0c;首先&#xff0c;第一步 1.在目录的这个地方创建文件夹css&#xff0c;里面放两个文件 . 第一个文件里面内容 第二个文件里面内容 .drawCur{ curs…

VR在线展厅重塑展览新维度,引领沉浸式科技体验与漫游新时代

一、VR在线展厅开启数字展览新篇章 VR在线展厅将传统的实体展览空间转化为数字化的虚拟环境。参观用户只需使用手机、平板、电脑等设备就能瞬间穿越至虚拟展厅中&#xff0c;身临其境地浏览各类展品。这种前所未有的科技体验不仅让参观者感受到了数字技术的魅力&#xff0c;更极…

JS实现警灯效果红蓝闪烁

代码&#xff1a; <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>警灯效果红蓝闪烁</title&…

【WiFi7】 支持wifi7的手机

数据来源 Smartphones with WiFi 7 - list of all latest phones 2024 Motorola Moto X50 Ultra 6.7" 1220x2712 Snapdragon 8s Gen 3 16GB RAM 1024 GB 4500 mAh a/b/g/n/ac/6e/7 Sony Xperia 1 VI 6.5" 1080x2340 Snapdragon 8 Gen 3 12GB RAM 512 G…

web服务实验

http实验 先创建需要访问的web页面文件index.html 编辑vim /etc/nginx/conf.d/testip.conf 测试通过域名访问需要编辑/etc/hosts 如果通过windows的浏览器访问需要编辑下面的文件通过一管理员身份打开的记事本编辑 C:\Windows\System32\drivers\etc下的hosts文件 192.168.1…

Kubernetes运行大数据组件-设计思路

环境说明 在Kubernetes集群添加三个节点作为大数据测试服务节点&#xff1a; NAME STATUS ROLES AGE VERSION bigdata199056 Ready worker 2d3h v1.20.6 bigdata199057 Ready worker 2d5h v1.20.6 bigdata199058 Ready work…

Maven的依赖

一、依赖的基本配置 根元素project下的dependencies可以包含多个 dependence元素&#xff0c;以声明多个依赖。每个依赖都应 该包含以下元素&#xff1a; 1. groupId, artifactId, version : 依赖的基本坐标&#xff0c; 对于任何⼀个依赖来说&#xff0c;基本坐标是最…

前端聊天室页面开发(赛博朋克科技风,内含源码)

肝了一天&#xff0c;经过各种处理美化&#xff0c;肝出来了一个赛博朋克科技风的前端页面&#xff0c;用的原生三件套htmlcssjavascript开发的&#xff0c;本来想是加点功能调用一下gpt接口&#xff0c;但是基本都需要webscoket通信&#xff0c;可惜我js学的不是很深入&#x…

使用Vue.js构建响应式Web应用

&#x1f496; 博客主页&#xff1a;瑕疵的CSDN主页 &#x1f4bb; Gitee主页&#xff1a;瑕疵的gitee主页 &#x1f680; 文章专栏&#xff1a;《热点资讯》 使用Vue.js构建响应式Web应用 1 引言 2 Vue.js简介 3 安装Vue CLI 4 创建Vue项目 5 设计应用结构 6 创建组件 7 使用…

C++——string的模拟实现(下)

目录 成员函数 3.4 修改操作 (3)insert()函数 (4)pop_back()函数 (5)erase()函数 (6)swap()函数 3.5 查找操作 (1)find()函数 (2)substr()函数 3.6 重载函数 (1)operator赋值函数 (2)其他比较函数 (3)流插入和流提取 完整代码 结束语 第一篇链接&#xff1a;C——…

基于Springboot无人驾驶车辆路径规划系统(源码+定制+开发)

博主介绍&#xff1a; ✌我是阿龙&#xff0c;一名专注于Java技术领域的程序员&#xff0c;全网拥有10W粉丝。作为CSDN特邀作者、博客专家、新星计划导师&#xff0c;我在计算机毕业设计开发方面积累了丰富的经验。同时&#xff0c;我也是掘金、华为云、阿里云、InfoQ等平台…

【大模型】Ollama+WebUI+AnythingLLM搭建本地知识库

目录 1 部署Ollama 1.1 下载镜像ollama 1.2 运行ollama 1.3 验证 2 模型选型 3 安装模型 3.1 选择模型安装 3.2 删除模型(选看) 4 安装webUI 4.1 拉镜像 4.2 启动服务 5 访问 5.1 注册 5.2 登录 5.3 设置 6 使用 7 使用api来调用 8 安装AnythingLLM搭建本地…

27.9 调用go-ansible执行playbook拷贝json文件重载采集器

本节重点介绍 : go-ansible执行playbook编写分发重载的playbook编译执行 测试停掉一个节点测试停掉的节点再回来 go-ansible执行playbook 新增 goansiblerun/run.go package goansiblerunimport ("context""github.com/apenella/go-ansible/pkg/execute&qu…

Python基础学习(四)程序控制结构

代码获取&#xff1a;https://github.com/qingxuly/hsp_python_course 完结版&#xff1a;Python基础学习完结版 程序控制结构 程序流程控制介绍 基本介绍 程序流程控制绝对程序是如何执行的&#xff0c;是我们必须掌握的&#xff0c;主要有三大流程控制语句。顺序控制、分支…

Linux中DHCP服务器配置和管理

文章目录 一、DHCP服务1.1、DHCP的工作流程1.2、DHCP的工作模式1.3、dhcp的主要配置文件 二、安装DHCP服务2.1、更新yum源2.2、安装DHCP服务软件包2.3、配置DHCP服务2.4、启用DHCP服务&#xff08;解决报错&#xff09;2.4.1、查看dhcpd服务的状态和最近的日志条目2.4.2、查看与…

js构造函数和原型对象,ES6中的class,四种继承方式

一、构造函数 1.构造函数是一种特殊的函数&#xff0c;主要用来初始化对象 2.使用场景 常见的{...}语法允许创建一个对象。可以通过构造函数来快速创建多个类似的对象。 const Peppa {name: 佩奇,age: 6,sex: 女}const George {name: 乔治,age: 3,sex: 男}const Mum {nam…

【react 和 vue】 ---- 实现组件的递归渲染

1. 需求场景 今天遇到了一个需求&#xff0c;就是 HTML 的递归渲染。问题就是商品的可用时间&#xff0c;使用规则等数据是后端配置&#xff0c;然后配置规则则是可以无限递归的往下配置&#xff0c;可以存在很多级。后端实现后&#xff0c;数据返回前端&#xff0c;就需要前端…

【mysql 进阶】2-1. MySQL 服务器介绍

MySQL 服务器简介 通常所说的 MySQL 服务器指的是mysqld程序&#xff0c;当运⾏mysqld后对外提供MySQL 服务&#xff0c;这个专题的内容涵盖了以下关于MySQL 服务器以及相关配置的内容&#xff0c;包括&#xff1a; 服务器⽀持的启动选项。可以在命令⾏和配置⽂件中指定这些选…

前后端请求、返回数据的多种方式

Springboot项目的业务逻辑 &#x1f319;项目基本结构&#xff1a; 通常情况下&#xff0c;我们在搭建后端项目的时候&#xff0c;处理业务逻辑我们需要用到Controller,Service,Mapper(mybatis,mybatis-plus)&#xff0c;Entry各层之间的相互调用来完成&#xff0c;还有就是我…

数据库->增删改查

目录 一、CRUD简介 二、Create新增 1.单行数据插入 2.查询 3. 多行数据插入 4.执行本机的SQL脚本插入 三、Retrieve检索 1.全列查询 2.指定列查询 3.查询字段为表达式 3.1 常量表达式 3.2列的值与常量运算 3.3列与列之间的运算 3.4为列指定别名 4.结果查询去重…