API文档 => java.io => File


File类的应用

File类的概述

文件可认为是相关记录或放在一起的数据的集合
在Java中,使用java.io.file类对文件进行操作

File类的常用方法

getName():文件名
getParent():文件所在目录的路径
getPath():文件的相对路径
getAbsolutePath():文件的绝对路径

案例1:

  1. package com.file;
  2. import java.io.File;
  3. import java.io.IOException;
  4. public class FileDemo {
  5. public static void main(String[] args){
  6. // 创建File对象
  7. File file1 = new File("/Users/chenyisong/study/java-file/score.txt");
  8. File file2 = new File("/Users/chenyisong","study/java-file/score.txt");
  9. File file3 = new File("/Users/chenyisong");
  10. File file4 = new File(file3,"study/java-file/score.txt");
  11. // 判断是文件还是目录
  12. // score.txt是已经存在的,这时候可以判断出来是文件还是目录
  13. System.out.println(file1.isDirectory()); // false
  14. System.out.println(file2.isDirectory()); // false
  15. System.out.println(file4.isDirectory()); // false
  16. System.out.println(file1.isFile()); // true
  17. System.out.println(file2.isFile()); // true
  18. System.out.println(file4.isFile()); // true
  19. // 创建单级目录
  20. File file5 = new File("/Users/chenyisong/study/java-file","hashSet");
  21. if(!file5.exists()){
  22. file5.mkdir();
  23. }
  24. // 创建多级目录
  25. File file6 = new File("/Users/chenyisong/study/java-file/hashSet/set");
  26. if(!file6.exists()){
  27. file6.mkdirs();
  28. }
  29. // 创建文件
  30. File file7 = new File("/Users/chenyisong/study/java-file/name.txt");
  31. // name.txt是还不存在的,那么无法判断出是文件还是目录
  32. System.out.println(file7.isDirectory()); // false
  33. System.out.println(file7.isFile()); // false
  34. if(!file7.exists()){
  35. try {
  36. file7.createNewFile();
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. System.out.println(file7.getName()); // name.txt
  42. System.out.println(file7.getParent()); // /Users/chenyisong/study/java-file
  43. System.out.println(file7.getPath()); // /Users/chenyisong/study/java-file/name.txt
  44. }
  45. }

案例2:

  1. package com.file;
  2. import java.io.File;
  3. import java.io.IOException;
  4. public class FileDemo {
  5. public static void main(String[] args){
  6. File file = new File("thread.txt");
  7. try {
  8. file.createNewFile();
  9. System.out.println(file.isAbsolute()); // false
  10. System.out.println(file.getPath()); // thread.txt
  11. System.out.println(file.getAbsolutePath()); // /Users/chenyisong/study/java-project/thread.txt
  12. System.out.println(file.getName()); // thread.txt
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }

字节流

字节流包括字节输入流(InputStream)和字节输出流(OutputStream)

image.png
image.png

FileInputStream

  • 从文件系统中的某个文件中获得输入字节
  • 用于读取诸如图像数据之类的原始字节流

由于以下编码方式原因,文件输入输出流比较适合复制图像等二进制文件,而不是适合处理字符
#如果要处理字符,建议使用字符流

API文档 => java.io => FileInputStream

image.png

案例1:

  1. package com.file;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. public class FileInputDemo {
  6. public static void main(String[] args){
  7. // 创建一个FileInputStream对象
  8. try {
  9. FileInputStream fileInputStream = new FileInputStream("thread.txt");
  10. int n = 0;
  11. while ((n=fileInputStream.read())!=-1){
  12. System.out.print((char)n);
  13. }
  14. fileInputStream.close(); // 记得读取完之后关闭文件输入流
  15. } catch (FileNotFoundException e) {
  16. e.printStackTrace();
  17. } catch (IOException e){
  18. e.printStackTrace();
  19. }
  20. }
  21. }

运行结果:hello java!

案例2:

  1. package com.file;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. public class FileInputDemo {
  6. public static void main(String[] args) {
  7. // 创建一个FileInputStream对象
  8. try {
  9. FileInputStream fileInputStream = new FileInputStream("thread.txt");
  10. byte[] bytes = new byte[100];
  11. // 一次性从输入流中读取100byte数据,并放到数组bytes中
  12. fileInputStream.read(bytes);
  13. System.out.println(new String(bytes));
  14. fileInputStream.close();
  15. } catch (FileNotFoundException e) {
  16. e.printStackTrace();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }

运行结果:hello java!*

案例3:

  1. package com.file;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. public class FileInputDemo {
  6. public static void main(String[] args) {
  7. // 创建一个FileInputStream对象
  8. try {
  9. FileInputStream fileInputStream = new FileInputStream("thread.txt");
  10. byte[] bytes = new byte[100];
  11. // 一次性从输入流中读取10byte数据,并存放到bytes数组中(从数组下标6开始存储)
  12. fileInputStream.read(bytes,5,10);
  13. System.out.println(new String(bytes));
  14. fileInputStream.close();
  15. } catch (FileNotFoundException e) {
  16. e.printStackTrace();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }

运行结果:
*hello java*

FileOutputStream

API文档 => java.io => FileOutputStream

image.png

案例1:

  1. package com.file;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. public class FileOutputDemo {
  6. public static void main(String[] args){
  7. try {
  8. FileOutputStream fileOutputStream = new FileOutputStream("thread.txt");
  9. fileOutputStream.write(50);
  10. fileOutputStream.write('a');
  11. fileOutputStream.close();
  12. } catch (FileNotFoundException e) {
  13. e.printStackTrace();
  14. } catch (IOException e){
  15. e.printStackTrace();
  16. }
  17. }
  18. }

运行结果:2a
#根据ASCII码,其中2是50所代表的字符

案例2:

  1. package com.file;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. public class FileOutputDemo {
  7. public static void main(String[] args) {
  8. // 文件拷贝
  9. try {
  10. FileInputStream fileInputStream = new FileInputStream("mac.png");
  11. FileOutputStream fileOutputStream = new FileOutputStream("macCopy.png");
  12. int n = 0;
  13. byte[] bytes = new byte[1024];
  14. while ((n = fileInputStream.read(bytes)) != -1) {
  15. fileOutputStream.write(bytes, 0, n);
  16. // n 代表读取到的实际字节数
  17. // 注意这里不要写成 fileOutputStream.write(bytes)
  18. // 试想一下,最后一次从输入流中读取1024个字节数据的时候,可能这1024个字节并不都是有效数据,也许只有1000字节是有效的
  19. // 这时候还是按1024字节填充到目标文件的话,会造成浪费
  20. }
  21. fileInputStream.close();
  22. fileOutputStream.close();
  23. } catch (FileNotFoundException e) {
  24. e.printStackTrace();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }

BufferedInputStream

BufferedOutputStream

  • 使用缓冲输入输出流可以提高读写速度
  • BufferedOutputStream的方法flush()是用来清空缓冲区的

当缓冲区被填满时就会自动执行写操作,但是当缓冲区不满时,就不会执行写操作。所以,当缓冲区未 被填满但要执行写操作时就要强制清空缓冲区

  1. package com.file;
  2. import java.io.*;
  3. public class FileOutputDemo {
  4. public static void main(String[] args) {
  5. try {
  6. FileOutputStream fos = new FileOutputStream("thread.txt");
  7. BufferedOutputStream bos = new BufferedOutputStream(fos);
  8. FileInputStream fis = new FileInputStream("thread.txt");
  9. BufferedInputStream bis = new BufferedInputStream(fis);
  10. bos.write(50);
  11. bos.write('c');
  12. bos.flush();
  13. System.out.print(bis.read());
  14. System.out.print((char)bis.read());
  15. fos.close();
  16. bos.close();
  17. fis.close();
  18. bis.close();
  19. } catch (FileNotFoundException e) {
  20. e.printStackTrace();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. }

字符流

字符流包括字符输入流Reader和字符输出流Writer

image.png
image.png

InputStreamReader

OutputStreamReader

字节字符转换流

案例1:一个字符一个字符读取

  1. package com.file;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. public class ReadDemo {
  7. public static void main(String[] args) {
  8. try {
  9. FileInputStream fis = new FileInputStream("thread.txt");
  10. InputStreamReader isr = new InputStreamReader(fis);
  11. int n = 0;
  12. while ((n = isr.read()) != -1) {
  13. System.out.print((char) n);
  14. }
  15. fis.close();
  16. isr.close();
  17. } catch (FileNotFoundException e) {
  18. e.printStackTrace();
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }

运行结果:
你好,Java
你好,Java
你好,Java

你好,Java
你好,Java
你好,Java

案例2:批量读取字符

  1. package com.file;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. public class ReadDemo {
  7. public static void main(String[] args) {
  8. try {
  9. FileInputStream fis = new FileInputStream("thread.txt");
  10. InputStreamReader isr = new InputStreamReader(fis);
  11. int n = 0;
  12. char[] cbuf = new char[10];
  13. while ((n = isr.read(cbuf)) != -1) {
  14. String s = new String(cbuf, 0, n);
  15. System.out.print(s);
  16. }
  17. fis.close();
  18. isr.close();
  19. } catch (FileNotFoundException e) {
  20. e.printStackTrace();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. }

运行结果:
你好,Java
你好,Java
你好,Java

你好,Java
你好,Java
你好,Java

案例3:写入字符串

  1. package com.file;
  2. import java.io.*;
  3. public class ReadDemo {
  4. public static void main(String[] args) {
  5. try {
  6. FileInputStream fis = new FileInputStream("thread.txt");
  7. InputStreamReader isr = new InputStreamReader(fis);
  8. FileOutputStream fos = new FileOutputStream("threadCopy.txt");
  9. OutputStreamWriter osw = new OutputStreamWriter(fos);
  10. int n = 0;
  11. char[] cbuf = new char[10];
  12. while ((n = isr.read(cbuf)) != -1) {
  13. osw.write(cbuf,0,n);
  14. }
  15. osw.flush();
  16. fis.close();
  17. fos.close();
  18. isr.close();
  19. osw.close();
  20. } catch (FileNotFoundException e) {
  21. e.printStackTrace();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }

运行结果:复制thread.txt的内容到threadCopy.txt

对象的序列化与反序列化

  • 创建一个类,实现Serializable接口
  • 创建对象
  • 将对象写入文件
  • 从文件读取对象信息

序列化:把Java对象转换为字节序列的过程
反序列化:把字节序列恢复为Java对象的过程

ObjectInputStream

ObjectOutputStream

对象输入输出流

  1. package com.serial;
  2. import java.io.*;
  3. public class GoodsTest {
  4. public static void main(String[] args) {
  5. // 定义Goods类的对象
  6. Goods goods1 = new Goods("gd001", "电脑", 3000);
  7. try {
  8. FileOutputStream fos = new FileOutputStream("good.txt");
  9. ObjectOutputStream oos = new ObjectOutputStream(fos);
  10. FileInputStream fis = new FileInputStream("good.txt");
  11. ObjectInputStream ois = new ObjectInputStream(fis);
  12. // 将goods信息写入文件
  13. oos.writeObject(goods1);
  14. oos.flush();
  15. // 读取对象信息
  16. try {
  17. Goods goods = (Goods) ois.readObject();
  18. System.out.println(goods);
  19. } catch (ClassNotFoundException e) {
  20. e.printStackTrace();
  21. }
  22. fos.close();
  23. oos.close();
  24. fis.close();
  25. ois.close();
  26. } catch (FileNotFoundException e) {
  27. e.printStackTrace();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }