6.1、File类

描述:该类是文件和目录路径名的抽象表示

构造方法:

方法 描述
public File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的File实例
public File(String parent, String child) 从父路径名字符串和子路径名字符串创建新的File实例
public File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的File实例

成员方法:

创建功能:

方法 描述
public boolean createNewFile() 当具有该名称的文件不存在时,创建一个由该抽象路径名命名的新空文件
public boolean mkdir() 创建由此抽象路径名命名的单级目录
public boolean mkdirs() 创建由此抽象路径名命名的多级目录

判断功能:

方法 描述
public boolean isDirectory() 测试此抽象路径名表示的File是否为目录
public boolean isFile() 测试此抽象路径名表示的File是否为文件
public boolean exists() 测试此抽象路径名表示的File是否存在

获取功能:

方法 描述
public String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串
public String getPath() 将此抽象路径名转换为路径名字符串
public String getName() 返回由此抽象路径名表示的文件或目录的名称
public String[] list() 返回此抽象路径名表示的目录中的文件和目录的名称字符串数组
public File[] listFiles() 返回此抽象路径名表示的目录中的文件和目录的File对象数组

删除功能:

方法 描述
public boolean delete() 删除由此抽象路径名表示的文件或目录

6.2、IO流

概述:IO流就是用来处理设备间数据传输问题的。常见的应用:文件复制、文件上传、文件下载、文件的读取、文件的写出等等

分类:

按照数据流向来分:
输入流:读数据
输出流:写数据 按照数据类型来分:
字节流
字节输入流
字节输出流
字符流
字符输入流
字符输出流

注意:

  1. 如果操作的是纯文本文件,优先使用字符流
  2. 如果操作的是图片、视频、音频、应用等二进制文件,优先使用字节流
  3. 如果不确定文件类型,优先使用字节流,字节流是万能的流

6.2.1、字节流

体系:

6.2.1.1、字节流写数据的三种方式

方法 描述
public void write(int b) 写入一个字节
public void write(byte[] b) 写入一个字节数组
public void write(byte[] b, int off, int len) 写入一个字节数组的一部分

6.2.1.2、字节流读数据的三种方式

方法 描述
public abstract int read() 读入一个字节
public int read(byte[] b) 读入一个字节数组
public int read(byte[] b, int off, int len) 读入一个字节数组的一部分

6.2.1.3、字节流复制文件的四种方式

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class Main {
public static void main(String[] args) throws IOException {
method1();
method2();
method3();
method4();
} // 基本字节流一次读写一个字节
public static void method1() throws IOException {
FileInputStream fis = new FileInputStream("sFolder\\demo.txt");
FileOutputStream fos = new FileOutputStream("dFolder\\demo.txt"); int by;
while ((by = fis.read()) != -1) {
fos.write(by);
} fos.close();
fis.close();
} // 基本字节流一次读写一个字节数组
public static void method2() throws IOException {
FileInputStream fis = new FileInputStream("sFolder\\demo.txt");
FileOutputStream fos = new FileOutputStream("dFolder\\demo.txt"); byte[] bys = new byte[1024];
int len;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
} fos.close();
fis.close();
} // 字节缓冲流一次读写一个字节
public static void method3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("sFolder\\demo.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("dFolder\\demo.txt")); int by;
while ((by = bis.read()) != -1) {
bos.write(by);
} bos.close();
bis.close();
} // 字节缓冲流一次读写一个字节数组
public static void method4() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("sFolder\\demo.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("dFolder\\demo.txt")); byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
} bos.close();
bis.close();
}
}

6.2.2、字符流

体系:

6.2.2.1、字符流写数据的五种方式

方法 描述
public void write(int c) 写入一个字符
public void write(char[] cbuf) 写入一个字符数组
public void write(char[] cbuf, int off, int len) 写入一个字符数组的一部分
public void write(String str) 写入一个字符串
public void write(String str, int off, int len) 写入一个字符串的一部分

6.2.2.2、字符流读数据的四种方式

方法 描述
public int read() 读入一个字符
public int read(char[] cbuf) 读入一个字符数组
public int read(char[] cbuf, int offset, int length) 读入一个字符数组的一部分
public String readLine() 读入一个字符串

