前言

Java文件读写可以分为字节流和字符流,之前一直没有好好归纳以下,借此机会做一个小笔记。

目录

字节流

字节流就是一个字节一个字节的传输,最常用的就是FileInputStream和FileOutputStream,代码如下

 public static void  readByFIS(String filepath) {File file = new File(filepath);try {FileInputStream fis = new FileInputStream(file);byte [] bytes = new byte[fis.available()];while (fis.read(bytes, 0, bytes.length) != -1) {System.out.println(new String(bytes));}fis.close();} catch (IOException e) {e.printStackTrace();}}public static void writeByFIS(String filepath) {File file = new File(filepath);if(!file.exists()) {try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}try {FileOutputStream fos = new FileOutputStream(file, true);fos.write("从此无心爱良夜,任他明月下西楼\n".getBytes());fos.close();} catch (IOException e) {e.printStackTrace();}}

注意在读的的时候我们运用了available去获取一个和文件大小刚好的空间,这存在一个问题,如果文件过大可能会导致内存溢出。

使用BufferedInputStream 和BufferedOutputStream如下

public static void readByBIS(String filepath) {File file = new File(filepath);if(file.exists()) {try {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));byte[] bytes = new byte[bis.available()];bis.read(bytes);System.out.println(new String(bytes));bis.close();} catch (IOException e) {e.printStackTrace();}}}public static void writeByBIS(String filepath) {File file = new File(filepath);if(!file.exists()) {try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}try {BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filepath));bos.write("从此无心爱良夜,任他明月下西楼".getBytes());bos.flush();bos.close();} catch (IOException e) {e.printStackTrace();}}

字符流

运用FileWriter和FileReader

public static void writeByFR(String filepath) {File file = new File(filepath);if(!file.exists()) {try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}try {FileWriter writer = new FileWriter(filepath, true);writer.write("从此无心爱良夜,任他明月下西楼");writer.close();} catch (IOException e) {e.printStackTrace();}}public static void readByFR(String filepath) {File file = new File(filepath);if(file.exists()) {try {FileReader reader = new FileReader(file);char[] chars = new char[100];reader.read(chars);System.out.println(chars);reader.close();} catch (IOException e) {e.printStackTrace();}}}

运用BufferedWriter和BufferedReader

ublic static void readByBR(String filepath) {File file = new File(filepath);String line;try {BufferedReader reader = new BufferedReader(new FileReader(file));while ((line =  reader.readLine()) != null) {System.out.println(line);;}reader.close();} catch (IOException e) {e.printStackTrace();}}public static void writeByBR(String filepath) {File file = new File(filepath);if(!file.exists()) {try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}try {BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));writer.write("从此无心爱良夜,任他明月下西楼");writer.flush();writer.newLine();writer.close();} catch (IOException e) {e.printStackTrace();}}

最后

点赞就是最大的支持,更多文章和资料可以关注公众号QStack,追寻最纯粹的技术,享受编程的快乐。

从字节流到字符流之Java文件读写相关推荐

  1. 字符流复制Java文件改进版

    案例需求 使用便捷流对象,把模块目录下的"ConversionStreamDemo.java" 复制到模块目录下的"Copy.java" 实现步骤 根据数据源创 ...

  2. 字符流复制Java文件

    案例需求 把模块目录下的"ConversionStreamDemo.java" 复制到模块目录下的"Copy.java" 实现步骤 根据数据源创建字符输入流对象 ...

  3. java字节流转字符串_字节流与字符流的区别及相互转换

    先来看一下流的概念: 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入输出都是以流的形 ...

  4. 字节流与字符流对文件复制的效率

    字节流与字符流在用于文件复制时效率差距较大 分别对文件夹下的txt文件,jpg文件与mp4文件进行复制粘贴操作,并使用日期类进行运行时间记录,比较几种方法的效率. public class copy ...

  5. java中字符流和字节流的区别_java中字节流和字符流有哪些区别

    java中字节流和字符流的区别有:1.定义不同:2.结尾不同:3.处理方式不同:4.缓冲区不同:5.编码方式不同.字节流默认不使用缓冲区,而字符流使用缓冲区.字节流采用ASCII编码,字符流采用uni ...

  6. 字节流与字符流(一)

    一标题:字节输出流:OutputStream 知识点: File类虽然可以操作文件,但是不支持操作文件内容,如果要对内容进行操作只有两种方式:字符流和字节流. 具体内容: 如果要进行输入,输出操作一般 ...

  7. (字节流与字符流)OutputStream字节输出流

    字节输出流:OutputStream 字节的数据是以byte类型为主实现的操作,在进行字节内容输出的时候可以使用OutPutStream完成,这个类的基本定义如下: public abstract c ...

  8. (字节流与字符流)InputStream字节输入流

    与OutputStream类对应的是资杰输入流,InputStream主要是实现的就是字节数组读取. public abstract class InputStream extends Object ...

  9. 字节(byte)、二进制、字节流、字符流相关概念分析

    1.字节: 字(Byte)节是长度单位.位(bit)也是长度单位. 因为计算机通信和存储的时候都是以010101这样的二进制数据为基础的,这儿的一个0和1占的地方就叫bit(位),即一个二进制位. 1 ...

最新文章

  1. 企业面试题库_数据库部分
  2. 深入理解select、poll和epoll及区别
  3. nginx 跨服务器显示图片,centos6.6下nginx配置远程服务器上图片访问
  4. 实现点击页面其他地方,隐藏div(vue)
  5. phpwind 报名插件 dpsign_修图神器!PS无损放大图片插件Alien Skin Blow Up,可调3600%...
  6. 移动端自适应缩放代码
  7. git安装 苹果笔记本_自己挖的坑自己填,无光驱安装苹果笔记本双系统
  8. android报错:org.ksoap2.SoapFault cannot be cast to org.ksoap2.serialization.SoapObject
  9. 【在大学的快乐生活】ROS,RC车,jetson nano,导航与飙车(1)配件准备
  10. matlab对两组数据画图,matlab两列数据画图
  11. Java 移位操作符
  12. linux系统安装SAI吗,求救~~~把linux安装到C盘了
  13. 此生未完成 --- 于娟
  14. Beyond Compare4破解方法
  15. android 6.0 vs ios9,安卓6.0彻底看呆!iOS 9安装率曝光 完胜
  16. 一级指针, 二级指针
  17. Topic 15. 临床预测模型之决策曲线 (DCA)
  18. 换地方上网后Kali Linux 网络设置
  19. 《设计模式》12.组合模式(结构型)
  20. 开源的抖音壁纸即刻取图出现“这张图不见了,联系客服解决吧”的解决办法

热门文章

  1. 坐标提取,坐标定位(根据经纬度在地图上定位)
  2. c语言分治法求众数重数_分治法实现众数问题--例如:S={1,2,2,2,3,5},则多重集S的众数是2,其重数为3。对于给定的由m个自然数组成的多重集S,计算出S的众数及其重数。...
  3. LSM6DS3TR的IIC地址问题
  4. uni-app 写小程序 索引列表,仿微信通讯录
  5. Haar-Feature分类器和卷积神经网络
  6. png,jpg转换为ico的方法
  7. spi.service.contexts.ParameterExpansionContext.findAnnotation(Ljava/lang/Class;)
  8. 2009年网易春季校园招聘笔试客观题(逻辑部分)真题
  9. 三种方式循环获取水仙花数
  10. Xamarin.Android通过闹钟(AlarmManager)实现定时功能