文件测试
批量对文件直接增加指定后缀(原有后缀变文件名的一部分)
import java.io.File;
// 批量对文件直接增加指定后缀(原有后缀变文件名的一部分)
public class AddFileExt {
public static void main(String[] args) {
String fileDir = "D:\\xxx\\xxx\\xxx";
String ext = ".war";
final File rawFileDirectory = new File(fileDir);
final File[] files = rawFileDirectory.listFiles();
for (File temp : files) {
final String targetFile = temp.getParent() + "/" + temp.getName() + ext;
temp.renameTo(new File(targetFile));
}
}
}
批量对某类文件后缀进行修改
import java.io.File;
// 批量对某类文件后缀进行修改
public class ChangeFileExt {
public static void main(String[] args) {
String fileDir = "D:\\xxx\\xxx\\xxx";
String rawExt = ".zip";
String targetExt = ".war";
final File rawFileDirectory = new File(fileDir);
final File[] files = rawFileDirectory.listFiles();
for (File temp : files) {
if (temp.getName().endsWith(rawExt)) {
final String targetFile = temp.getParent() + "/" + temp.getName().substring(0, temp.getName().lastIndexOf(".")) + targetExt;
temp.renameTo(new File(targetFile));
}
}
}
}
批量删除指定文件类型的后缀
import java.io.File;
// 批量删除指定文件类型的后缀
public class DeleteFileSubExt {
public static void main(String[] args) {
String fileDir = "D:\\xxx\\xxx\\xxx";
String removeExt = ".war";
final File rawFileDirectory = new File(fileDir);
final File[] files = rawFileDirectory.listFiles();
for (File temp : files) {
if (temp.getName().endsWith(removeExt)) {
final String targetFile = temp.getParent() + "/" + temp.getName().substring(0, temp.getName().lastIndexOf("."));
temp.renameTo(new File(targetFile));
}
}
}
}
Java文件之File各个方法获取到的值
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) throws IOException {
final File file = new File("D:\\xxx\\xxx\\test.txt");
System.out.println("file.getName() = " + file.getName());
System.out.println("file.getPath() = " + file.getPath());
System.out.println("file.getParent() = " + file.getParent());
System.out.println("file.getAbsolutePath() = " + file.getAbsolutePath());
System.out.println("file.getCanonicalPath() = " + file.getCanonicalPath());
System.out.println("file.getParentFile() = " + file.getParentFile());
System.out.println();
new FileTest().printThisName();
}
public void printThisName() {
System.out.println("this.getClass().getPackage() = " + this.getClass().getPackage());
System.out.println("this.getClass().getName() = " + this.getClass().getName());
System.out.println("this.getClass().getSimpleName() = " + this.getClass().getSimpleName());
System.out.println("this.getClass().getCanonicalName() = " + this.getClass().getCanonicalName());
System.out.println("this.getClass().getTypeName() = " + this.getClass().getTypeName());
}
}
输出结果
file.getName() = test.txt
file.getPath() = D:\xxx\xxx\test.txt
file.getParent() = D:\xxx\xxx
file.getAbsolutePath() = D:\xxx\xxx\test.txt
file.getCanonicalPath() = D:\xxx\xxx\test.txt
file.getParentFile() = D:\xxx\xxx
this.getClass().getPackage() = package com.test.io
this.getClass().getName() = com.test.io.FileTest
this.getClass().getSimpleName() = FileTest
this.getClass().getCanonicalName() = com.test.io.FileTest
this.getClass().getTypeName() = com.test.io.FileTest
类加载器获取资源路径
import java.io.IOException;
public class ClassLoaderResource {
public static void main(String[] args) throws IOException {
new ClassLoaderResource().print();
}
private void print() throws IOException {
System.out.println("this.getClass().getClassLoader().getResource() = " + this.getClass().getClassLoader().getResource(""));
System.out.println("this.getClass().getClassLoader().getParent() = " + this.getClass().getClassLoader().getParent());
System.out.println("this.getClass().getClassLoader().getResources() = " + this.getClass().getClassLoader().getResources(""));
}
}
输出结果
this.getClass().getClassLoader().getResource() = file:/F:/xxx/xxx/xxx/target/classes/
this.getClass().getClassLoader().getParent() = sun.misc.Launcher$ExtClassLoader@1f89ab83
this.getClass().getClassLoader().getResources() = sun.misc.CompoundEnumeration@6bc168e5