当前位置: 首页 > news >正文

文件操作(与文件相关)相关笔记

1.FileInputStream

        1.构造方法

                new FileInputStream(String);

                意思是创建一个对象,让这个对象指向某个文件,然后对这个文件进行读取操作,如果这个文件不存在

        2.读取文件

                读取文件使用read()方法;

                如果每次读取一个字节,那么read方法返回的是这个字节的内容,也就是一个ascII码值,当没有其他字节的时候会返回-1

                如果每次读取多个字节,需要首先定义一个byte数组a,然后让这个数组作为read的形参,此时read函数返回的是这个数组的长度,如果没有其他字节了,就会返回-1

               示例:

//每次读取一个字节public static void readOne(String name){//FileInputStream,打印ascII码//假设文件当中的内容是abcdefg,这些字符每个字符一个字节,如果是汉字,每个字三个字节FileInputStream a = null;try{a = new FileInputStream(name);int result;while((result = a.read())!=-1){System.out.println(result);}//所以每次会打印一个字节的ascII码}catch(FileNotFoundException e){e.printStackTrace();}catch(IOException e){e.printStackTrace();;}finally{try{if(a!=null) {a.close();}}catch(IOException e){e.printStackTrace();}}}//每次读取多个字节public static void readMore(String name){//FileInputStream//假设文件当中的内容是abcdefg,这些字符每个字符一个字节,如果是汉字,每个字三个字节FileInputStream a = null;try{a = new FileInputStream(name);byte num[] = new byte[3];//假设每次读取三个字节int result;while((result = a.read(num))!=-1){//他会把这个获取到的字节数赋值给result//可以把数组直接转成字符串来打印String temp = new String(num,0,result);//意思是在num当中从第0个字节开始读取result个字节,然后形成字符串让String指向它System.out.println(temp+"\t"+result+"个字节");
//                for(byte o :num)
//                    System.out.println(o);}//每次提取三个字节}catch(FileNotFoundException e){e.printStackTrace();}catch(IOException e){e.printStackTrace();;}finally{try{if(a!=null) {a.close();}}catch(IOException e){e.printStackTrace();}}}

2.FileOutputStream

        1.构造方法:

                1.new FileOutputStream(String);

                2.newFileOutputStream(String,boolean);

                3.共同点

                        以上两种构造方法都会创建一个对象让这个对象指向某个文件,如果这个文件不存在就会直接创建这个文件,如果这个文件存在就会进行写入,如果文件无法打开就会抛

                4.区别

                        写入的时候:

                                       第一种构造方法会清空原文件然后重新开始写入

                                       第二种构造方法如果传入的布尔值为true说明存在这个文件,就会接续着原文件的内容进行写入

        2.写入文件

                        1.使用write方法

                        2.write方法的使用

                                write(byte数组)

                        3.如何从String转byte数组:

                                使用getBytes方法

 eg:

        String a="asda";

        byte b[] = a.getBytes();

//写入文件public static void write(String name,String name1){FileOutputStream a = null;FileOutputStream b = null;//写了两个文件,一个本来没有,一个本来就有try{a= new FileOutputStream(name);b=new FileOutputStream(name1,true);String str = "adwawdawdawdawd";String str1 = "java从入门到放弃";byte[] str2 = str.getBytes();byte[] str3 = str1.getBytes();a.write(str2);b.write(str3);a.flush();b.flush();}catch(FileNotFoundException e){e.printStackTrace();}catch(IOException e){e.printStackTrace();}finally{try{if(a!=null){a.close();}if(b!=null){b.close();}}catch(IOException e){e.printStackTrace();}}}

3. FileReader

        与FileInputStream的用法一致,只不过在读取的时候读取多字节从byte[],变成了char[]

4.FileWriter

        与FileOutputStream的用法一致,也是在写入文件的时候从write(byte数组)变成了write(String)

public static void wirterW(String name){FileWriter a = null;try {a = new FileWriter(name);String str ="这是FileWriter";a.write(str+"\n"+"已经另起一行了");a.flush();} catch (FileNotFoundException e){e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally{if(a!=null){try {a.close();} catch (IOException e) {e.printStackTrace();}}}}

        

相关文章:

  • 代码随想录第六十三天——被围绕的区域,太平洋大西洋水流问题,最大人工岛
  • ubuntu 20.04部署brc20 ordinals铭文
  • Python科学计算进阶:数值积分与微分求解算法应用在Python
  • JAVA获取昨日和今日日期时间
  • C++ | 四、指针、链表
  • Maxwell数据同步(增量)
  • 2024年学鸿蒙开发就业前景怎么样?
  • NLP论文阅读记录 - 2021 | WOS01 通过对比学习增强 Seq2Seq 自动编码器进行抽象文本摘要
  • img标签的奇怪问题
  • ubuntu20.04+opencv+vscode
  • 基于Java (spring-boot)的社团管理系统
  • Android 自动滚动的RecyclerView,手动滑动和自动滑动无缝衔接,手动滑动时数据不重复
  • C++核心编程——内存分区、引用、函数提高和函数重载
  • 观测云产品更新 | 日志、场景仪表板、监控器等
  • python基础教程八(循环完)
  • php的引用
  • python3.6+scrapy+mysql 爬虫实战
  • [译]前端离线指南(上)
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • AngularJS指令开发(1)——参数详解
  • axios 和 cookie 的那些事
  • Docker下部署自己的LNMP工作环境
  • JavaScript的使用你知道几种?(上)
  • JavaScript类型识别
  • LeetCode刷题——29. Divide Two Integers(Part 1靠自己)
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • node-glob通配符
  • select2 取值 遍历 设置默认值
  • SpingCloudBus整合RabbitMQ
  • vue-router的history模式发布配置
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 分类模型——Logistics Regression
  • - 概述 - 《设计模式(极简c++版)》
  • 快速构建spring-cloud+sleuth+rabbit+ zipkin+es+kibana+grafana日志跟踪平台
  • 前端_面试
  • 前端技术周刊 2019-01-14:客户端存储
  • 如何解决微信端直接跳WAP端
  • 微服务框架lagom
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • 阿里云重庆大学大数据训练营落地分享
  • 如何正确理解,内页权重高于首页?
  • ​Python 3 新特性:类型注解
  • ​业务双活的数据切换思路设计(下)
  • #NOIP 2014# day.1 生活大爆炸版 石头剪刀布
  • ()、[]、{}、(())、[[]]等各种括号的使用
  • (C#)获取字符编码的类
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (自用)learnOpenGL学习总结-高级OpenGL-抗锯齿
  • .bat批处理(三):变量声明、设置、拼接、截取
  • .NET 5.0正式发布,有什么功能特性(翻译)
  • .NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式
  • .Net Web项目创建比较不错的参考文章
  • .NET Windows:删除文件夹后立即判断,有可能依然存在
  • .NET 分布式技术比较
  • .NET设计模式(2):单件模式(Singleton Pattern)