File 类
java.io.File
类:文件和目录路径名的抽象表示形式,与平台无关。- File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。 如果需要访问文件内容本身,则需要使用 输入/输出流。
- File对象可以作为参数传递给流的构造函数。
File的静态属性
String separator存储了当前系统的路径分隔符。 在UNIX中,此字段为‘/’,在Windows中,为‘\’。
File类的常见构造方法
public File(String pathname)
以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。
public File(String parent,String child)
以parent为父路径,child为子路径创建File对象。
File类的常见方法
- 访问文件名:
getName() getPath() getAbsoluteFile() getAbsolutePath() getParent() renameTo(File newName)
- 文件检测:
exists() canWrite() canRead() isFile() isDirectory()
- 获取常规文件信息:
lastModified() length()
- 文件操作相关:
createNewFile() delete()
- 目录操作相关:
mkDir() mkDirs() list() listFiles()
File dir1 = new File("D:/IOTest/dir1");
// 如果D:/IOTest/dir1不存在,就创建为目录
if (!dir1.exists()) {
dir1.mkdir();
}
// 创建以dir1为父目录,名为"dir2"的File对象
File dir2 = new File(dir1, "dir2");
if (!dir2.exists()) { // 如果还不存在,就创建为目录
dir2.mkdirs();
}
File dir4 = new File(dir1, "dir3/dir4");
if (!dir4.exists()) {
dir4.mkdirs();
}
// 创建以dir2为父目录,名为"test.txt"的File对象
File file = new File(dir2, "test.txt");
if (!file.exists()) { // 如果还不存在,就创建为文件
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
练习
利用File构造器,new 一个目录file
1)在其中创建多个文件和目录。
2)编写方法,实现删除file中文件的操作。
public class Test2 {
public static void main(String[] args) {
File dir = new File("d:\\test2\\");
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(dir, "test.txt");
if (!file.exists()) { // 如果还不存在,就创建为文件
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("创建的文件名是:"+file.getName());
}
//删除文件
file.delete();
if(!file.exists()){
System.out.println("文件删除成功");
}
}
}
Java IO原理
- IO流用来处理设备之间的数据传输。
- Java程序中,对于数据的输入/输出操作以“流(stream)”的方式进行。
- java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。
- 输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。
- 输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中。
流的分类
- 按操作数据单位不同分为:字节流(8 bit),字符流(16 bit) 。
- 按数据流的流向不同分为:输入流,输出流。
- 按流的角色的不同分为:节点流,处理流。
- Java的IO流共涉及40多个类,实际上非常规则,都是从如下4个抽象基类派生的。
- 由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。
IO 流体系
节点流和处理流
- 节点流可以从一个特定的数据源读写数据。
- 处理流是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。
InputStream & Reader
- InputStream 和 Reader 是所有输入流的基类。
- InputStream(典型实现:FileInputStream)。
int read(); int read(byte[] b); int read(byte[] b, int off, int len);
- Reader(典型实现:FileReader)
int read(); int read(char [] c); int read(char [] c, int off, int len);
- 程序中打开的文件IO资源不属于内存里的资源,垃圾回收机制无法回收该资源,所以应该显式关闭文件IO资源。
OutputStream & Writer
- OutputStream 和 Writer 是所有输出流的基类。
- OutputStream(典型实现:FileOutputStream)。
void write(int b/int c); void write(byte[] b/char[] cbuf); void write(byte[] b/char[] buff, int off, int len); void flush(); void close(); //需要先刷新,再关闭此流。
- 因为字符流直接以字符作为操作单位,所以 Writer 可以用字符串来替换字符数组,即以 String 对象作为参数。
void write(String str); void write(String str, int off, int len);
文件流
读取文件
- 建立一个流对象,将已存在的一个文件加载进流。
FileReader fr = new FileReader(“Test.txt”);
- 创建一个临时存放数据的数组。
char[] ch = new char[1024];
- 调用流对象的读取方法将流中的数据读入到数组中。
fr.read(ch);
FileReader fr = null;
try {
fr = new FileReader("c:\\test.txt");
char[] buf = new char[1024];
int len = 0;
while ((len = fr.read(buf)) != -1) {
System.out.println(new String(buf, 0, len));
}
} catch (IOException e) {
System.out.println("read-Exception :" + e.toString());
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
System.out.println("close-Exception :" + e.toString());
}
}
}
写入文件
-
创建流对象,建立数据存放文件。
FileWriter fw = new FileWriter("Test.txt")
-
调用流对象的写入方法,将数据写入流。
fw.write("text")
-
关闭流资源,并将流中的数据清空到文件中。
fw.close()
例:
FileWriter fw = null;
try {
fw = new FileWriter("Test.txt");
fw.write("text");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
注意
- 定义文件路径时,注意:可以用“/”或者“\”。
- 在写入一个文件时,如果目录下有同名文件将被覆盖。
- 在读取文件时,必须保证该文件已存在,否则出异常。
处理流之一:缓冲流
- 为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组。
- 根据数据操作单位可以把缓冲流分为:
BufferedInputStream 和 BufferedOutputStream
BufferedReader 和 BufferedWriter
- 缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法。
- 对于输出的缓冲流,写出的数据会先在内存中缓存,使用
flush()
将会使内存中的数据立刻写出。
BufferedReader br = null;
BufferedWriter bw = null;
try {
//step1:创建缓冲流对象:它是过滤流,是对节点流的包装
br = new BufferedReader(new FileReader("d:\\IOTest\\source.txt"));
bw = new BufferedWriter(new FileWriter("d:\\IOTest\\destBF.txt"));
String str = null;
while ((str = br.readLine()) != null) {
//一次读取字符文本文件的一行字符
bw.write(str); //一次写入一行字符串
bw.newLine(); //写入行分隔符
}
bw.flush(); //step2:刷新缓冲区
} catch (IOException e) {
e.printStackTrace();
} finally {
// step3: 关闭IO流对象
try {
if (bw != null) {
//关闭过滤流时,会自动关闭它所包装的底层节点流
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
练习
分别使用
节点流:FileInputStream、FileOutputStream
和
缓冲流:BufferedInputStream、BufferedOutputStream
实现文本文件/图片/视频文件的复制。
并比较二者在数据复制方面的效率。
public class Test3 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("src/main/java/io/Test3.java");
FileOutputStream fos = new FileOutputStream("copy.txt");
int len = 0;
byte[] b = new byte[1024];
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
fis.close();
fos.close();
System.out.println("复制完成");
}
}
public class Test4 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("src/main/java/io/Test4.java"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.txt"));
int len = 0;
byte[] b = new byte[1024];
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
bis.close();
bos.close();
System.out.println("复制完成");
}
}
处理流之二:转换流
- 转换流提供了在字节流和字符流之间的转换。
- Java API提供了两个转换流:
InputStreamReader 和 OutputStreamWriter
- 字节流中的数据都是字符时,转成字符流操作更高效。
InputStreamReader
- 用于将字节流中读取到的字节按指定字符集解码成字符。需要和InputStream“套接”。
- 构造方法:
public InputStreamReader(InputStream in)
public InputSreamReader(InputStream in,String charsetName)
如:Reader isr = new InputStreamReader(System.in,”ISO5334_1”);
OutputStreamWriter
- 用于将要写入到字节流中的字符按指定字符集编码成字节。需要和OutputStream“套接”。
- 构造方法:
public OutputStreamWriter(OutputStream os)
public OutputSreamWriter(OutputStream os,String charsetName)
public void testMyInput() throws Exception {
FileInputStream fis = new FileInputStream("dbcp.txt");
FileOutputStream fos = new FileOutputStream("dbcp5.txt");
InputStreamReader isr = new InputStreamReader(fis, "GBK");
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
BufferedReader br = new BufferedReader(isr);
BufferedWriter bw = new BufferedWriter(osw);
String str = null;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
补充:字符编码
- 编码表的由来
计算机只能识别二进制数据,早期由来是电信号。为了方便应用计算机,让它可以识别各个国家的文字。就将各个国家的文字用数字来表示,并一一对应,形成一张表。这就是编码表。
- 常见的编码表
ASCII:美国标准信息交换码。用一个字节的7位可以表示。 ISO8859-1:拉丁码表。欧洲码表。用一个字节的8位表示。 GB2312:中国的中文编码表。 GBK:中国的中文编码表升级,融合了更多的中文文字符号。 Unicode:国际标准码,融合了多种文字。所有文字都用两个字节来表示,Java语言使用的就是unicode。 UTF-8:最多用三个字节来表示一个字符。
- 编码:字符串 → 字节数组
- 解码:字节数组 → 字符串
- 转换流的编码应用
可以将字符按指定编码格式存储。 可以对文本数据按指定编码格式来解读。 指定编码表的动作由构造器完成。
练习
将“你好”两个字符查指定的utf-8的码表,获取对应的数字,并写入到text.txt文件中。
public class Test {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("text.txt"), "utf-8");
osw.write("你好");
osw.close();
//读取硬盘上的文件数据,将获取到的数据查指定utf-8 的码表来解析该数据。
InputStreamReader isr = new InputStreamReader(new FileInputStream("text.txt"), "utf-8");
char[] buf = new char[10];
int num = isr.read(buf);
String s = new String(buf, 0, num);
System.out.println(s);
//传入编码表的方法都会抛出不支持编码异常(UnsupportedEncodingException);
}
}
处理流之三:标准输入输出流
- System.in和System.out分别代表了系统标准的输入和输出设备。
- 默认输入设备是键盘,输出设备是显示器。
- System.in的类型是InputStream。
- System.out的类型是PrintStream,其是OutputStream的子类FilterOutputStream 的子类。
- 通过System类的setIn,setOut方法对默认设备进行改变。
public static void setIn(InputStream in)
public static void setOut(PrintStream out)
例题
从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。
public class Test {
public static void main(String[] args) {
System.out.println("请输入信息(退出输入e或exit):");
//把"标准"输入流(键盘输入)这个字节流包装成字符流,再包装成缓冲流
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
String s = null;
try {
while ((s = br.readLine()) != null) { //读取用户输入的一行数据 --> 阻塞程序
if (s.equalsIgnoreCase("e") || s.equalsIgnoreCase("exit")) {
System.out.println("安全退出!!");
break;
}
//将读取到的整行字符串转成大写输出
System.out.println("-->:" + s.toUpperCase());
System.out.println("继续输入信息");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close(); //关闭过滤流时,会自动关闭它包装的底层节点流
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
或者
public class Test5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入字符串:");
while (input.hasNext()) {
String line = input.nextLine();
if ("e".equals(line) || "exit".equals(line)) {
System.exit(0);
}
String upperStr = line.toUpperCase();
System.out.println(upperStr);
}
}
}
处理流之四:打印流(了解)
- 在整个IO包中,打印流是输出信息最方便的类。
- PrintStream(字节打印流)和PrintWriter(字符打印流)。
提供了一系列重载的print和println方法,用于多种数据类型的输出。 PrintStream和PrintWriter的输出不会抛出异常。 PrintStream和PrintWriter有自动flush功能。 System.out返回的是PrintStream的实例。
public class Test {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("D:\\IO\\text.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//创建打印输出流,
//设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
PrintStream ps = new PrintStream(fos, true);
if (ps != null) { // 把标准输出流(控制台输出)改成文件
System.setOut(ps);
}
for (int i = 0; i <= 255; i++) { //输出ASCII字符
System.out.print((char) i);
if (i % 50 == 0) { //每50个数据一行
System.out.println(); // 换行
}
}
ps.close();
}
}
处理流之五:数据流(了解)
- 为了方便地操作Java语言的基本数据类型的数据,可以使用数据流。
- 数据流有两个类:(用于读取和写出基本数据类型的数据)
DataInputStream 和 DataOutputStream
分别“套接”在 InputStream 和 OutputStream 节点流上。
- DataInputStream中的方法:
boolean readBoolean()
char readChar()
float readFloat()
double readDouble()
byte readByte()
short readShort()
int readInt()
long readLong()
String readUTF()
void readFully(byte[] b)
- DataOutputStream中的方法
将上述的方法的read改为相应的write即可。
public class Test {
public static void main(String[] args) {
DataOutputStream dos = null;
try {
//创建连接到指定文件的数据输出流对象
dos = new DataOutputStream(new FileOutputStream("d:\\IOTest\\destData.dat"));
dos.writeUTF("ab中国"); //写UTF字符串
dos.writeBoolean(false); //写入布尔值
dos.writeLong(1234567890L); //写入长整数
System.out.println("写文件成功!");
} catch (IOException e) {
e.printStackTrace();
} finally { //关闭流对象
try {
if (dos != null) {
// 关闭过滤流时,会自动关闭它包装的底层节点流
dos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
处理流之六:对象流
- ObjectInputStream 和 OjbectOutputSteam
用于存储和读取对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。
- 序列化(Serialize):用ObjectOutputStream类将一个Java对象写入IO流中。
- 反序列化(Deserialize):用ObjectInputStream类从IO流中恢复该Java对象。
ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量。
对象的序列化
- 对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象
- 序列化的好处在于可将任何实现了
Serializable
接口的对象转化为字节数据,使其在保存和传输时可被还原 - 序列化是 RMI(Remote Method Invoke – 远程方法调用)过程的参数和返回值都必须实现的机制,而 RMI 是 JavaEE 的基础。因此序列化机制是 JavaEE 平台的基础
- 如果需要让某个对象支持序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一:
Serializable
Externalizable
- 凡是实现Serializable接口的类都有一个表示序列化版本标识符的静态变量:
private static final long serialVersionUID;
serialVersionUID
用来表明类的不同版本间的兼容性。 如果类没有显示定义这个静态变量,它的值是Java运行时环境根据类的内部细节自动生成的。若类的源代码作了修改,serialVersionUID
可能发生变化。故建议,显示声明。
- 显示定义serialVersionUID的用途:
希望类的不同版本对序列化兼容,因此需确保类的不同版本具有相同的serialVersionUID。 不希望类的不同版本对序列化兼容,因此需确保类的不同版本具有不同的serialVersionUID。
使用对象流序列化对象
- 若某个类实现了 Serializable 接口,该类的对象就是可序列化的:
创建一个 ObjectOutputStream。
调用 ObjectOutputStream对象的writeObject()方法输出可序列化对象。
注意写出一次,操作flush()。
- 反序列化
创建一个 ObjectInputStream。
调用 readObject() 方法读取流中的对象。
- 强调:如果某个类的字段不是基本数据类型或 String 类型,而是另一个引用类型,那么这个引用类型必须是可序列化的,否则拥有该类型的 Field 的类也不能序列化。
class Person implements Serializable {
private static final long serialVersionUID = 4911899814118879158L;
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Test {
public static void main(String[] args) throws Exception {
//序列化:将对象写入到磁盘或者进行网络传输。 要求对象必须实现序列化。
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test3.txt"));
Person p = new Person("韩梅梅", 18);
oos.writeObject(p);
oos.flush();
oos.close();
//反序列化:将磁盘中的对象数据源读出。
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test3.txt"));
Person p1 = (Person) ois.readObject();
System.out.println(p1.toString());
ois.close();
}
}
RandomAccessFile 类
- RandomAccessFile 类支持 “随机访问” 的方式,程序可以直接跳到文件的任意地方来读、写文件。
支持只访问文件的部分内容。
可以向已存在的文件后追加内容。
- RandomAccessFile 对象包含一个记录指针,用以标示当前读写处的位置。RandomAccessFile 类对象可以自由移动记录指针:
long getFilePointer():获取文件记录指针的当前位置。 void seek(long pos):将文件记录指针定位到 pos 位置。
- 构造器
public RandomAccessFile(File file, String mode)
public RandomAccessFile(String name, String mode)
- 创建 RandomAccessFile 类实例需要指定一个 mode 参数,该参数指定 RandomAccessFile 的访问模式:
r: 以只读方式打开。
rw:打开以便读取和写入。
rwd:打开以便读取和写入;同步文件内容的更新。
rws:打开以便读取和写入;同步文件内容和元数据的更新。
读取文件内容
public class Test {
public static void main(String[] args) throws Exception {
RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");
raf.seek(5);
byte[] b = new byte[1024];
int off = 0;
int len = 5;
raf.read(b, off, len);
String str = new String(b, 0, len);
System.out.println(str);
raf.close();
}
}
写入文件内容
public class Test {
public static void main(String[] args) throws Exception {
RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");
raf.seek(5);
//先读出来
String temp = raf.readLine();
raf.seek(5);
raf.write("xyz".getBytes());
raf.write(temp.getBytes());
raf.close();
}
}
流的基本应用小节
- 流是用来处理数据的。
- 处理数据时,一定要先明确数据源,与数据目的地。
数据源可以是文件,可以是键盘。
数据目的地可以是文件、显示器或者其他设备。
-
而流只是在帮助数据进行传输,并对传输的数据进行处理,比如过滤处理、转换处理等。
-
字节流-缓冲流(重点)
输入流:InputStream、FileInputStream、BufferedInputStream
输出流:OutputStream、FileOutputStream、BufferedOutputStream
- 字符流-缓冲流(重点)
输入流:Reader、FileReader、BufferedReader
输出流:Writer、FileWriter、BufferedWriter
- 转换流(字节流转换为字符流)
InputSteamReader、OutputStreamWriter
- 对象流(难点)
序列化:ObjectInputStream
反序列化:ObjectOutputStream
- 随机存取流(掌握读取、写入)
RandomAccessFile