API文档 => java.io => File
File类的应用
File类的概述
文件可认为是相关记录或放在一起的数据的集合
在Java中,使用java.io.file类对文件进行操作
File类的常用方法
getName():文件名
getParent():文件所在目录的路径
getPath():文件的相对路径
getAbsolutePath():文件的绝对路径
案例1:
package com.file;import java.io.File;import java.io.IOException;public class FileDemo {public static void main(String[] args){// 创建File对象File file1 = new File("/Users/chenyisong/study/java-file/score.txt");File file2 = new File("/Users/chenyisong","study/java-file/score.txt");File file3 = new File("/Users/chenyisong");File file4 = new File(file3,"study/java-file/score.txt");// 判断是文件还是目录// score.txt是已经存在的,这时候可以判断出来是文件还是目录System.out.println(file1.isDirectory()); // falseSystem.out.println(file2.isDirectory()); // falseSystem.out.println(file4.isDirectory()); // falseSystem.out.println(file1.isFile()); // trueSystem.out.println(file2.isFile()); // trueSystem.out.println(file4.isFile()); // true// 创建单级目录File file5 = new File("/Users/chenyisong/study/java-file","hashSet");if(!file5.exists()){file5.mkdir();}// 创建多级目录File file6 = new File("/Users/chenyisong/study/java-file/hashSet/set");if(!file6.exists()){file6.mkdirs();}// 创建文件File file7 = new File("/Users/chenyisong/study/java-file/name.txt");// name.txt是还不存在的,那么无法判断出是文件还是目录System.out.println(file7.isDirectory()); // falseSystem.out.println(file7.isFile()); // falseif(!file7.exists()){try {file7.createNewFile();} catch (IOException e) {e.printStackTrace();}}System.out.println(file7.getName()); // name.txtSystem.out.println(file7.getParent()); // /Users/chenyisong/study/java-fileSystem.out.println(file7.getPath()); // /Users/chenyisong/study/java-file/name.txt}}
案例2:
package com.file;import java.io.File;import java.io.IOException;public class FileDemo {public static void main(String[] args){File file = new File("thread.txt");try {file.createNewFile();System.out.println(file.isAbsolute()); // falseSystem.out.println(file.getPath()); // thread.txtSystem.out.println(file.getAbsolutePath()); // /Users/chenyisong/study/java-project/thread.txtSystem.out.println(file.getName()); // thread.txt} catch (IOException e) {e.printStackTrace();}}}
字节流
字节流包括字节输入流(InputStream)和字节输出流(OutputStream)


FileInputStream
- 从文件系统中的某个文件中获得输入字节
- 用于读取诸如图像数据之类的原始字节流
由于以下编码方式原因,文件输入输出流比较适合复制图像等二进制文件,而不是适合处理字符
#如果要处理字符,建议使用字符流
API文档 => java.io => FileInputStream

案例1:
package com.file;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class FileInputDemo {public static void main(String[] args){// 创建一个FileInputStream对象try {FileInputStream fileInputStream = new FileInputStream("thread.txt");int n = 0;while ((n=fileInputStream.read())!=-1){System.out.print((char)n);}fileInputStream.close(); // 记得读取完之后关闭文件输入流} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e){e.printStackTrace();}}}
运行结果:hello java!
案例2:
package com.file;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class FileInputDemo {public static void main(String[] args) {// 创建一个FileInputStream对象try {FileInputStream fileInputStream = new FileInputStream("thread.txt");byte[] bytes = new byte[100];// 一次性从输入流中读取100byte数据,并放到数组bytes中fileInputStream.read(bytes);System.out.println(new String(bytes));fileInputStream.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
运行结果:hello java!*
案例3:
package com.file;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class FileInputDemo {public static void main(String[] args) {// 创建一个FileInputStream对象try {FileInputStream fileInputStream = new FileInputStream("thread.txt");byte[] bytes = new byte[100];// 一次性从输入流中读取10byte数据,并存放到bytes数组中(从数组下标6开始存储)fileInputStream.read(bytes,5,10);System.out.println(new String(bytes));fileInputStream.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
运行结果:
*hello java*
FileOutputStream
API文档 => java.io => FileOutputStream

案例1:
package com.file;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class FileOutputDemo {public static void main(String[] args){try {FileOutputStream fileOutputStream = new FileOutputStream("thread.txt");fileOutputStream.write(50);fileOutputStream.write('a');fileOutputStream.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e){e.printStackTrace();}}}
运行结果:2a
#根据ASCII码,其中2是50所代表的字符
案例2:
package com.file;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class FileOutputDemo {public static void main(String[] args) {// 文件拷贝try {FileInputStream fileInputStream = new FileInputStream("mac.png");FileOutputStream fileOutputStream = new FileOutputStream("macCopy.png");int n = 0;byte[] bytes = new byte[1024];while ((n = fileInputStream.read(bytes)) != -1) {fileOutputStream.write(bytes, 0, n);// n 代表读取到的实际字节数// 注意这里不要写成 fileOutputStream.write(bytes)// 试想一下,最后一次从输入流中读取1024个字节数据的时候,可能这1024个字节并不都是有效数据,也许只有1000字节是有效的// 这时候还是按1024字节填充到目标文件的话,会造成浪费}fileInputStream.close();fileOutputStream.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
BufferedInputStream
BufferedOutputStream
- 使用缓冲输入输出流可以提高读写速度
- BufferedOutputStream的方法flush()是用来清空缓冲区的
当缓冲区被填满时就会自动执行写操作,但是当缓冲区不满时,就不会执行写操作。所以,当缓冲区未 被填满但要执行写操作时就要强制清空缓冲区
package com.file;import java.io.*;public class FileOutputDemo {public static void main(String[] args) {try {FileOutputStream fos = new FileOutputStream("thread.txt");BufferedOutputStream bos = new BufferedOutputStream(fos);FileInputStream fis = new FileInputStream("thread.txt");BufferedInputStream bis = new BufferedInputStream(fis);bos.write(50);bos.write('c');bos.flush();System.out.print(bis.read());System.out.print((char)bis.read());fos.close();bos.close();fis.close();bis.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
字符流
字符流包括字符输入流Reader和字符输出流Writer
InputStreamReader
OutputStreamReader
字节字符转换流
案例1:一个字符一个字符读取
package com.file;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;public class ReadDemo {public static void main(String[] args) {try {FileInputStream fis = new FileInputStream("thread.txt");InputStreamReader isr = new InputStreamReader(fis);int n = 0;while ((n = isr.read()) != -1) {System.out.print((char) n);}fis.close();isr.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
运行结果:
你好,Java
你好,Java
你好,Java
你好,Java
你好,Java
你好,Java
案例2:批量读取字符
package com.file;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;public class ReadDemo {public static void main(String[] args) {try {FileInputStream fis = new FileInputStream("thread.txt");InputStreamReader isr = new InputStreamReader(fis);int n = 0;char[] cbuf = new char[10];while ((n = isr.read(cbuf)) != -1) {String s = new String(cbuf, 0, n);System.out.print(s);}fis.close();isr.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
运行结果:
你好,Java
你好,Java
你好,Java
你好,Java
你好,Java
你好,Java
案例3:写入字符串
package com.file;import java.io.*;public class ReadDemo {public static void main(String[] args) {try {FileInputStream fis = new FileInputStream("thread.txt");InputStreamReader isr = new InputStreamReader(fis);FileOutputStream fos = new FileOutputStream("threadCopy.txt");OutputStreamWriter osw = new OutputStreamWriter(fos);int n = 0;char[] cbuf = new char[10];while ((n = isr.read(cbuf)) != -1) {osw.write(cbuf,0,n);}osw.flush();fis.close();fos.close();isr.close();osw.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
运行结果:复制thread.txt的内容到threadCopy.txt
对象的序列化与反序列化
- 创建一个类,实现Serializable接口
- 创建对象
- 将对象写入文件
- 从文件读取对象信息
序列化:把Java对象转换为字节序列的过程
反序列化:把字节序列恢复为Java对象的过程
ObjectInputStream
ObjectOutputStream
对象输入输出流
package com.serial;import java.io.*;public class GoodsTest {public static void main(String[] args) {// 定义Goods类的对象Goods goods1 = new Goods("gd001", "电脑", 3000);try {FileOutputStream fos = new FileOutputStream("good.txt");ObjectOutputStream oos = new ObjectOutputStream(fos);FileInputStream fis = new FileInputStream("good.txt");ObjectInputStream ois = new ObjectInputStream(fis);// 将goods信息写入文件oos.writeObject(goods1);oos.flush();// 读取对象信息try {Goods goods = (Goods) ois.readObject();System.out.println(goods);} catch (ClassNotFoundException e) {e.printStackTrace();}fos.close();oos.close();fis.close();ois.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

