1、File类
1.1、file类的三种构造方法
- File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
File f1 = new File("D:/下载/JavaSE.png");
- File(String parent, String child) 从父路径名字符串和子路径名字符串创建新的 File实例。
File f2 = new File("D:/","下载/JavaSE.png");
- File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的 File实例
File file = new File("D:/");
File f3 = new File(file,"下载/JavaSE.png");
1.2、file类的常用方法
获取功能的方法
- public String getAbsolutepath(); 获取绝对路径名字符串
- public String getpath(); 获取相对路径字符串
- public String getName(); 获取文件名或文件夹名
- public long length(); 文件大小的字节长度 ( 文件夹没有大小为 0 )
- public long lastModified(); 获取最后一次的修改时间
File file = new File("a.txt");
System.out.println(file.getPath()); //获取相对路径
System.out.println(file.getAbsolutePath()); //获取绝对路径
System.out.println(file.getName()); //获取文件名或文件夹名
System.out.println(file.length()); //获取文件大小的字节长度(文件夹没有大小默认是0)
long l1 = file.lastModified(); //获取最后修改的时间
Date date = new Date(l1);
String format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
System.out.println(format1);
运行结果:
判断功能的方法
- public boolean isDirectory(); 判断是否是目录
- public boolean isFile(); 判断是否是文件
- public boolean exists(); 判断是否存在
- public boolean canRead(); 判断是否可读
- public boolean canwrite(); 判断是否可写
- public boolean isHidden(); 判断是否隐藏
创建删除功能的方法
- public boolean createNewFile(); 当且仅当具有该名称的文件尚不存在时,原子地创建一个由该抽象路径名命名的新的空文件。
- public boolean mkdir(); 创建由此抽象路径名命名的目录。
- public boolean mkdirs(); 创建由此抽象路径名命名的目录,包括任何必需但不存在的父目录
- public boolean delete(); 删除由此抽象路径名表示的文件或目录。
遍历文件夹目录
- String[] list() 返回一个字符串数组,命名由此抽象路径名表示的目录中的文件和目录。
- File[] listFiles() 返回一个抽象路径名数组,表示由该抽象路径名表示的目录中的文件。
//list遍历返回的是字符数组
File file = new File("D:/下载");
String[] list = file.list();
for (String s : list) {
System.out.println(s);
没有遍历文件夹的文件,遍历不完全
//listfiles遍历返回的是字符数组
public static void getAllfile(String string){
File file = new File(string);
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isDirectory()){
getAllfile(file1.getPath());
}else {
System.out.println(file1); //默认调用的是getputh
}
}
}
遍历结果:全部遍历了
例题:遍历D:/下载 下的所有.java文件
- 第一种
File file = new File(string);
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isDirectory()){
getAllfile(file1.getPath());
}else {
if (file1.getPath().endsWith(".java")){
System.out.println(file1);
}
}
}
遍历结果:
- 第二种:实现过滤器
public static void getAlltofilter(String string){
File file = new File(string);
File[] files = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()){
return true;
}else if(pathname.getPath().endsWith(".java")){
return true;
}else {
return false;
}
}
});
for (File file1 : files) {
if (file1.isDirectory()){
getAlltofilter(file1.getPath());
}else {
System.out.println(file1);
}
}
}
遍历结果:
2、IO流
2.1、字节流
一切文件数据(文本、图片、视频等)都是以二进制的形式保存
字节输入流和输出流
OutputStream和InputStream是抽象类,其具体实现要看其子类
InputStream常用方法:
- abstract int read() 从输入流读取数据的下一个字节。
- void close() 关闭此输入流并释放与流相关联的任何系统资源。
- int read(byte[] b) 从输入流读取一些字节数,并将它们存储到缓冲区 b 。
- int read(byte[] b, int off, int len) 从输入流读取最多 len字节的数据到一个字节数组。
OutputStream常用方法:
- void close() 关闭此输出流并释放与此流相关联的任何系统资源。
- void flush() 刷新此输出流并强制任何缓冲的输出字节被写出。
- void write(byte[] b) 将 b.length字节从指定的字节数组写入此输出流。
- void write(byte[] b, int off, int len) 从指定的字节数组写入 len个字节,从偏移 off开始输出到此输出流。
- abstract void write(int b) 将指定的字节写入此输出流。
1、FileInputStream和FileOutputStream 文件操作流
FileInputStream
构造方法:
- FileInputStream(File file) ; 通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。
- FileInputStream(String name) ; 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
FileOutputStream
构造方法:
- FileOutputStream(File file) ; 创建文件输出流以写入由指定的 File对象表示的文件。
- FileOutputStream(String name) ;创建文件输出流以指定的名称写入文件。
- FileOutputStream(File file, boolean append) ;创建文件输出流以写入由指定的 File对象表示的文件,append设置为true 可追加写。
- FileOutputStream(String name, boolean append) ;创建文件输出流以指定的名称写入文件,append设置为true 可追加写。
测试代码:
//1.FileInputStream 读取 a.txt 文件
FileInputStream f1 = new FileInputStream("a.txt");
FileOutputStream f2 = new FileOutputStream("b.txt", true);
byte[] bytes= new byte[1024];
int len;
while ((len =(f1.read(bytes)))!= -1){
System.out.println(new String(bytes,0,len));
//2.FileOutputStream 向b.txt 中写入数据
//append :true 表示不会覆盖文件数据,可以继续向文件中写入数据
f2.write(bytes,0,len);
}
//3.关闭流资源
f1.close();
f2.close();
2、ObjectInputStream和ObjectOutputStream 序列化流
ObjectOutputStream 序列化
构造方法:
- ObjectOutputStream(OutputStream out)
创建一个写入指定的OutputStream的ObjectOutputStream。
特有方法:
- void writeObject(Object obj) 将指定的对象写入ObjectOutputStream。
ObjectInputStream 反序列化
构造方法:
- ObjecInputStream(InputStream in)
创建一个写入指定的InputStream的ObjectInputStream。
特有方法:
- void readObject(Object obj) 将指定的对象写入ObjectInputStream。
实例代码:
User user = new User("小王",12);
//1、将对象序列化储存到 c.txt 文件中
ObjectOutputStream o1 = new ObjectOutputStream(new FileOutputStream("c.txt"));
o1.writeObject(user);
o1.close();
//2、将 c.txt 文件中存储的对象反序列化,得到对象
ObjectInputStream o2 = new ObjectInputStream(new FileInputStream("c.txt"));
User o = (User)o2.readObject();
System.out.println(o);
注意点:
1、序列化的对象,其实体类要实现 Serializable 接口
2、当某个属性不想被序列化时,可以用 transient 修饰
2.2、字符流
字符输入流和输出流
Reader 和 Writer 是抽象类,其具体实现要看其子类
Reader常用方法:
- abstract void close() 关闭流并释放与之相关联的任何系统资源。
- int read() 读一个字符
- int read(char[] cbuf) ; 将字符读入数组
- read(char[] cbuf, int off, int len) ; 将字符读入数组的一部分。
- read(CharBuffer target) ; 尝试将字符读入指定的字符缓冲区。
Writer常用方法:
- abstract void close() ; 关闭流,先刷新。
- abstract void flush() ; 刷新流。
- void write(char[] cbuf) ; 写入一个字符数组。
- abstract void write(char[] cbuf, int off, int len) ; 写入字符数组的一部分。
- void write(String str) ; 写一个字符串
- void write(String str, int off, int len) ; 写一个字符串的一部分。
1、InputStreamReader和OutputStreamWriter 转换流
InputStreamReader
构造方法:
- InputStreamReader(InputStream in) ;创建一个使用默认字符集的InputStreamReader。
- InputStreamReader(InputStream in, String charsetName) ; 创建一个使用命名字符集的InputStreamReader。
OutputStreamWriter
构造方法:
- OutputStreamWriter(OutputStream out) ; 创建一个使用默认字符编码的OutputStreamWriter。
- OutputStreamWriter(OutputStream out, Charset cs) ; 创建一个使用给定字符集的OutputStreamWriter。
public static void main(String[] args) throws Exception {
InputStreamReader ir1 = new InputStreamReader(new FileInputStream("b.txt"),"UTF-8");
//charsetName 设置文件编码格式 append:true 设置可追加写入
OutputStreamWriter or1 = new OutputStreamWriter(new FileOutputStream("d.txt",true),"GBK");
//1、InputStreamReader读取b.txt 文件
int len = 0;
char[] chars = new char[1024];
while ((len = (ir1.read(chars))) != -1){
String s = new String(chars, 0, len);
System.out.println(s);
//2、OutputStreamWriter 将读取到的b.txt 文件的内容,写入到d.txt
or1.write(s);
}
//3、关闭资源
or1.close();
ir1.close();
}
控制台输出:
b.txt 文件
d.txt 文件
2.3、缓冲流
1、BufferedInputStream和 BufferedOutputStream 字节缓冲流
BufferedInputStream构造方法:
- BufferedInputStream(InputStream in) ; 创建一个 BufferedInputStream并保存其参数,输入流 in ,供以后使用。
- BufferedInputStream(InputStream in, int size) ; 创建 BufferedInputStream具有指定缓冲区大小,并保存其参数,输入流 in ,供以后使用。
测试代码:
BufferedInputStream b1 = new BufferedInputStream(new FileInputStream("a.txt"));
int len = 0;
byte[] bytes = new byte[1024];
while ((len=(b1.read(bytes))) != -1){
String s = new String(bytes, 0, len);
System.out.println(s);
}
b1.close();
BufferedOutputStream构造方法:
- BufferedOutputStream(OutputStream out) ; 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
- BufferedOutputStream(OutputStream out, int size) ; 创建一个新的缓冲输出流,以便以指定的缓冲区大小将数据写入指定的底层输出流。
测试代码:
public static void main(String[] args) throws Exception {
BufferedOutputStream b1 = new BufferedOutputStream(new FileOutputStream("a.txt", true));
b1.write("妖精的尾巴".getBytes());
b1.flush();
b1.close();
}
2、BufferedReader和BufferedWriter 字符缓冲流
BufferReader构造方法:
- BufferedReader(Reader in) ;创建使用默认大小的输入缓冲区的缓冲字符输入流。
- BufferedReader(Reader in, int sz) ;创建使用指定大小的输入缓冲区的缓冲字符输入流。
特有方法:
- String readLine() 读一行文字, 读取到末尾,返回值不是-1 而是 null。
测试代码:
BufferedReader b1 = new BufferedReader(new FileReader("a.txt"));
String len;
while ((len = b1.readLine()) != null){
System.out.println(len);
}
b1.close();
结果:
BufferedWriter构造方法:
- BufferedWriter(Writer out) ; 创建使用默认大小的输出缓冲区的缓冲字符输出流。
- BufferedWriter(Writer out, int sz) ; 创建一个新的缓冲字符输出流,使用给定大小的输出缓冲区。
特有方法:
- newLine() 写入一个行分隔符
测试代码:
BufferedWriter b1 = new BufferedWriter(new FileWriter("a.txt", true));
b1.newLine(); //换行
b1.write("风吹云雾见真容");
b1.flush();
b1.close();
结果:
未完待续…