在日常的java开发中少不了文件的读取和 写入,这就涉及到文件的I/O操作,今天就来总结下文件的IO操作,顺便文件的IO操作也需要File了的帮助,所以一起总结了。

以下图片为我根据其他博客所总结的内容进行了相应的总结和IO流的类结构图,类结构图中还少了几个类

简单描述下:

IO分为字节和字符流2中方式,字节流以byte为单位,字符流以字符为单位,1个字节8byte 0·255,字节流的抽象类为inputstream和outputstream,他们无法实例化,所以需要

其子类来实现。字符流通常按2个字节来表示则为16byte,字符流的抽象类为reader和writer。

字符流通常是需要将文件读取到内存才能进行操作的,而字节流则是直接操作文件的。

还是通过具体的例子来说明吧。

@Testpublic void testFileReader() throws IOException {    File file = new File("D:/train/train.txt");    FileInputStream fs = new FileInputStream(file);    InputStreamReader reader = new InputStreamReader(fs,"utf8");    BufferedReader bufferedReader = new BufferedReader(reader);    System.out.println(bufferedReader.readLine());// 关闭文件流
bufferedReader.flush();

bufferedReader.close();
reader.close();fs.close();
}

@Testpublic void testFileOut() throws IOException {    File file = new File("D:/train/train.txt");    FileOutputStream out = new FileOutputStream(file);    String str = "hello world";    out.write(str.getBytes("utf8"));
out.close();
}
 

上面的代码包含了文件的读取和文件的写入,包含字符集设置,读取操作其实可以包含了IO中绝大多数的操作了。需要注意的是不管是字节流,还是字符流他们的read()方法返回的结果都是int类型,需要将其转换成char类型。具体的源码可以根据类的结构树去查看。

一上为简单总结了IO流,下面是File类的总结:

File -> FileSystem -> WinNTFileSystem

操作文件其实通过操作系统的文件功能来实现的,不同的系统如windows和UNIX其实是不同的,新建的File通过getFileSystem()方法获取系统操作文件的权限,每个新建的文件都包含2个重要参数path和prefixLength

 /*** This abstract pathname's normalized pathname string. A normalized* pathname string uses the default name-separator character and does not* contain any duplicate or redundant separators.* 这个抽象路径的正常路径名称,"d:/program file/train"这种格式* @serial*/private final String path;/*** The length of this abstract pathname's prefix, or zero if it has no* prefix. 抽象路径的前缀长度,不存在则为0*/private final transient int prefixLength;

文件系统的操作其实很多都是通过这个前缀来创建文件的。

通过看源码可以发现,File类其实有点像接口,而WinNTFileSystem类则像实现接口。

看下File的构造函数,其中通过uri的构造函数没有贴出,有需要可以自己去查阅

/*** Creates a new <code>File</code> instance by converting the given* pathname string into an abstract pathname.  If the given string is* the empty string, then the result is the empty abstract pathname.** @param   pathname  A pathname string* @throws  NullPointerException*          If the <code>pathname</code> argument is <code>null</code> 最通用给构造函数,通过文件路径来创建文件*/public File(String pathname) {if (pathname == null) {throw new NullPointerException();}this.path = fs.normalize(pathname);this.prefixLength = fs.prefixLength(this.path);}/*** Creates a new <code>File</code> instance from a parent pathname string* and a child pathname string.** <p> If <code>parent</code> is <code>null</code> then the new* <code>File</code> instance is created as if by invoking the* single-argument <code>File</code> constructor on the given* <code>child</code> pathname string.** <p> Otherwise the <code>parent</code> pathname string is taken to denote* a directory, and the <code>child</code> pathname string is taken to* denote either a directory or a file.  If the <code>child</code> pathname* string is absolute then it is converted into a relative pathname in a* system-dependent way.  If <code>parent</code> is the empty string then* the new <code>File</code> instance is created by converting* <code>child</code> into an abstract pathname and resolving the result* against a system-dependent default directory.  Otherwise each pathname* string is converted into an abstract pathname and the child abstract* pathname is resolved against the parent.* 通过父路径和子路径来创建文件* @param   parent  The parent pathname string* @param   child   The child pathname string* @throws  NullPointerException*          If <code>child</code> is <code>null</code>*/public File(String parent, String child) {if (child == null) {throw new NullPointerException();}if (parent != null) {if (parent.equals("")) {this.path = fs.resolve(fs.getDefaultParent(), -->"/"fs.normalize(child));} else {this.path = fs.resolve(fs.normalize(parent),fs.normalize(child));}} else {this.path = fs.normalize(child);}this.prefixLength = fs.prefixLength(this.path);}/** 通过父文件和子路径来创建文件* Creates a new <code>File</code> instance from a parent abstract* pathname and a child pathname string.** <p> If <code>parent</code> is <code>null</code> then the new* <code>File</code> instance is created as if by invoking the* single-argument <code>File</code> constructor on the given* <code>child</code> pathname string.** <p> Otherwise the <code>parent</code> abstract pathname is taken to* denote a directory, and the <code>child</code> pathname string is taken* to denote either a directory or a file.  If the <code>child</code>* pathname string is absolute then it is converted into a relative* pathname in a system-dependent way.  If <code>parent</code> is the empty* abstract pathname then the new <code>File</code> instance is created by* converting <code>child</code> into an abstract pathname and resolving* the result against a system-dependent default directory.  Otherwise each* pathname string is converted into an abstract pathname and the child* abstract pathname is resolved against the parent.** @param   parent  The parent abstract pathname* @param   child   The child pathname string* @throws  NullPointerException*          If <code>child</code> is <code>null</code>*/public File(File parent, String child) {if (child == null) {throw new NullPointerException();}if (parent != null) {if (parent.path.equals("")) {this.path = fs.resolve(fs.getDefaultParent(),fs.normalize(child));} else {this.path = fs.resolve(parent.path,   --> 将父文件转换成路径,然后和子路径拼接fs.normalize(child));}} else {this.path = fs.normalize(child);}this.prefixLength = fs.prefixLength(this.path);}

