文章目录
1. 文件
1.1 File
1.1.1 方法讲解
唯一与文件、文件目录本身有关的操作类 – 表示文件、目录路径名的抽象表示
创建文件、目录方式
//方法1
File file1 = new File("C:/Users/lrc/Desktop/a")
File file2 = new File("C:/Users/lrc/Desktop/a/b"
//方法2 -- File.separatorChar = "/"
File file1 = new File("C:"+ File.separatorChar +"Users/lrc/Desktop/a")
file1.createNewFile() // 创建文件
file2.mkdir() // 创建目录 -- 多级目录不会创建,并返回false
file2.mkdirs() // 创建多级目录 -- 常用这个
方法测试
class exeFilter implement FileFilter {
@Override
public boolean accept( File file ) {
return file.getPath().endsWith(".exe");
}
}
File file = new File( "E:\Git\Git" );
File[] files = file.listFiles( new exeFilter() );
for( File f : files ) {
System.out.println( f.getName() );
}
方法测试
File file = new File("helloTest.txt");
String relativePath = file.getPath(); // 相对Java项目下的路径
String absolutePath = file.getAbsolutePath(); // 电脑硬盘上真实完整的路径的
System.out.println( relativePath );
System.out.println( absolutePath );
1.1.2 具体应用
1.1.2.1 查找某个目录下的某个文件
class FileTest {
private static List<File> filesList = new ArrayList<File>();
public static void searchFile(File file, String target_fileName) {
// 1. 列出当前目录下的所有文件
File[] files = file.listFiles();
// 2. 循环迭代判断是否跟目标文件名相同
for(File f : files) {
// 3. 如果是目录,则进入此目录查找,是文件,则判断是否是以查找的文件名查找
if(f.isDirectory()) {
searchFile(f, target_fileName);
}else {
if( f.getPath().endsWith(target_fileName) )
filesList.add(f); // 一旦查找到相同则放入静态列表中
}
}
}
// 4. 将查找到的文件以File[]输出,并且清空filesList容器中的文字,方便下次查找
public static Object[] getFiles() {
Object[] files = filesList.toArray();
filesList.clear();
return files;
}
}
//测试用例 -- 结果如下图
File directory = new File( "E:\\Git\\Git" );
FileTest.searchFile( directory, "openssl.cnf" );
for( Object f : FileTest.getFiles() ) {
System.out.println(f);
}
1.2 Properties - 可读写配置文件
Properties 就是个 Hashtable<Object, Object>
常用到Java开发中(额好像这句是废话)
代码可从一个配置文件中读取信息,到时只需要更改文件内容,即可动态的改变程序获取的值,例如:Connection的连接信息,把其放置在配置文件上
获取配置文件数据
简单使用 - 获取配置文件的数据
public static Hashtable<Object, Object> getProperties(String propertiesPath) {
try {
// 1. 将配置文件转为 字节流
InputStream is = new FileInputStream(propertiesPath);
// 2. 创建一个Properties对象
Properties p = new Properties();
// 3. 读取字节流 -- 并且将配置文件的键值对存到Hashtable中
p.load(is);
// 4. 关闭文件字节流
is.close();
return p;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// 测试代码
Hashtable data = getProperties("load.properties");
System.out.println(data);
运行结果
修改配置文件数据
public static void updateProperties(String propertiesPath, HashMap<String, String> datas) {
try {
// 1. 创建一个配置文件属性对象
Properties p = new Properties();
// 2. 将配置文件转为文件输入流 --- 不可在3步骤之前定义该配置文件的输出流
InputStream is = new FileInputStream(propertiesPath);
// 3. 读取字节流 -- 并且将配置文件的键值对存到Hashtable中
p.load(is);
// 4. 关闭文件字节流 - 记得先关输入流,在建该文件的输出流
is.close();
// 5. 将字节流文件 转为处理流 进行 设置 输入的文字编码
OutputStream os = new FileOutputStream(propertiesPath);
OutputStreamWriter osr = new OutputStreamWriter(os, "GBK");
// 6. 将实参的Map转为 集合进来遍历数据更新
Set<Map.Entry<String, String>> elements = datas.entrySet();
for (Map.Entry<String, String> element : elements) {
p.put(element.getKey(), element.getValue());
}
// 7. 将properties更新的数据写入到 配置文件propertiesPath中
p.store(osr, "UpDate-Time: " + new Date());
osr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 测试代码
HashMap<String, String> datas = new HashMap<String,
String>();
datas.put("year", "22");
datas.put("name", "gcw");
datas.put("school", "GDMU");
updateProperties("load.properties", datas);
运行结果
1.3. Path
代表文件的路径对象
代码示例
public static void main(String[] args) throws IOException {
// 参数与参数之间自动根据当前系统的文件分隔符自动的增添
Path path = Paths.get("C:\\Users\\lrc\\Desktop", "线程4,.java");
System.out.println(path);
Path path2 = FileSystems.getDefault().getPath("C:\\Users\\lrc\\Desktop", "线程4,.java");
System.out.println(path2);
File file = new File("C:\\Users\\lrc\\Desktop", "线程4,.java");
Path path3 = file.toPath();
System.out.println(path3);
}
运行结果
1.4 Files – 文件的操纵
1.4.1 简略
自己查看文档查看对应的方法
1.4.2 常用方法介绍
1. write( Path, byte[], OpenOption )
public static void appendContent(String targetPath, String content) {
try {
// 1. 获取目标文件路径对象
Path path = Paths.get(targetPath);
// 2. 将content字符串转为 字节数组, 并将其转为 utf-8编码
content = "\n" + content;
byte[] datas = content.getBytes("utf-8");
// 3. 将内容进行写入到对应的文件
Files.write(path, datas, StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
// 测试代码
String targetPath = "C:\\Users\\lrc\\Desktop\\测试.txt";
String content = "CSDN上的博客帮助了我很多,谢谢你们!";
appendContent( targetPath, content );
测试代码运行后
2. readAllBytes( Path )
public static String readContent(String targetPath) {
try {
Path path = Paths.get(targetPath);
byte[] data = Files.readAllBytes(path);
return new String(data, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//测试代码
String fileContent = readContent("C:\\Users\\lrc\\Desktop\\测试.txt");
System.out.println( fileContent );
运行结果
3. copy( 源Path, 复制Path, CopyOption ) – 文件复制
public static void copyFile2(String sourcePath) {
// 1. 获取源文件的Path对象
Path sourPath = Paths.get(sourcePath);
// 2. 复制的文件名 放在桌面上
Path copyPath = Paths.get("C:\\Users\\lrc\\Desktop", sourPath.getName(sourPath.getNameCount()-1).toString());
// 3. 开始复制
try {
Files.copy(sourPath, copyPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
move( 源Path, 移动Path, CopyOption )
public static void moveFile( String sourcePath ) {
// 1. 获取源文件的Path对象
Path sourPath = Paths.get(sourcePath);
// 2. 移动文件 放在桌面上
Path movePath = Paths.get("C:\\Users\\lrc\\Desktop", sourPath.getName(sourPath.getNameCount() - 1).toString());
// 3. 移动文件
try {
Files.move(sourPath, movePath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}