6.2.2.3、字符流复制文本的七种方式

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter; public class Main {
public static void main(String[] args) throws IOException {
method1();
method2();
method3();
method4();
method5();
method6();
method7();
} // 基本字符流一次读写一个字符
public static void method1() throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("sFolder\\demo.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("dFolder\\demo.txt")); int ch;
while ((ch = isr.read()) != -1) {
osw.write(ch);
} osw.close();
isr.close();
} // 基本字符流一次读写一个字符数组
public static void method2() throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("sFolder\\demo.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("dFolder\\demo.txt")); char[] chs = new char[1024];
int len;
while ((len = isr.read(chs)) != -1) {
osw.write(chs, 0, len);
} osw.close();
isr.close();
} // 文件字符流一次读写一个字符
public static void method3() throws IOException {
FileReader fr = new FileReader("sFolder\\demo.txt");
FileWriter fw = new FileWriter("dFolder\\demo.txt"); int ch;
while ((ch = fr.read()) != -1) {
fw.write(ch);
} fw.close();
fr.close();
} // 文件字符流一次读写一个字符数组
public static void method4() throws IOException {
FileReader fr = new FileReader("sFolder\\demo.txt");
FileWriter fw = new FileWriter("dFolder\\demo.txt"); char[] chs = new char[1024];
int len;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
} fw.close();
fr.close();
} // 字符缓冲流一次读写一个字符
public static void method5() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("sFolder\\demo.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("dFolder\\demo.txt")); int ch;
while ((ch = br.read()) != -1) {
bw.write(ch);
} bw.close();
br.close();
} // 字符缓冲流一次读写一个字符数组
public static void method6() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("sFolder\\demo.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("dFolder\\demo.txt")); char[] chs = new char[1024];
int len;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
} bw.close();
br.close();
} // 字符缓冲流特有功能复制文本文件
public static void method7() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("sFolder\\demo.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("dFolder\\demo.txt")); String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
} bw.close();
br.close();
}
}

6.3、文件夹复制

