Java IO
File
构造方法
方法名称 | 说明 |
---|
public File(String pathname) | 根据指定的字符串为路径创建File对象 |
public File(String parent,String child) | 根据指定的父级路径与子路径创建对象 |
public File(File parent,String child) | 根据指定的File 对象为父级路径,字符串为子级路径创建File对象 |
查询文件
方法名称 | 说明 |
---|
public boolean exists() | 判断当前路径是否存在 |
public boolean isFile() | 判断当前路径是否为文件 |
public boolean isDirectory() | 判断当前路径是否为为文件夹 |
public long length() | 返回文件的字节数 |
public String getAbsolutPath() | 返回文件的绝对路径 |
public String getPath() | 返回定义文件时使用的路径 |
public String getName() | 返回文件名称 带后缀 |
public class Main {
public static void main(String[] args) {
File f=new File("C:\\Program Files\\Common Files\\DESIGNER");
System.out.println(f.exists());
System.out.println(f.isFile());
System.out.println(f.isDirectory());
}
}
增删文件
方法名称 | 说明 |
---|
public boolean createNewFile() | 创建一个空文件 |
public boolean mkdir() | 创建一个空文件夹 |
public boolean mkdirs() | 递归创建一个空文件夹 |
public boolean delete() | 删除文件、空文件夹 |
createNewFile
public class Main {
public static void main(String[] args) throws IOException {
File f=new File("C:\\Users\\Guohy\\Desktop\\IOtest\\a.txt");
f.createNewFile();
}
}
mkdir
public class Main {
public static void main(String[] args) throws IOException {
File f=new File("C:\\Users\\Guohy\\Desktop\\IOtest\\aaa");
f.mkdir();
}
}
mkdirs
public class Main {
public static void main(String[] args) throws IOException {
File f=new File("C:\\Users\\Guohy\\Desktop\\IOtest\\aaa\\bbb");
f.mkdir();
}
}
delete
public class Main {
public static void main(String[] args) throws IOException {
File f=new File("C:\\Users\\Guohy\\Desktop\\IOtest\\a.txt");
f.delete();
}
}
遍历文件夹
方法名称 | 说明 |
---|
public static File[] listRoots() | 列出可用系统根 |
public String[] list() | 获取当前路径下所有内容 |
public String[] list(FilenameFilter filter) | 利用文件名过滤器获取当前该路径下所有内容 |
public File[] listFiles() | 获取当前路径下所有内容 |
public File[] listFiles(FilenameFilter filter) | 利用文件名过滤器获取当前该路径下所有内容 |
Filelist
public class Main {
public static void main(String[] args) throws IOException {
File f = new File("C:\\Users\\Guohy\\Desktop\\IOtest");
File[] fs = f.listFiles();
for (File i : fs) {
System.out.println(i);
}
}
}
输出指定路径下的所有文件信息
public class Main {
static void find(File f) {
File[] fs = f.listFiles();
System.out.println(f.getAbsolutePath());
if (fs == null || fs.length == 0) return;
for (File i : fs) find(i);
}
public static void main(String[] args) throws IOException {
File f = new File("C:\\Users\\Guohy\\Desktop");
find(f);
}
}
IO流
- 用于读取文件中的数据 (可以读写文件 或网络中的数据)
- 创建字节流文件对象使用之后一定要使用
close()
方法关闭流 否则可能会发生内存泄露 - 流的方向
- 输入流 (从文件中获取数据)
- 输出流 (输出数据到文件中)
- 操作文件方式
- 字节流 (一次只 输入|输出 一个字节)
- 字符流 (根据编码方式 输入|输出 一个字节)
- 字节流能够操作所有类型的文件 字符流只能操作文本文件
字节输出流 FileOutputStream
构造方法
方法名 | 说明 |
---|
FileOutputStream(File file,boolean append) | 以指定的File 对象创建输入流 |
FileOutputStream(Stiing file,boolean append) | 以指定的字符串为路径创建输入流 |
- 第二个参数为续写开关 如果为 true,则不清空文件 默认为false
常用方法
方法名 | 说明 |
---|
public void write(int b) | 将指定的字节写入文件 |
public void write(byte[] b) | 将 b.length 个字节从指定的字节数组写入此文件 |
public void write(byte[] b, int off, int len) | 从下标off 开始写入len 个字节到此文件 |
public void close() | 关闭此文件输出流并释放与此流相关联的任何系统资源 |
public class Main {
public static void main(String[] args) throws IOException {
FileOutputStream f = new FileOutputStream("a.txt");
f.write(97);
f.write(98);
f.write(99);
f.write(100);
f.write(101);
f.write(102);
f.close();
}
}
字节输入流 FileInputStream
构造方法
方法名 | 说明 |
---|
FileInputStream(File file) | 以指定的File 对象创建输入流 |
FileInputStream(String file) | 以指定的字符串为路径创建输入流 |
如果路径不存在 直接程序终止
常用方法
方法名 | 说明 |
---|
public int read() | 从该输入流读取一个字节的数据 |
public int read(byte[] b) | 从该输入流读取最多 b.length 个字节的数据为字节数组 |
public int read(byte[] b, int off, int len) | 以下标off 开始 从文件中最多读取len 个字节到字节数组 |
public void close() | 关闭此文件输入流并释放与此流相关联的任何系统资源 |
public class Main {
public static void main(String[] args) throws IOException {
FileInputStream f = new FileInputStream("src/Main.java");
while (true) {
int i = f.read();
if (i == -1) break;
System.out.print((char) i);
}
f.close();
}
}
- 字节流只能操作单个字节 汉字在内存中是多个字节 所以字节流不能读取汉字
- 字符流会根据编码一次取一个或多个字符
字符输出流 FileWriter
构造方法
方法名 | 说明 |
---|
FileWriter(File file, boolean append) | 以指定的File 对象创建输入流 |
FileWriter(String fileName, boolean append) | 以指定的字符串为路径创建输入流 |
常用方法
方法名 | 说明 |
---|
public void write(String str) | 把数据写入缓冲区 |
public void flush() | 刷新缓冲区 将缓冲区的数据刷新到本地文件中 |
public void close() | 刷新缓冲区 并且关流 |
public class Main {
public static void main(String[] args) throws IOException {
FileWriter f = new FileWriter("a.txt",true);
f.write("sd");
f.close();
}
}
字符输入流 FileReader
构造方法
方法名 | 说明 |
---|
FileReader(File file) | 以指定的File 对象创建输入流 |
FileReader(String fileName) | 以指定的字符串为路径创建输入流 |
常用方法
方法名 | 说明 |
---|
public int read() | 读取一个字符 |
public int read(char[] cbuf) | 从该输入流读取最多 cbuf.length 个字符到数组 |
public void close() | 关闭流 |