其他的一些方法如创建新文件,creatNewFile(),创建文件夹mkdir(),mkdirs()等方法,相信看下源码就能很清楚了。

转载于:https://www.cnblogs.com/angleshoot/p/10584148.html

java基础之-I/O流和File类解析相关推荐

  1. java基础9(IO流)-File类

    File类 File:文件和目录路径名的抽象表示形式.即java中把文件或者目录都封装成File对象 代码练习1 import java.io.File;public class FileDemo1{ ...

  2. java(九)-方法引用, Stream流,File类 , 递归 ,字节流

    day09[方法引用.Lambda表达式.Stream流] 今日目标 线程状态 等待与唤醒 Lambda表达式 Stream流 教学目标 能够说出线程6个状态的名称 能够理解等待唤醒案例 能够掌握La ...

  3. Java基础:常用IO流

    1. 数据流 1.1 概述 数据流是操作基本数据类型的流,分为数据输入流,数据输出流. 1.2 数据输入流 1.DataInputStream:数据输出流允许应用程序以适当方式将基本 Java 数据类 ...

  4. java io 创建文件夹_Java中Io流操作-File类的常用操作-创建文件,创建文件夹

    package com.hxzy.IOSer; import java.io.File; import java.io.IOException; public class Demo03 { publi ...

  5. Java基础知识(二)(Object类的常用方法、日期时间类、System类、StringBuilder类、包装类、Collection集合、Iterator迭代器、泛型、list集Set接口...)

    文章目录 Java基础知识(二) 1.Object类的常用方法 1.1 toString方法 1.2 equals方法 1.3 Objects类 2.日期时间类 2.1 Date类 2.2 DateF ...

  6. 15.方法引用, Stream流,File类 , 递归 ,字节流

    JavaSE高级 Lambda表达式.方法引用.Stream流.File类 第一章 JDK8新特性 JDK新特性: Lambda 表达式 默认方法[已学习过] Stream API 方法引用 Base ...

  7. JAVA基础再回首(十三)——BigInteger类、BigDecimal类、Date类、DateFormat类、Calendar类

    JAVA基础再回首(十三)--BigInteger类.BigDecimal类.Date类.DateFormat类.Calendar类 版权声明:转载必须注明本文转自程序员杜鹏程的博客:http://b ...

  8. java基础--IO流之File类

    一.File类概述 用来将文件或者文件夹封装成对象,方便对文件与文件夹的属性信息进行操作,File对象可以作为参数传递给流的构造函数 二.File类常见方法: 1,创建 boolean createN ...

  9. 黑马程序员——Java基础--IO流(一)---File类以及其他流对象

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 一.File类 File类是将文件系统中的文件和文件夹封装成了对象.提供了更多的属性和行为可以对 ...

最新文章

  1. R函数:交集intersect、并集union、找不同setdiff、判断相同setequal
  2. 【BZOJ-2342】双倍回文 Manacher + 并查集
  3. RedHat(Linux) Oracle数据库设置开机自启动
  4. 多并发-最后刷新页面
  5. RabbitMQ入门(一)-安装(Windows环境下)
  6. 如何实现用户通信授权的可信、可知、可追溯?——通信授权服务技术解读
  7. C++STL笔记(一):STL综述
  8. android中搭建NDK环境及使用JNI技术
  9. 人工智能将为维护网络安全带来更多可能
  10. maven如果正常配置不成功,就按照我的就可以配置成功了
  11. 使用BoobSnail生成任意Excel 4.0 XLM宏文件
  12. python3.7怎么用ghost.py_python ghost.py使用笔记
  13. dfmea文件_DFMEA范本.doc
  14. 贵港市计算机成绩查询,贵港驾驶证扣分查询
  15. Flutter键盘弹出造成布局异常解决
  16. 私藏多年的vscode插件分享,让你成为一个高效开发的程序员
  17. 《机器学习实战》源代码和数据集下载
  18. win7系统卸载360管家之后无法上网怎么回事?
  19. 栈与队列:7-6 彩虹瓶
  20. 船舶电子电气工程专业出来的交响_船舶电子电气工程本科专业介绍

热门文章

  1. 写在使用 Linux 工作一年后
  2. Oracle安装——环境准备
  3. hive数据写入elasticsearch
  4. 牛客网Java刷题知识点之构造函数可以调用一般函数,但是一般函数不可以直接调用构造函数...
  5. ise和modelsim联合仿真的一些准备
  6. 你怎么看待 Bug?
  7. MBG配置详解及最佳实践
  8. mysql+drbd+heartbeat高可用配置说明
  9. VMWare 全屏后最小化死机
  10. 红帽启动apache服务器_红帽7搭建httpd的三种模式(基于主机,端口,IP)