6.3.1、复制单级文件夹

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class Main {
public static void main(String[] args) throws IOException {
File srcFolder = new File("D:\\sFolder");
File destFolder = new File("D:\\dFolder");
copyFolder(srcFolder, destFolder);
} /**
* 复制单级文件夹
*
* @param srcFolder 源文件夹
* @param destFolder 目的文件夹
* @throws IOException
*/
private static void copyFolder(File srcFolder, File destFolder) throws IOException {
// 判断路径是否存在
if (!destFolder.exists()) {
destFolder.mkdirs();
}
// 获取目的文件列表
File[] listFiles = srcFolder.listFiles();
// 遍历目的文件列表
for (File file : listFiles) {
copyFile(file, new File(destFolder, file.getName()));
}
} /**
* 复制文件
*
* @param srcFile 源文件
* @param destFile 目的文件
* @throws IOException
*/
private static void copyFile(File srcFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}

6.3.2、复制多级文件夹

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class Main {
public static void main(String[] args) throws IOException {
File srcFolder = new File("D:\\sFolder");
File destFolder = new File("D:\\dFolder");
copyFolder(srcFolder, destFolder);
} /**
* 复制多级文件夹
*
* @param srcFolder 源文件夹
* @param destFolder 目的文件夹
* @throws IOException
*/
private static void copyFolder(File srcFolder, File destFolder) throws IOException {
// 判断路径是否存在
if (!destFolder.exists()) {
destFolder.mkdirs();
}
// 获取目的文件列表
File[] listFiles = srcFolder.listFiles();
// 遍历目的文件列表
for (File file : listFiles) {
if (file.isDirectory()) {
copyFolder(file, new File(destFolder, file.getName()));
} else {
copyFile(file, new File(destFolder, file.getName()));
}
}
} /**
* 复制文件
*
* @param srcFile 源文件
* @param destFile 目的文件
* @throws IOException
*/
private static void copyFile(File srcFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}

6.3.3、捕获异常新特性

6.3.3.1、JDK7以前做法

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; public class Main {
public static void main(String[] args) {
method();
} private static void method() {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("fr.txt");
fw = new FileWriter("fw.txt");
char[] chs = new char[1024];
int len;
while ((len = fr.read()) != -1) {
fw.write(chs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

6.3.3.2、JDK7版本改进

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; public class Main {
public static void main(String[] args) {
method();
} private static void method() {
try (FileReader fr = new FileReader("fr.txt");
FileWriter fw = new FileWriter("fw.txt");) {
char[] chs = new char[1024];
int len;
while ((len = fr.read()) != -1) {
fw.write(chs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

6.4、IO特殊流

6.4.1、标准输入流

import java.io.IOException;
import java.io.InputStream; public class Main {
public static void main(String[] args) throws IOException {
InputStream is = System.in; int by;
while ((by = is.read()) != -1) {
System.out.print((char) by);
} is.close();
}
}

6.4.2、标准输出流

import java.io.IOException;
import java.io.PrintStream; public class Main {
public static void main(String[] args) throws IOException {
PrintStream ps = System.out; ps.println("Hello,World");
ps.write("Hello,World".getBytes()); ps.close();
}
}

6.4.3、字节打印流

import java.io.IOException;
import java.io.PrintStream; public class Main {
public static void main(String[] args) throws IOException {
PrintStream ps = new PrintStream("ps.txt"); ps.println(97);
ps.write(97); ps.close();
}
}

6.4.4、字符打印流

import java.io.IOException;
import java.io.PrintWriter; public class Main {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter("pw.txt"); pw.println("hello");
pw.write("Hello"); pw.close();
}
}

6.4.5、对象序列化流

注意:需要实现Serializable接口,同时需要给出serialVersionUID

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable; class Student implements Serializable {
private static final long serialVersionUID = 5923003911550370832L;
private String name;
private Integer age; public Student() {
super();
} public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
} public class Main {
public static void main(String[] args) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt")); Student s = new Student("曹晨磊", 30);
oos.writeObject(s); oos.close();
}
}

6.4.6、对象反序列化流

注意:成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable; class Student implements Serializable {
private static final long serialVersionUID = 5923003911550370832L;
private String name;
private Integer age; public Student() {
super();
} public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
} public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oos.txt")); Object obj = ois.readObject();
Student s = (Student) obj;
System.out.println(s); ois.close();
}
}

6.5、Properties集合

import java.util.Properties;
import java.util.Set; public class Main {
public static void main(String[] args) {
Properties prop = new Properties(); // 存储元素
prop.put("student1", "林青霞");
prop.put("student2", "张曼玉"); // 普通遍历
Set<Object> keySet = prop.keySet();
for (Object key : keySet) {
Object value = prop.get(key);
System.out.println(key + "," + value);
} // 特有方法
prop.setProperty("student3", "赵云");
prop.setProperty("student4", "张飞"); // 特有遍历
Set<String> names = prop.stringPropertyNames();
for (String key : names) {
String value = prop.getProperty(key);
System.out.println(key + "," + value);
}
}
}
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties; public class Main {
public static void main(String[] args) throws IOException {
// 把集合中的数据保存到文件
myStore();
// 把文件中的数据加载到集合
myLoad();
} private static void myStore() throws IOException {
Properties prop = new Properties();
prop.setProperty("student1", "林青霞");
prop.setProperty("student2", "张曼玉");
FileWriter fw = new FileWriter("fw.txt");
prop.store(fw, null);
fw.close();
} private static void myLoad() throws IOException {
Properties prop = new Properties();
FileReader fr = new FileReader("fw.txt");
prop.load(fr);
fr.close();
System.out.println(prop);
}
}

第六章 文件&IO流的更多相关文章

  1. Java - 文件(IO流)

    Java - 文件 (IO)   流的分类:     > 文件流:FileInputStream | FileOutputStream | FileReader | FileWriter     ...

  2. Java文件IO流的操作总结

    Java中的IO操作涉及到的概念及相关类很多,很容易弄混,今天特来整理总结一下,并附上一份完整的文件操作的代码. 概念解析 读和写 流就是管道,向管道里面写数据用输出流:write 从管道里面读数据, ...

  3. 文件IO流总结

    文件在网络上或不同设备之间是怎么传输的,在Java程序中又是怎么来实现文件的传输,带着这两个问题,来了解一下Java中的IO流相关类及操作. 一.什么是流及流的用途 流是一组有顺序,有起点和终点的字节 ...

  4. Java File类与文件IO流总结

    1.File类 File类被定义为“文件和目录路径名的抽象表示形式”,这是因为File类既可以表示“文件”也可以表示“目录”,他们都通过对应的路径来描述.通过构造函数创建一个File类对象,则该对象就 ...

  5. Python学习笔记 -- 第六章 文件操作

    I/O编程 在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开一个文件对象(通常称为文件描述符),然后,通过操作系统提供的接口从这 ...

  6. 关于java读取文件IO流学习总结(一)

    IO流的分类: 1.根据流的数据对象来分: 高端流:所有的内存中的流都是高端流,比如:InputStreamReader 低端流:所有的外界设备中的流都是低端流,比如InputStream,Outpu ...

  7. 3,Java中的文件IO流

    1,File类 ··· 概念:File对象可以表示一个文件或目录.可以对其进行增删改查. ··· 常用方法:     File f = new File(".");     判断是 ...

  8. 文件IO流完成文件的复制(复杂版本主要用来演示各种流的用途,不是最佳复制方案哦)

    package io; import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import j ...

  9. 文件IO流

    //字节流读写含有中文的文本文件会出现问题,我在实践中居然没有检验出该问题,新人小菜,希望大家能指出: import java.io.FileInputStream; import java.io.F ...

随机推荐

  1. JavaScript基础Literal 与 Constructor(008)

    JavaScript支持以字面声名法(Literal)的方式来声名对象和数组,相对于构造函数(constructor)的方式,Literal的声 名方式更简洁,更易读,也更少导致Bug.事实上,JSO ...

  2. 阿里云centos7安装jdk8

    1.准备Linux版本的jdk8直接上Oracle公司的官网下载就好了    http://www.oracle.com/technetwork/java/javase/downloads/jdk8- ...

  3. Guava RateLimiter限流器使用示例

    Guava中的RateLimiter可以限制单进程中某个方法的速率,本文主要介绍如何使用,实现原理请参考文档:推荐:超详细的Guava RateLimiter限流原理解析和推荐:RateLimiter ...

  4. 常用API - 时间日期类

    Date类 概述 java.util.Date类 表示特定的瞬间,精确到毫秒. 继续查阅Date类的描述,发现Date拥有多个构造函数,只是部分已经过时,但是其中有未过时的构造函数可以把毫秒值转成日期 ...

  5. CF796C Bank Hacking 题解

    洛谷链接 题目 Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To ...

  6. List集合的遍历方式

    遍历List集合的三种方法 List list = new ArrayList(); list.add("aaa"); list.add("bbb"); lis ...

  7. [JAVA]字符串常量池String pool

    字符串常量池(String Pool)保存着所有字符串字面量(literal strings),这些字面量在编译时期就确定.不仅如此,还可以使用 String 的 intern() 方法在运行过程中将 ...

  8. MYSQL 之 JDBC(一): 数据库连接(一)通过Driver接口获取数据库连接

    通过Driver接口获取数据库连接 数据持久化 数据库存取技术分类 JDBC直接访问数据库 JDO技术 第三方O/R工具,如Hibernate,ibatis等JDBC是java访问数据库的基石 JDB ...

  9. java IO流 (四) 缓冲流的使用

    1.缓冲流涉及到的类: * BufferedInputStream* BufferedOutputStream* BufferedReader* BufferedWriter 2.作用:作用:提供流的 ...

  10. 07 drf源码剖析之节流

    07 drf源码剖析之节流 目录 07 drf源码剖析之节流 1. 节流简述 2. 节流使用 3. 源码剖析 总结: 1. 节流简述 节流类似于权限,它确定是否应授权请求.节流指示临时状态,并用于控制 ...