java 文 件 类(File)
File类:创建和删除文件,不能操作文件内容
File(文件):文件夹/文件(.txt、.ext、压缩包、安装包)
File file = new File(地址);
file.length():文件大小(B、KB、MB、GB、TB、PB)
exists判断文件是否存在
getPath():获取文件的相对路劲
getAbsolutePath():获取文件的绝对路劲
lastModified():获取最后一次修改文件的毫秒值
createNewFile():创建文件
mkdir():创建单层目录
mkdirs():创建多层目录
isFile():判断给定的文件是否是文件
isDirectory():判断给定的文件是否是目录
delete():删除文件,慎用(会直接删除,不能撤回)
getName():获取文件名称
listFiles():获取指定目录下所有子文件
没有参数就是获取所有文件
带参数就是过滤,找到相应文件
canExecute():判断文件是否能打开
canRead():判断文件是否可读
canWrite():判断文件是否可写
setReadOnly():设置文件为只读
setLastModified(10000L):设置文件内容的最后修改时间 isHidden():判断文件是否为隐藏文件
public static void main(String[] args) {
//当前路径在该类所在的工程下
/*
* 路径分类:
* 绝对路径:从盘符开始找文件的位置
* 相对路径(常用):从某个文件所在的位置开始,相对于这个文件,目标文件所在的位置
*/
File file = new File("./src/filetest/Demo01.java");
long l = file.length();
System.out.println(l);
//exists判断文件是否存在
boolean exists = file.exists();
System.out.println(exists);
//获取文件的相对路劲
String path = file.getPath();
System.out.println(path);
//获取文件的绝对路劲
String absolutePath = file.getAbsolutePath();
System.out.println(absolutePath);
//获取最后一次修改文件的毫秒值
long lastModified = file.lastModified();
System.out.println(lastModified);
Date date = new Date(lastModified);
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = s.format(date);
System.out.println(format);
//创建文件
try {
//boolean返回值,表示文件创建成功与否
boolean createNewFile = file.createNewFile();
System.out.println(createNewFile);
} catch (IOException e) {
e.printStackTrace();
}
/* 创建目录
1、创建单层目录
mkdir()
2、创建多层目录
mkdirs()
*/
File file2 = new File("D:/Desktop/test/a.txt");
//必要判断:在创建文件之前,先判断改文件是否存在,如果存在就不创建,不存在才创建
// if (!file2.exists()) {
// file2.mkdirs();
// System.out.println("文件创建成功!");
// }else {
// System.out.println("改文件已存在!");
// }
//isFile():判断给定的文件是否是文件
boolean f = file2.isFile();
System.out.println(f);
//isDirectory():判断给定的文件是否是目录
boolean directory = file2.isDirectory();
System.out.println(directory);
}
获取指定目录下所有子文件(listFiles)
//带参数就是过滤,找到相应文件
//创建类,继承FileFilter类
File[] listFiles2 = file.listFiles(new Filter());
for(File f : listFiles2){
System.out.println(f.getName());
}
//获取某个文件夹下所有的可执行文件(.exe、.bat)
File file2 = new File("D:\\Program Files\\jdk1.8.0_201\\jdk1.8.0_201\\bin");
//匿名内部类形式:
File[] listFiles3 = file2.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
String name = file.getName();
return name.endsWith(".exe") || name.endsWith(".dll") || name.endsWith(".ini");
}
});
for(File f : listFiles3){
System.out.println(f.getName());
}
}
//创建类,继承FileFilter
class Filter implements FileFilter{
@Override
public boolean accept(File file) {
//添加过滤条件,所有以.java结尾的文件
String strName = file.getName();
return strName.endsWith(".java");
}
}
delete(多层文件夹删除)
public static void main(String[] args) {
File file = new File("D:/Desktop/test/a");
delete(file);
}
//delete删除当前目录下所有文件(多层文件夹)
public static void delete(File file){
if (file.isDirectory()) {
File[] listFiles = file.listFiles();
for(File f : listFiles){
delete(f);
}
}
file.delete();
}
RandomAccessFile类
RandomAccessFile:唯一一个可以双向操作文件的类
1、RandomAccessFile(File,"rw"),给文件对象(常用)
2、RandomAccessFile(String,"rw"),给文件名
r:可读
w:可写
//文件指针
1、getFilePointer():获取当前文件指针的位置
2、seek(long):移动文件指针的字节数(设置文件指针所在位置)
3、skipBytes(long):移动文件指针,在当前位置跳过多少字节
length():获取操作的文件长度
read():只读一个字节
write():往文件中写内容,以字节为单位写入
getBytes():字符串转字节数组
写的方法:
r.writeByte(1);
r.writeShort(123);
r.writeLong(10L);
r.writeDouble(1.12);
r.writeFloat(1.2F);
r.writeBoolean(true);
r.writeChar('中');
读的方法:
readInt()
readLong()
readByte()
readShort()
readDouble()
readFloat()
readChar()
readBoolean()
读取文件
//读
public static void main(String[] args) {
try {
RandomAccessFile r = new RandomAccessFile("test.txt", "r");
//只读一个字节
int read = r.read();
System.out.println((char)read);
//读字节数组
byte[] b = new byte[100];
//尝试读取字节数组长度类容,返回值表示实际读取到的字节量
int read2 = r.read(b);
System.out.println(read2);
System.out.println(Arrays.toString(b));
//将byte型数组转为字符串
String str = new String(b);
System.out.println(str);
//读字节数组,存储位置从指定位置开始存,存多少个
//三个参数
raf.read(bs,0,5);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//1、RandomAccessFile(File,"rw"),给文件对象(常用)
//2、RandomAccessFile(String,"rw"),给文件名
try {
File file = new File("test.txt");
if (file.exists() == false) {
file.createNewFile();
}
RandomAccessFile raf = new RandomAccessFile(file, "rw");
//length():获取操作的文件长度
long length = raf.length();
// System.out.println(length);
//write():往文件中写内容,以字节为单位写入
raf.write('a');
//只能写一个字节,而一个字节不能存储汉字,只能写进文件中汉字的一半(中文有两个字节)
String str = "中国人";
byte[] bs = str.getBytes();//字符串转字节数组
for (int i = 0; i < bs.length; i++) {
// raf.write(bs[i]);
}
str = "中国";
bs = str.getBytes();
// raf.write(bs);
// raf.write(bs, 0, 2);
//文件指针
//getFilePointer():获取当前文件指针的位置
//seek(long):移动文件指针的字节数
//skipBytes(long):移动文件指针跳过多少字节
//在文件的末尾追加写
long filePointer = raf.getFilePointer();
System.out.println(filePointer);
raf.seek(raf.length());
filePointer = raf.getFilePointer();
System.out.println(filePointer);
// raf.write("中华有为".getBytes());
long pointer = raf.getFilePointer();
raf.skipBytes(2);
pointer = raf.getFilePointer();
raf.write("007".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
RandomAccessFile r = new RandomAccessFile("test.txt", "rw");
// r.write('d');
String str = "中华有为";
byte[] bytes = str.getBytes();
// r.write(bytes);
// r.writeInt(Integer.MAX_VALUE);
// System.out.println(Integer.MAX_VALUE);
// 写
r.writeByte(1);
// r.writeShort(123);
// r.writeLong(100L);
// r.writeDouble(1.22);
// r.writeFloat(1.5f);
// r.writeBoolean(false);
// r.writeChar('果');
// 读
/* int readInt = r.readInt();
System.out.println(readInt);
r.skipBytes(1);
short readShort = r.readShort();
System.out.println(readShort);
long readLong = r.readLong();
System.out.println(readLong);
double readDouble = r.readDouble();
System.out.println(readDouble);
float readFloat = r.readFloat();
System.out.println(readFloat);
boolean readBoolean = r.readBoolean();
System.out.println(readBoolean);
char readChar = r.readChar();
System.out.println(readChar);*/
} catch (IOException e) {
e.printStackTrace();
}
}
文件的复制(单个字节的复制)
public static void main(String[] args) {
/*
* 文件的复制
* 一个对象读,通过另一个对象写
* 单个字节的复制
*
*/
try {
//读取test.txt文件
RandomAccessFile r = new RandomAccessFile("test.txt", "r");
//复制文件
File file = new File("test_copy.txt");
if (!file.exists()) {
file.createNewFile();
}
RandomAccessFile r2 = new RandomAccessFile(file, "rw");
while(true){
int read = r.read();//读一个写一个
//当读取到的内容为-1时,表示读取到文件末尾
if (read == -1) {
break;
}
r2.write(read);
}
//操作完成后,需要将流关闭
r.close();
r2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
文件的复制(通过字节数组复制)
public static void main(String[] args) {
/*
* 文件的复制
* 一个对象读,通过另一个对象写
* 通过字节数组复制
*
*/
try {
//读取test.txt文件
RandomAccessFile r = new RandomAccessFile("test.txt", "r");
//复制文件
File file = new File("test_copy2.txt");
if (!file.exists()) {
file.createNewFile();
}
RandomAccessFile r2 = new RandomAccessFile(file, "rw");
byte[] b = new byte[1024*10];
while(true){
//实际读取到的字节量
int read = r.read(b);//读一个字节数组,写一个字节数组
//当读取到的内容为-1时,表示读取到文件末尾
if (read == -1) {
break;
}
r2.write(b, 0, read);
}
//操作完成后,需要将流关闭
r.close();
r2.close();
} catch (IOException e) {
e.printStackTrace();
}
}