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

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

简单描述下:

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

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

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

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


@Test
public 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();
}

@Test
public 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()等方法,相信看下源码就能很清楚了。

java基础之-I/O流和File类解析的更多相关文章

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

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

  2. 【Java基础】【19异常&IO(File类)】

    19.01_异常(异常的概述和分类) A:异常的概述 异常就是Java程序在运行过程中出现的错误. B:异常的分类 通过API查看Throwable Error 服务器宕机,数据库崩溃等 Except ...

  3. IO流,File类的测试........课堂加总结

    package liu0926; import java.io.File; import java.io.IOException; public class Text01 { public stati ...

  4. Java基础系列2:深入理解String类

    Java基础系列2:深入理解String类 String是Java中最为常用的数据类型之一,也是面试中比较常被问到的基础知识点,本篇就聊聊Java中的String.主要包括如下的五个内容: Strin ...

  5. java基础——IO流之File类

    1.File类的构造方法: File(String  pathName):通过一个指定的字符串类型路径来创建一个文件对象 File  (String parent,String child):通过指定 ...

  6. Java基础系列8——IO流超详细总结

    该系列博文会告诉你如何从入门到进阶,一步步地学习Java基础知识,并上手进行实战,接着了解每个Java知识点背后的实现原理,更完整地了解整个Java技术体系,形成自己的知识框架. 在初学Java时,I ...

  7. Java基础之详谈IO流

    Java基础知识.IO流详细讲解.你所要的IO这里都有

  8. Java笔记(二十七)……IO流中 File文件对象与Properties类

    File类 用来将文件或目录封装成对象 方便对文件或目录信息进行处理 File对象可以作为参数传递给流进行操作 File类常用方法 创建 booleancreateNewFile():创建新文件,如果 ...

  9. -1-4 java io java流 常用流 分类 File类 文件 字节流 字符流 缓冲流 内存操作流 合并序列流

      File类 •文件和目录路径名的抽象表示形式 构造方法 •public File(String pathname) •public File(String parent,Stringchild) ...

随机推荐

  1. Git学习:如何登陆以及创建本地代码仓库、并提交本地代码至Github(最简单方法)

    在我们的实际开发当中,代码经常会被公司要求上传到网络上,能够大家共同完成一个项目,因此掌握git技能也是一项必不可少的技能了,这里我们来学习以下基本的git操作.首先我们要想使用git这个东西需要把它 ...

  2. python从学渣到学沫的半月天

    今天又要引进一个新的知识点了,就是模块,可以直接引用的一个东西,从实用性来说很强大,不过还是需要记住模块的类型啊,如何应用还是需要学习和了解的.其中模块是分三种的,一种内置模块python内部提供的功 ...

  3. [转]微信小程序实现图片上传功能

    本文转自:http://blog.csdn.net/feter1992/article/details/77877659 前端: 微信开发者工具 后端:.Net 服务器:阿里云 这里介绍微信小程序如何 ...

  4. 发布基于Orchard Core的友浩达科技官网

    2018.9.25 日深圳市友浩达科技有限公司发布基于Orchard Core开发的官网 http://www.weyhd.com/. 本篇文章为你介绍如何基于Orchard Core开发一个公司网站 ...

  5. VS2013 百度云资源以及密钥

    https://pan.baidu.com/s/1eu3XycWO8fWItmkFeYNv9w提取码:dy9r 密钥:BWG7X-J98B3-W34RT-33B3R-JVYW9 vs2015 http ...

  6. 【重学计算机】机组D7章:总线

    1. 系统总线的特性及应用 总线概念:将计算机系统中各部件连接起来 总线分类:(外部/内部,系统/非系统,串行/并行,同步/异步...) 按用途分类: 存储总线:cpu与存储器 系统总线:连接存储总线 ...

  7. ASP.NET Core中使用自定义MVC过滤器属性的依赖注入

    除了将自己的中间件添加到ASP.NET MVC Core应用程序管道之外,您还可以使用自定义MVC过滤器属性来控制响应,并有选择地将它们应用于整个控制器或控制器操作. ASP.NET Core中常用的 ...

  8. 微服务实战(三):落地微服务架构到直销系统(构建基于RabbitMq的消息总线)

    从前面文章可以看出,消息总线是EDA(事件驱动架构)与微服务架构的核心部件,没有消息总线,就无法很好的实现微服务之间的解耦与通讯.通常我们可以利用现有成熟的消息代理产品或云平台提供的消息服务来构建自己 ...

  9. Abp中使用可视化的日志面板

    Abp中使用可视化的日志面板 如果你还不了解LogDashboard请看这里. ABP的相关知识不做介绍如果有需要请阅读ABP官方文档 ABP是Net下非常优秀的开发框架,在中国很多的项目都正在使用它 ...

  10. java jdk 8反编译工具JD-GUI、procyon-decompiler、luyten、crf下载使用简介

    本文对常用的反编译工具进行简单介绍 JD-GUI.procyon-decompiler.luyten.crf   反编译工具分类 JD-GUI JDK7以及之前可以使用   JD-GUI,如果版本&g ...