File类的使用
1.File类的理解
记住三个单词
**_file_**:__文件**_directory_**:__文件夹\目录**_path_**:__路径
1. File类的一个对象,代表一个文件或一个文件目录(俗称:文件夹)
2. File类声明在java.io包下
3. File类中涉及到关于文件或文件目录的创建、删除、重命名、修改时间、文件大小等方法,并未涉及到写入或读取文件内容的操作。如果需要读取或写入文件内容,必须使用IO流来完成。
4. 后续File类的对象常会作为参数传递到流的构造器中,指明读取或写入的”终点”.
2.File的实例化
2.1 常用构造器(构造方法)
File(String Pathname)通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
参数:string pathname:字符串的路径名称
路径可以是以文件结尾,也可以是以文件夹结尾
路径可以是相对路径,也可以是绝对路径
路径可以是存在,也可以是不存在
创建FiLe对象,只是把字符串路径封装为FiLe对象,只是把字符串路径封装为文件对象,不考虑路径的真假情况
File(String parentPath,String childPath)从父路径名字符串和子路径名字符串创建新的 File实例。
参数:把路径分成两部分
String parentPath:父路径
String childPath:子路径File(File parentFile,String childPath)从父抽象路径名和子路径名字符串创建新的 File实例。
// 文件路径名String pathname = "D:\\aaa.txt";File file1 = new File(pathname);// 文件路径名String pathname2 = "D:\\aaa\\bbb.txt";File file2 = new File(pathname2);// 通过父路径和子路径字符串String parent = "d:\\aaa";String child = "bbb.txt";File file3 = new File(parent, child);// 通过父级File对象和子路径字符串File parentDir = new File("d:\\aaa");String child = "bbb.txt";File file4 = new File(parentDir, child);
静态成员变量
| static String pathSeparator | 与系统有关的路径分隔符,为了方便,它被表示为一个字符串。 |
|---|---|
| static char pathSeparatorChar | 与系统有关的路径分隔符。 |
| static String separator | 与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串 |
| static char separatorChar | 与系统有关的默认名称分隔符。 |
2.2 路径的分类
相对路径:相较于某个(当前项目的根目录)路径下,指明的路径。
绝对路径:包含盘符在内的文件或文件目录的路径
路径不区分大小写
说明:
IDEA中:
如果大家开发使用JUnit中的单元测试方法测试,相对路径即为当前Module下。
如果大家使用main()测试,相对路径即为当前的Project下。
Eclipse中:
不管使用单元测试方法还是使用main()测试,相对路径都是当前的Project下。
2.3 路径分隔符
注意转义字符
路径分隔符
windows:分号;
linux:冒号:
文件名称分隔符
windows和DOS系统默认使用“\”来表示
UNIX和URL使用“/”来表示
3.File类的常用方法
获取功能的方法
public String getAbsolutePath():返回此File的绝对路径名字符串。public String getPath():将此File转换为路径名字符串。public String getName():返回由此File表示的文件或目录的名称。public long length():返回由此File表示的文件的长度(大小,字节为单位)。
方法演示,代码如下:
public class FileGet {public static void main(String[] args) {File f = new File("d:/aaa/bbb.java");System.out.println("文件绝对路径:"+f.getAbsolutePath());System.out.println("文件构造路径:"+f.getPath());System.out.println("文件名称:"+f.getName());System.out.println("文件长度:"+f.length()+"字节");File f2 = new File("d:/aaa");System.out.println("目录绝对路径:"+f2.getAbsolutePath());System.out.println("目录构造路径:"+f2.getPath());System.out.println("目录名称:"+f2.getName());System.out.println("目录长度:"+f2.length());}}输出结果:文件绝对路径:d:\aaa\bbb.java文件构造路径:d:\aaa\bbb.java文件名称:bbb.java文件长度:636字节目录绝对路径:d:\aaa目录构造路径:d:\aaa目录名称:aaa目录长度:4096
绝对路径和相对路径
- 绝对路径:从盘符开始的路径,这是一个完整的路径。
相对路径:相对于项目目录的路径,这是一个便捷的路径,开发中经常使用。
public class FilePath {public static void main(String[] args) {// D盘下的bbb.java文件File f = new File("D:\\bbb.java");System.out.println(f.getAbsolutePath());// 项目下的bbb.java文件File f2 = new File("bbb.java");System.out.println(f2.getAbsolutePath());}}输出结果:D:\bbb.javaD:\idea_project_test4\bbb.java
判断功能的方法
public boolean exists():此File表示的文件或目录是否实际存在。public boolean isDirectory():此File表示的是否为目录(文件夹)。public boolean isFile():此File表示的是否为文件。- 获取文件/文件夹先判段存不存在
方法演示,代码如下:
public class FileIs {public static void main(String[] args) {File f = new File("d:\\aaa\\bbb.java");File f2 = new File("d:\\aaa");// 判断是否存在System.out.println("d:\\aaa\\bbb.java 是否存在:"+f.exists());System.out.println("d:\\aaa 是否存在:"+f2.exists());// 判断是文件还是目录System.out.println("d:\\aaa 文件?:"+f2.isFile());System.out.println("d:\\aaa 目录?:"+f2.isDirectory());}}输出结果:d:\aaa\bbb.java 是否存在:trued:\aaa 是否存在:trued:\aaa 文件?:falsed:\aaa 目录?:true
创建删除功能的方法
public boolean createNewFile():当且仅当具有该名称的文件尚不存在时,创建一个新的空文件。
创建文件必须存在否则抛出异常
public boolean createNewFile() throws IOException
createNewFile 声明抛出了IOException,我们调用这个方法,就必须处理这个异常, 要么throws,要么try catch
public boolean delete():删除由此File表示的文件或目录。public boolean mkdir():创建由此File表示的单级空目录。public boolean mkdirs():创建由此File表示的单级/多级空目录,包括任何必需但不存在的父目录。
方法演示,代码如下:
public class FileCreateDelete {public static void main(String[] args) throws IOException {// 文件的创建File f = new File("aaa.txt");System.out.println("是否存在:"+f.exists()); // falseSystem.out.println("是否创建:"+f.createNewFile()); // trueSystem.out.println("是否存在:"+f.exists()); // true// 目录的创建File f2= new File("newDir");System.out.println("是否存在:"+f2.exists());// falseSystem.out.println("是否创建:"+f2.mkdir()); // trueSystem.out.println("是否存在:"+f2.exists());// true// 创建多级目录File f3= new File("newDira\\newDirb");System.out.println(f3.mkdir());// falseFile f4= new File("newDira\\newDirb");System.out.println(f4.mkdirs());// true// 文件的删除System.out.println(f.delete());// true// 目录的删除System.out.println(f2.delete());// trueSystem.out.println(f4.delete());// false}}
目录的遍历
public String[] list():返回一个String数组,表示该File目录中的所有子文件或目录。public File[] listFiles():返回一个File数组,表示该File目录中的所有的子文件或目录。public class FileFor {public static void main(String[] args) {File dir = new File("d:\\java_code");//获取当前目录下的文件以及文件夹的名称。String[] names = dir.list();for(String name : names){System.out.println(name);}//获取当前目录下的文件以及文件夹对象,只要拿到了文件对象,那么就可以获取更多信息File[] files = dir.listFiles();for (File file : files) {System.out.println(file);}}}
调用listFiles方法的File对象,表示的必须是实际存在的目录,否则返回null,无法进行遍历。
IO流概述
生活中,你肯定经历过这样的场景。当你编辑一个文本文件,忘记了ctrl+s ,可能文件就白白编辑了。当你电脑上插入一个U盘,可以把一个视频,拷贝到你的电脑硬盘里。那么数据都是在哪些设备上的呢?键盘、内存、硬盘、外接设备等等。
我们把这种数据的传输,可以看做是一种数据的流动,按照流动的方向,以内存为基准,分为输入input 和输出output ,即流向内存是输入流,流出内存的输出流。
Java中I/O操作主要是指使用java.io包下的内容,进行输入、输出操作。输入也叫做读取数据,输出也叫做作写出数据。1.流的分类
1.操作数据单位:
字节流:以字节为单位,读写数据的流。
字符流:以字符为单位,读写数据的流。
2.数据的流向:
输入流:把数据从其他设备上读取到内存中的流。
输出流:把数据从内存 中写出到其他设备上的流。
3.流的角色:
节点流
处理流
图示:
2.流的体系结构

说明:红框对应的是IO流中的4个抽象基类。
蓝框的流需要大家重点关注。顶级父类们
| 输入流 | 输出流 | | | —- | —- | —- | | 字节流 | 字节输入流InputStream | 字节输出流OutputStream | | 字符流 | 字符输入流Reader | 字符输出流Writer |
3.重点说明的几个流结构
4.输入、输出的标准化过程
4.1 输入过程
① 创建File类的对象,指明读取的数据的来源。(要求此文件一定要存在)
② 创建相应的输入流,将File类的对象作为参数,传入流的构造器中
③ 具体的读入过程:
创建相应的byte[] 或 char[]。
④ 关闭流资源
说明:程序中出现的异常需要使用try-catch-finally处理。
4.2 输出过程
① 创建File类的对象,指明写出的数据的位置。(不要求此文件一定要存在)
② 创建相应的输出流,将File类的对象作为参数,传入流的构造器中
③ 具体的写出过程:
write(char[]/byte[] buffer,0,len)
④ 关闭流资源
说明:程序中出现的异常需要使用try-catch-finally处理。
节点流(或文件流)
一切皆为字节
一切文件数据(文本、图片、视频等)在存储时,都是以二进制数字的形式保存,都一个一个的字节,那么传输时一样如此。所以,字节流可以传输任意文件数据。在操作流的时候,我们要时刻明确,无论使用什么样的流对象,底层传输的始终为二进制数据。
1.FileReader/FileWriter的使用:
1.1 FileReader的使用
/*将day09下的hello.txt文件内容读入程序中,并输出到控制台说明点:1. read()的理解:返回读入的一个字符。如果达到文件末尾,返回-12. 异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理3. 读入的文件一定要存在,否则就会报FileNotFoundException。*/@Testpublic void testFileReader1() {FileReader fr = null;try {//1.File类的实例化File file = new File("hello.txt");//2.FileReader流的实例化fr = new FileReader(file);//3.读入的操作//read(char[] cbuf):返回每次读入cbuf数组中的字符的个数。如果达到文件末尾,返回-1char[] cbuf = new char[5];int len;while((len = fr.read(cbuf)) != -1){//方式一://错误的写法// for(int i = 0;i < cbuf.length;i++){// System.out.print(cbuf[i]);// }//正确的写法// for(int i = 0;i < len;i++){// System.out.print(cbuf[i]);// }//方式二://错误的写法,对应着方式一的错误的写法// String str = new String(cbuf);// System.out.print(str);//正确的写法String str = new String(cbuf,0,len);System.out.print(str);}} catch (IOException e) {e.printStackTrace();} finally {if(fr != null){//4.资源的关闭try {fr.close();} catch (IOException e) {e.printStackTrace();}}}}
1.2 FileWriter的使用
/*从内存中写出数据到硬盘的文件里。说明:1. 输出操作,对应的File可以不存在的。并不会报异常2.File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件。File对应的硬盘中的文件如果存在:如果流使用的构造器是:FileWriter(file,false) / FileWriter(file):对原文件的覆盖如果流使用的构造器是:FileWriter(file,true):不会对原文件覆盖,而是在原文件基础上追加内容*/@Testpublic void testFileWriter() {FileWriter fw = null;try {//1.提供File类的对象,指明写出到的文件File file = new File("hello1.txt");//2.提供FileWriter的对象,用于数据的写出fw = new FileWriter(file,false);//3.写出的操作fw.write("I have a dream!\n");fw.write("you need to have a dream!");} catch (IOException e) {e.printStackTrace();} finally {//4.流资源的关闭if(fw != null){try {fw.close();} catch (IOException e) {e.printStackTrace();}}}}
1.3 文本文件的复制:
@Testpublic void testFileReaderFileWriter() {FileReader fr = null;FileWriter fw = null;try {//1.创建File类的对象,指明读入和写出的文件File srcFile = new File("hello.txt");File destFile = new File("hello2.txt");//不能使用字符流来处理图片等字节数据// File srcFile = new File("爱情与友情.jpg");// File destFile = new File("爱情与友情1.jpg");//2.创建输入流和输出流的对象fr = new FileReader(srcFile);fw = new FileWriter(destFile);//3.数据的读入和写出操作char[] cbuf = new char[5];int len;//记录每次读入到cbuf数组中的字符的个数while((len = fr.read(cbuf)) != -1){//每次写出len个字符fw.write(cbuf,0,len);}} catch (IOException e) {e.printStackTrace();} finally {//4.关闭流资源//方式一:// try {// if(fw != null)// fw.close();// } catch (IOException e) {// e.printStackTrace();// }finally{// try {// if(fr != null)// fr.close();// } catch (IOException e) {// e.printStackTrace();// }// }//方式二:try {if(fw != null)fw.close();} catch (IOException e) {e.printStackTrace();}try {if(fr != null)fr.close();} catch (IOException e) {e.printStackTrace();}}}
2.FileInputStream / FileOutputStream的使用:
1. 对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
2. 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,…),使用字节流处理
字节输出流【OutputStream】
java.io.OutputStream抽象类是表示字节输出流的所有类的超类,将指定的字节信息写出到目的地。它定义了字节输出流的基本共性功能方法。
public void close():关闭此输出流并释放与此流相关联的任何系统资源。public void flush():刷新此输出流并强制任何缓冲的输出字节被写出。public void write(byte[] b):将 b.length字节从指定的字节数组写入此输出流。public void write(byte[] b, int off, int len):从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流。public abstract void write(int b):将指定的字节输出流。
小贴士:
close方法,当完成流的操作时,必须调用此方法,释放系统资源。
FileOutputStream类
OutputStream有很多子类,我们从最简单的一个子类开始。java.io.FileOutputStream类是文件输出流,用于将数据写出到文件。
构造方法
public FileOutputStream(File file):创建文件输出流以写入由指定的 File对象表示的文件。public FileOutputStream(String name): 创建文件输出流以指定的名称写入文件。
当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有这个文件,会创建该文件。如果有这个文件,会清空这个文件的数据。
构造举例,代码如下:
public class FileOutputStreamConstructor throws IOException {public static void main(String[] args) {// 使用File对象创建流对象File file = new File("a.txt");FileOutputStream fos = new FileOutputStream(file);// 使用文件名称创建流对象FileOutputStream fos = new FileOutputStream("b.txt");}}
写出字节数据
写出字节:write(int b) 方法,每次可以写出一个字节数据,代码使用演示:
public class FOSWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileOutputStream fos = new FileOutputStream("fos.txt");// 写出数据fos.write(97); // 写出第1个字节fos.write(98); // 写出第2个字节fos.write(99); // 写出第3个字节// 关闭资源fos.close();}}输出结果:abc
小贴士:
- 虽然参数为int类型四个字节,但是只会保留一个字节的信息写出。
- 流操作完毕后,必须释放系统资源,调用close方法,千万记得。
写出字节数组:write(byte[] b),每次可以写出数组中的数据,代码使用演示:
public class FOSWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileOutputStream fos = new FileOutputStream("fos.txt");// 字符串转换为字节数组byte[] b = "黑马程序员".getBytes();// 写出字节数组数据fos.write(b);// 关闭资源fos.close();}}输出结果:黑马程序员
写出指定长度字节数组:write(byte[] b, int off, int len) ,每次写出从off索引开始,len个字节,代码使用演示:
public class FOSWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileOutputStream fos = new FileOutputStream("fos.txt");// 字符串转换为字节数组byte[] b = "abcde".getBytes();// 写出从索引2开始,2个字节。索引2是c,两个字节,也就是cd。fos.write(b,2,2);// 关闭资源fos.close();}}输出结果:cd
数据追加续写
经过以上的演示,每次程序运行,创建输出流对象,都会清空目标文件中的数据。如何保留目标文件中数据,还能继续添加新数据呢?
public FileOutputStream(File file, boolean append): 创建文件输出流以写入由指定的 File对象表示的文件。public FileOutputStream(String name, boolean append): 创建文件输出流以指定的名称写入文件。
这两个构造方法,参数中都需要传入一个boolean类型的值,true 表示追加数据,false 表示清空原有数据。这样创建的输出流对象,就可以指定是否追加续写了,代码使用演示:
public class FOSWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileOutputStream fos = new FileOutputStream("fos.txt",true);// 字符串转换为字节数组byte[] b = "abcde".getBytes();// 写出从索引2开始,2个字节。索引2是c,两个字节,也就是cd。fos.write(b);// 关闭资源fos.close();}}文件操作前:cd文件操作后:cdabcde
写出换行
Windows系统里,换行符号是\r\n 。把
以指定是否追加续写了,代码使用演示:
public class FOSWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileOutputStream fos = new FileOutputStream("fos.txt");// 定义字节数组byte[] words = {97,98,99,100,101};// 遍历数组for (int i = 0; i < words.length; i++) {// 写出一个字节fos.write(words[i]);// 写出一个换行, 换行符号转成数组写出fos.write("\r\n".getBytes());}// 关闭资源fos.close();}}输出结果:abcde
- 回车符
\r和换行符\n:
- 回车符:回到一行的开头(return)。
- 换行符:下一行(newline)。
- 系统中的换行:
- Windows系统里,每行结尾是
回车+换行,即\r\n;- Unix系统里,每行结尾只有
换行,即\n;- Mac系统里,每行结尾是
回车,即\r。从 Mac OS X开始与Linux统一。
字节输入流【InputStream】
java.io.InputStream抽象类是表示字节输入流的所有类的超类,可以读取字节信息到内存中。它定义了字节输入流的基本共性功能方法。
public void close():关闭此输入流并释放与此流相关联的任何系统资源。public abstract int read(): 从输入流读取数据的下一个字节。public int read(byte[] b): 从输入流中读取一些字节数,并将它们存储到字节数组 b中 。小贴士: close方法,当完成流的操作时,必须调用此方法,释放系统资源。
FileInputStream类
java.io.FileInputStream类是文件输入流,从文件中读取字节。
构造方法
FileInputStream(File file): 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。FileInputStream(String name): 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有该文件,会抛出FileNotFoundException 。
构造举例,代码如下:
public class FileInputStreamConstructor throws IOException{public static void main(String[] args) {// 使用File对象创建流对象File file = new File("a.txt");FileInputStream fos = new FileInputStream(file);// 使用文件名称创建流对象FileInputStream fos = new FileInputStream("b.txt");}}
读取字节数据
读取字节:
read方法,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回-1,代码使用演示:public class FISRead {public static void main(String[] args) throws IOException{// 使用文件名称创建流对象FileInputStream fis = new FileInputStream("read.txt");// 读取数据,返回一个字节int read = fis.read();System.out.println((char) read);read = fis.read();System.out.println((char) read);read = fis.read();System.out.println((char) read);read = fis.read();System.out.println((char) read);read = fis.read();System.out.println((char) read);// 读取到末尾,返回-1read = fis.read();System.out.println( read);// 关闭资源fis.close();}}输出结果:abcde-1
循环改进读取方式,代码使用演示:
public class FISRead {public static void main(String[] args) throws IOException{// 使用文件名称创建流对象FileInputStream fis = new FileInputStream("read.txt");// 定义变量,保存数据int b ;// 循环读取while ((b = fis.read())!=-1) {System.out.println((char)b);}// 关闭资源fis.close();}}输出结果:abcde
小贴士:
- 虽然读取了一个字节,但是会自动提升为int类型。
- 流操作完毕后,必须释放系统资源,调用close方法,千万记得。
使用字节数组读取:read(byte[] b),每次读取b的长度个字节到数组中,返回读取到的有效字节个数,读取到末尾时,返回-1 ,代码使用演示:
public class FISRead {public static void main(String[] args) throws IOException{// 使用文件名称创建流对象.FileInputStream fis = new FileInputStream("read.txt"); // 文件中为abcde// 定义变量,作为有效个数int len ;// 定义字节数组,作为装字节数据的容器byte[] b = new byte[2];// 循环读取while (( len= fis.read(b))!=-1) {// 每次读取后,把数组变成字符串打印System.out.println(new String(b));}// 关闭资源fis.close();}}输出结果:abcded
错误数据d,是由于最后一次读取时,只读取一个字节e,数组中,上次读取的数据没有被完全替换,所以要通过len,获取有效的字节,代码使用演示:
public class FISRead {public static void main(String[] args) throws IOException{// 使用文件名称创建流对象.FileInputStream fis = new FileInputStream("read.txt"); // 文件中为abcde// 定义变量,作为有效个数int len ;// 定义字节数组,作为装字节数据的容器byte[] b = new byte[2];// 循环读取while (( len= fis.read(b))!=-1) {// 每次读取后,把数组的有效字节部分,变成字符串打印System.out.println(new String(b,0,len));// len 每次读取的有效字节个数}// 关闭资源fis.close();}}输出结果:abcde
小贴士: 使用数组读取,每次读取多个字节,减少了系统间的IO操作次数,从而提高了读写的效率,建议开发中使用。
字节流练习:图片复制
复制原理图解
案例实现
复制图片文件,代码使用演示:
public class Copy {public static void main(String[] args) throws IOException {// 1.创建流对象// 1.1 指定数据源FileInputStream fis = new FileInputStream("D:\\test.jpg");// 1.2 指定目的地FileOutputStream fos = new FileOutputStream("test_copy.jpg");// 2.读写数据// 2.1 定义数组byte[] b = new byte[1024];// 2.2 定义长度int len;// 2.3 循环读取while ((len = fis.read(b))!=-1) {// 2.4 写出数据fos.write(b, 0 , len);}// 3.关闭资源fos.close();fis.close();}}
小贴士: 流的关闭原则:先开后关,后开先关。
/*实现对图片的复制操作*/@Testpublic void testFileInputOutputStream() {FileInputStream fis = null;FileOutputStream fos = null;try {//1.造文件File srcFile = new File("爱情与友情.jpg");File destFile = new File("爱情与友情2.jpg");//2.造流fis = new FileInputStream(srcFile);fos = new FileOutputStream(destFile);//3.复制的过程byte[] buffer = new byte[5];int len;while((len = fis.read(buffer)) != -1){fos.write(buffer,0,len);}} catch (IOException e) {e.printStackTrace();} finally {if(fos != null){//4.关闭流try {fos.close();} catch (IOException e) {e.printStackTrace();}}if(fis != null){try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}
【注意】
相对路径在IDEA和Eclipse中使用的区别?
IDEA:
如果使用单元测试方法,相对路径基于当前的Module的。
如果使用main()测试,相对路径基于当前Project的。
Eclipse:
单元测试方法还是main(),相对路径都是基于当前Project的。
字符流
当使用字节流读取文本文件时,可能会有一个小问题。就是遇到中文字符时,可能不会显示完整的字符,那是因为一个中文字符可能占用多个字节存储。所以Java提供一些字符流类,以字符为单位读写数据,专门用于处理文本文件。
字符输入流【Reader】
java.io.Reader抽象类是表示用于读取字符流的所有类的超类,可以读取字符信息到内存中。它定义了字符输入流的基本共性功能方法。
public void close():关闭此流并释放与此流相关联的任何系统资源。public int read(): 从输入流读取一个字符。public int read(char[] cbuf): 从输入流中读取一些字符,并将它们存储到字符数组 cbuf中 。FileReader类
java.io.FileReader `类是读取字符文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。
小贴士:
- 字符编码:字节与字符的对应规则。Windows系统的中文编码默认是GBK编码表。
idea中UTF-8- 字节缓冲区:一个字节数组,用来临时存储字节数据。
构造方法
FileReader(File file): 创建一个新的 FileReader ,给定要读取的File对象。FileReader(String fileName): 创建一个新的 FileReader ,给定要读取的文件的名称。
当你创建一个流对象时,必须传入一个文件路径。类似于FileInputStream 。
构造举例,代码如下:
public class FileReaderConstructor throws IOException{public static void main(String[] args) {// 使用File对象创建流对象File file = new File("a.txt");FileReader fr = new FileReader(file);// 使用文件名称创建流对象FileReader fr = new FileReader("b.txt");}}
读取字符数据
读取字符:
read方法,每次可以读取一个字符的数据,提升为int类型,读取到文件末尾,返回-1,循环读取,代码使用演示:public class FRRead {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileReader fr = new FileReader("read.txt");// 定义变量,保存数据int b ;// 循环读取while ((b = fr.read())!=-1) {System.out.println((char)b);}// 关闭资源fr.close();}}输出结果:黑马程序员
小贴士:虽然读取了一个字符,但是会自动提升为int类型。
使用字符数组读取:read(char[] cbuf),每次读取b的长度个字符到数组中,返回读取到的有效字符个数,读取到末尾时,返回-1 ,代码使用演示:
public class FRRead {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileReader fr = new FileReader("read.txt");// 定义变量,保存有效字符个数int len ;// 定义字符数组,作为装字符数据的容器char[] cbuf = new char[2];// 循环读取while ((len = fr.read(cbuf))!=-1) {System.out.println(new String(cbuf));}// 关闭资源fr.close();}}输出结果:黑马程序员序
获取有效的字符改进,代码使用演示:
public class FISRead {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileReader fr = new FileReader("read.txt");// 定义变量,保存有效字符个数int len ;// 定义字符数组,作为装字符数据的容器char[] cbuf = new char[2];// 循环读取while ((len = fr.read(cbuf))!=-1) {System.out.println(new String(cbuf,0,len));}// 关闭资源fr.close();}}输出结果:黑马程序员
字符输出流【Writer】
java.io.Writer抽象类是表示用于写出字符流的所有类的超类,将指定的字符信息写出到目的地。它定义了字节输出流的基本共性功能方法。
void write(int c)写入单个字符。void write(char[] cbuf)写入字符数组。abstract void write(char[] cbuf, int off, int len)写入字符数组的某一部分,off数组的开始索引,len写的字符个数。void write(String str)写入字符串。void write(String str, int off, int len)写入字符串的某一部分,off字符串的开始索引,len写的字符个数。void flush()刷新该流的缓冲。-
FileWriter类
java.io.FileWriter类是写出字符到文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。构造方法
FileWriter(File file): 创建一个新的 FileWriter,给定要读取的File对象。FileWriter(String fileName): 创建一个新的 FileWriter,给定要读取的文件的名称。
当你创建一个流对象时,必须传入一个文件路径,类似于FileOutputStream。
构造举例,代码如下:
public class FileWriterConstructor {public static void main(String[] args) throws IOException {// 使用File对象创建流对象File file = new File("a.txt");FileWriter fw = new FileWriter(file);// 使用文件名称创建流对象FileWriter fw = new FileWriter("b.txt");}}
基本写出数据
写出字符:
write(int b)方法,每次可以写出一个字符数据,代码使用演示:public class FWWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileWriter fw = new FileWriter("fw.txt");// 写出数据fw.write(97); // 写出第1个字符fw.write('b'); // 写出第2个字符fw.write('C'); // 写出第3个字符fw.write(30000); // 写出第4个字符,中文编码表中30000对应一个汉字。/*【注意】关闭资源时,与FileOutputStream不同。如果不关闭,数据只是保存到缓冲区,并未保存到文件。*/// fw.close();}}输出结果:abC田
小贴士:
- 虽然参数为int类型四个字节,但是只会保留一个字符的信息写出。
- 未调用close方法,数据只是保存到了缓冲区,并未写出到文件中。
关闭和刷新
因为内置缓冲区的原因,如果不关闭输出流,无法写出字符到文件中。但是关闭的流对象,是无法继续写出数据的。如果我们既想写出数据,又想继续使用流,就需要flush 方法了。
flush:刷新缓冲区,流对象可以继续使用。close:先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了。
代码使用演示:
public class FWWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileWriter fw = new FileWriter("fw.txt");// 写出数据,通过flushfw.write('刷'); // 写出第1个字符fw.flush();fw.write('新'); // 继续写出第2个字符,写出成功fw.flush();// 写出数据,通过closefw.write('关'); // 写出第1个字符fw.close();fw.write('闭'); // 继续写出第2个字符,【报错】java.io.IOException: Stream closedfw.close();}}
小贴士:即便是flush方法写出了数据,操作的最后还是要调用close方法,释放系统资源。
写出其他数据
写出字符数组 :write(char[] cbuf) 和 write(char[] cbuf, int off, int len) ,每次可以写出字符数组中的数据,用法类似FileOutputStream,代码使用演示:
public class FWWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileWriter fw = new FileWriter("fw.txt");// 字符串转换为字节数组char[] chars = "黑马程序员".toCharArray();// 写出字符数组fw.write(chars); // 黑马程序员// 写出从索引2开始,2个字节。索引2是'程',两个字节,也就是'程序'。fw.write(b,2,2); // 程序// 关闭资源fos.close();}}
写出字符串:write(String str) 和 write(String str, int off, int len),每次可以写出字符串中的数据,更为方便,代码使用演示:
public class FWWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象FileWriter fw = new FileWriter("fw.txt");// 字符串String msg = "黑马程序员";// 写出字符数组fw.write(msg); //黑马程序员// 写出从索引2开始,2个字节。索引2是'程',两个字节,也就是'程序'。fw.write(msg,2,2); // 程序// 关闭资源fos.close();}}
续写和换行:操作类似于FileOutputStream。
public class FWWrite {public static void main(String[] args) throws IOException {// 使用文件名称创建流对象,可以续写数据FileWriter fw = new FileWriter("fw.txt",true);// 写出字符串fw.write("黑马");// 写出换行fw.write("\r\n");// 写出字符串fw.write("程序员");// 关闭资源fw.close();}}输出结果:黑马程序员
小贴士:字符流,只能操作文本文件,不能操作图片,视频等非文本文件。 当我们单纯读或者写文本文件时 使用字符流 其他情况使用字节流
IO异常的处理
之前的入门练习,我们一直把异常抛出,而实际开发中并不能这样处理,建议使用try…catch…finally 代码块,处理异常部分,代码使用演示:
public class HandleException1 {public static void main(String[] args) {// 声明变量FileWriter fw = null;try {//创建流对象fw = new FileWriter("fw.txt");// 写出数据fw.write("黑马程序员"); //黑马程序员} catch (IOException e) {e.printStackTrace();} finally {try {if (fw != null) {fw.close();}} catch (IOException e) {e.printStackTrace();}}}}
JDK7的处理(扩展知识点了解内容)
还可以使用JDK7优化后的try-with-resource 语句,该语句确保了每个资源在语句结束时关闭。所谓的资源(resource)是指在程序完成后,必须关闭的对象。
格式:
try (创建流对象语句,如果多个,使用';'隔开) {// 读写数据} catch (IOException e) {e.printStackTrace();}
代码使用演示:
public class HandleException2 {public static void main(String[] args) {// 创建流对象try ( FileWriter fw = new FileWriter("fw.txt"); ) {// 写出数据fw.write("黑马程序员"); //黑马程序员} catch (IOException e) {e.printStackTrace();}}}
JDK9的改进(扩展知识点了解内容)
JDK9中try-with-resource的改进,对于引入对象的方式,支持的更加简洁。被引入的对象,同样可以自动关闭,无需手动close,我们来了解一下格式。
改进前格式:
// 被final修饰的对象final Resource resource1 = new Resource("resource1");// 普通对象Resource resource2 = new Resource("resource2");// 引入方式:创建新的变量保存try (Resource r1 = resource1;Resource r2 = resource2) {// 使用对象}
改进后格式:
// 被final修饰的对象final Resource resource1 = new Resource("resource1");// 普通对象Resource resource2 = new Resource("resource2");// 引入方式:直接引入try (resource1; resource2) {// 使用对象}
改进后,代码使用演示:
public class TryDemo {public static void main(String[] args) throws IOException {// 创建流对象final FileReader fr = new FileReader("in.txt");FileWriter fw = new FileWriter("out.txt");// 引入到try中try (fr; fw) {// 定义变量int b;// 读取数据while ((b = fr.read())!=-1) {// 写出数据fw.write(b);}} catch (IOException e) {e.printStackTrace();}}}
Properties属性集
概述
java.util.Properties 继承于Hashtable ,来表示一个持久的属性集。它使用键值结构存储数据,每个键及其对应值都是一个字符串。该类也被许多Java类使用,比如获取系统属性时,System.getProperties 方法就是返回一个Properties对象。
Properties类
构造方法
public Properties() :创建一个空的属性列表。
基本的存储方法
public Object setProperty(String key, String value): 保存一对属性。public String getProperty(String key):使用此属性列表中指定的键搜索属性值。public Set<String> stringPropertyNames():所有键的名称的集合。public class ProDemo {public static void main(String[] args) throws FileNotFoundException {// 创建属性集对象Properties properties = new Properties();// 添加键值对元素properties.setProperty("filename", "a.txt");properties.setProperty("length", "209385038");properties.setProperty("location", "D:\\a.txt");// 打印属性集对象System.out.println(properties);// 通过键,获取属性值System.out.println(properties.getProperty("filename"));System.out.println(properties.getProperty("length"));System.out.println(properties.getProperty("location"));// 遍历属性集,获取所有键的集合Set<String> strings = properties.stringPropertyNames();// 打印键值对for (String key : strings ) {System.out.println(key+" -- "+properties.getProperty(key));}}}输出结果:{filename=a.txt, length=209385038, location=D:\a.txt}a.txt209385038D:\a.txtfilename -- a.txtlength -- 209385038location -- D:\a.txt
与流相关的方法
public void load(InputStream inStream): 从字节输入流中读取键值对。
参数中使用了字节输入流,通过流对象,可以关联到某文件上,这样就能够加载文本中的数据了。文本数据格式:
filename=a.txtlength=209385038location=D:\a.txt
加载代码演示:
public class ProDemo2 {public static void main(String[] args) throws FileNotFoundException {// 创建属性集对象Properties pro = new Properties();// 加载文本中信息到属性集pro.load(new FileInputStream("read.txt"));// 遍历集合并打印Set<String> strings = pro.stringPropertyNames();for (String key : strings ) {System.out.println(key+" -- "+pro.getProperty(key));}}}输出结果:filename -- a.txtlength -- 209385038location -- D:\a.txt
小贴士:文本中的数据,必须是键值对形式,可以使用空格、等号、冒号等符号分隔。
缓冲流的使用
1.缓冲流涉及到的类:
BufferedInputStreamBufferedOutputStreamBufferedReaderBufferedWriter
2.作用:
作用:提供流的读取、写入的速度
提高读写速度的原因:内部提供了一个缓冲区。默认情况下是8kb
3.典型代码
3.1 使用BufferedInputStream和BufferedOutputStream:处理非文本文件
//实现文件复制的方法public void copyFileWithBuffered(String srcPath,String destPath){BufferedInputStream bis = null;BufferedOutputStream bos = null;try {//1.造文件File srcFile = new File(srcPath);File destFile = new File(destPath);//2.造流//2.1 造节点流FileInputStream fis = new FileInputStream((srcFile));FileOutputStream fos = new FileOutputStream(destFile);//2.2 造缓冲流bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);//3.复制的细节:读取、写入byte[] buffer = new byte[1024];int len;while((len = bis.read(buffer)) != -1){bos.write(buffer,0,len);}} catch (IOException e) {e.printStackTrace();} finally {//4.资源关闭//要求:先关闭外层的流,再关闭内层的流if(bos != null){try {bos.close();} catch (IOException e) {e.printStackTrace();}}if(bis != null){try {bis.close();} catch (IOException e) {e.printStackTrace();}}//说明:关闭外层流的同时,内层流也会自动的进行关闭。关于内层流的关闭,我们可以省略.// fos.close();// fis.close();}}
3.2 使用BufferedReader和BufferedWriter:处理文本文件
@Testpublic void testBufferedReaderBufferedWriter(){BufferedReader br = null;BufferedWriter bw = null;try {//创建文件和相应的流br = new BufferedReader(new FileReader(new File("dbcp.txt")));bw = new BufferedWriter(new FileWriter(new File("dbcp1.txt")));//读写操作//方式一:使用char[]数组// char[] cbuf = new char[1024];// int len;// while((len = br.read(cbuf)) != -1){// bw.write(cbuf,0,len);// // bw.flush();// }//方式二:使用StringString data;while((data = br.readLine()) != null){//方法一:// bw.write(data + "\n");//data中不包含换行符//方法二:bw.write(data);//data中不包含换行符bw.newLine();//提供换行的操作}} catch (IOException e) {e.printStackTrace();} finally {//关闭资源if(bw != null){try {bw.close();} catch (IOException e) {e.printStackTrace();}}if(br != null){try {br.close();} catch (IOException e) {e.printStackTrace();}}}}
转换流的使用
1.转换流涉及到的类:属于字符流
InputStreamReader:将一个字节的输入流转换为字符的输入流
解码:字节、字节数组 —->字符数组、字符串
OutputStreamWriter:将一个字符的输出流转换为字节的输出流
编码:字符数组、字符串 —-> 字节、字节数组
2.作用:提供字节流与字符流之间的转换
3.图示:
4.典型实现:
@Testpublic void test1() throws IOException {FileInputStream fis = new FileInputStream("dbcp.txt");// InputStreamReader isr = new InputStreamReader(fis);//使用系统默认的字符集//参数2指明了字符集,具体使用哪个字符集,取决于文件dbcp.txt保存时使用的字符集InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//使用系统默认的字符集char[] cbuf = new char[20];int len;while((len = isr.read(cbuf)) != -1){String str = new String(cbuf,0,len);System.out.print(str);}isr.close();}/*此时处理异常的话,仍然应该使用try-catch-finally综合使用InputStreamReader和OutputStreamWriter*/@Testpublic void test2() throws Exception {//1.造文件、造流File file1 = new File("dbcp.txt");File file2 = new File("dbcp_gbk.txt");FileInputStream fis = new FileInputStream(file1);FileOutputStream fos = new FileOutputStream(file2);InputStreamReader isr = new InputStreamReader(fis,"utf-8");OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");//2.读写过程char[] cbuf = new char[20];int len;while((len = isr.read(cbuf)) != -1){osw.write(cbuf,0,len);}//3.关闭资源isr.close();osw.close();}
5.说明:
文件编码的方式(比如:GBK),决定了解析时使用的字符集(也只能是GBK)。
常见的编码表
ASCII:美国标准信息交换码。
用一个字节的7位可以表示。
ISO8859-1:拉丁码表。欧洲码表
用一个字节的8位表示。
GB2312:中国的中文编码表。最多两个字节编码所有字符
GBK:中国的中文编码表升级,融合了更多的中文文字符号。最多两个字节编码
Unicode:国际标准码,融合了目前人类使用的所字符。为每个字符分配唯一的字符码。所有的文字都用两个字节来表示。
UTF-8:变长的编码方式,可用1-4个字节来表示一个字符。
对后面学习的启示
客户端/浏览器端 <——> 后台(java,GO,Python,Node.js,php) <——> 数据库
要求前前后后使用的字符集都要统一:UTF-8.
其它的流的使用
1. 标准的输入输出流:
System.in:标准的输入流,默认从键盘输入System.out:标准的输出流,默认从控制台输出
修改默认的输入和输出行为:
System类的setIn(InputStream is) / setOut(PrintStream ps)方式重新指定输入和输出的流。
2. 打印流:
PrintStream 和PrintWriter
说明:
提供了一系列重载的print()和println()方法,用于多种数据类型的输出System.out返回的是PrintStream的实例
3. 数据流:
DataInputStream 和 DataOutputStream
作用:
用于读取或写出基本数据类型的变量或字符串
示例代码:
/*练习:将内存中的字符串、基本数据类型的变量写出到文件中。注意:处理异常的话,仍然应该使用try-catch-finally.*/@Testpublic void test3() throws IOException {//1.DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));//2.dos.writeUTF("刘建辰");dos.flush();//刷新操作,将内存中的数据写入文件dos.writeInt(23);dos.flush();dos.writeBoolean(true);dos.flush();//3.dos.close();}/*将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中。注意点:读取不同类型的数据的顺序要与当初写入文件时,保存的数据的顺序一致!*/@Testpublic void test4() throws IOException {//1.DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));//2.String name = dis.readUTF();int age = dis.readInt();boolean isMale = dis.readBoolean();System.out.println("name = " + name);System.out.println("age = " + age);System.out.println("isMale = " + isMale);//3.dis.close();}
对象流的使用
1.对象流:
ObjectInputStream 和 ObjectOutputStream
2.作用:
ObjectOutputStream:内存中的对象—->存储中的文件、通过网络传输出去:序列化过程ObjectInputStream:存储中的文件、通过网络接收过来 —->内存中的对象:反序列化过程
3.对象的序列化机制:
对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘
上,或通过网络将这种二进制流传输到另一个网络节点。//当其它程序获取了这种二进制流,就可以恢复成原来的
Java对象
4.序列化代码实现:
@Testpublic void testObjectOutputStream(){ObjectOutputStream oos = null;try {//1.oos = new ObjectOutputStream(new FileOutputStream("object.dat"));//2.oos.writeObject(new String("我爱北京天安门"));oos.flush();//刷新操作oos.writeObject(new Person("王铭",23));oos.flush();oos.writeObject(new Person("张学良",23,1001,new Account(5000)));oos.flush();} catch (IOException e) {e.printStackTrace();} finally {if(oos != null){//3.try {oos.close();} catch (IOException e) {e.printStackTrace();}}}}
5.反序列化代码实现:
@Testpublic void testObjectInputStream(){ObjectInputStream ois = null;try {ois = new ObjectInputStream(new FileInputStream("object.dat"));Object obj = ois.readObject();String str = (String) obj;Person p = (Person) ois.readObject();Person p1 = (Person) ois.readObject();System.out.println(str);System.out.println(p);System.out.println(p1);} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally {if(ois != null){try {ois.close();} catch (IOException e) {e.printStackTrace();}}}}
6.实现序列化的对象所属的类需要满足:
1.需要实现接口:Serializable
2.当前类提供一个全局常量:serialVersionUID
3.除了当前Person类需要实现Serializable接口之外,还必须保证其内部所属性,也必须是可序列化的。(默认情况下,基本数据类型可序列化)
补充:ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量
RandomAccessFile的使用
1.随机存取文件流:RandomAccessFile
2.使用说明:
1.RandomAccessFile直接继承于java.lang.Object类,实现了DataInput和DataOutput接口
2.RandomAccessFile既可以作为一个输入流,又可以作为一个输出流
3.如果RandomAccessFile作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建。如果写出到的文件存在,则会对原文件内容进行覆盖。(默认情况下,从头覆盖)
4. 可以通过相关的操作,实现RandomAccessFile“插入”数据的效果。seek(int pos)
3.
典型代码1:
@Testpublic void test1() {RandomAccessFile raf1 = null;RandomAccessFile raf2 = null;try {//1.raf1 = new RandomAccessFile(new File("爱情与友情.jpg"),"r");raf2 = new RandomAccessFile(new File("爱情与友情1.jpg"),"rw");//2.byte[] buffer = new byte[1024];int len;while((len = raf1.read(buffer)) != -1){raf2.write(buffer,0,len);}} catch (IOException e) {e.printStackTrace();} finally {//3.if(raf1 != null){try {raf1.close();} catch (IOException e) {e.printStackTrace();}}if(raf2 != null){try {raf2.close();} catch (IOException e) {e.printStackTrace();}}}}
典型代码2:
/*使用RandomAccessFile实现数据的插入效果*/@Testpublic void test3() throws IOException {RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");raf1.seek(3);//将指针调到角标为3的位置//保存指针3后面的所数据到StringBuilder中StringBuilder builder = new StringBuilder((int) new File("hello.txt").length());byte[] buffer = new byte[20];int len;while((len = raf1.read(buffer)) != -1){builder.append(new String(buffer,0,len)) ;}//调回指针,写入“xyz”raf1.seek(3);raf1.write("xyz".getBytes());//将StringBuilder中的数据写入到文件中raf1.write(builder.toString().getBytes());raf1.close();//思考:将StringBuilder替换为ByteArrayOutputStream}
Path、Paths、Files的使用
1.NIO的使用说明:
Java NIO (New IO,Non-Blocking IO)是从Java 1.4版本开始引入的一套新的IO API,可以替代标准的Java IO AP。
>NIO与原来的IO同样的作用和目的,但是使用的方式完全不同,NIO支持面向缓冲区的(IO是面向流的)、基于通道的IO操作。
>NIO将以更加高效的方式进行文件的读写操作。
>随着 JDK 7 的发布,Java对NIO进行了极大的扩展,增强了对文件处理和文件系统特性的支持,以至于我们称他们为 NIO.2。2.Path的使用 —-jdk7提供
2.1Path的说明:
2.2如何实例化:
2.3常用方法:
3.Files工具类 —-jdk7提供
3.1作用:
3.2常用方法:




