Java IO——文件操作

xiaoxiao2021-02-28  38

Java IO 中关于文件的操作

涉及到的文件操作如下:

新建文件夹(目录)新建文件删除文件删除空文件夹删除某个文件夹下所有的文件夹(不删除其他非文件夹的文件)清空文件夹(不分文件类型,清空所有文件)读取文件(逐行读取文件)写入文件写入随机文件(RandomAccessFile)读取文件的各项属性设置文件属性(修改文件属性)枚举一个目录下的所有文件(包括该目录下子目录中的文件)复制单个文件(复制多个文件可以多次调用该方法)复制单个文件夹(连同文件夹中的文件一起复制)移动单个文件(原理是先复制该文件,然后将源文件删除)移动单个文件夹

想到其他的以后再补充。

package me.ethan.file; import java.io.*; import java.util.Date; //主要目的是熟悉文件操作 public class FileOperate { //1.创建文件夹 public void establishFolder(String folderPath) { File myFolderPath = new File(folderPath); try { if (!myFolderPath.exists()) { //如果不存在这个文件夹则创建,使用mkdir() myFolderPath.mkdir(); } } catch (Exception e) { System.out.println("创建文件夹失败!"); e.printStackTrace(); } } //2.创建文件 public void establishFile(String filePath) { File myFilePath = new File(filePath); try { if (!myFilePath.exists()) { //如果不存在这个文件则创建此文件,使用createNewFile() myFilePath.createNewFile(); } FileWriter resultFile = new FileWriter(myFilePath); //构造一个FileWriter,用于向其写入数据 PrintWriter myFile = new PrintWriter(resultFile); //构造一个PrintWriter,参数为FileWriter类型的数据 myFile.println("插入文本"); resultFile.close(); //关闭FileWriter类型的对象 } catch (IOException e) { System.out.println("创建文件失败!"); e.printStackTrace(); } } //3.删除文件 public void deleteFile(String filePath) { File myFilePath = new File(filePath); try { myFilePath.delete(); } catch (Exception e) { System.out.println("删除文件失败!"); e.printStackTrace(); } } //4.删除空文件夹 public void deleteFolder(String filePath) { File myFolderPath = new File(filePath); try { myFolderPath.delete(); } catch (Exception e) { System.out.println("删除文件夹失败!"); e.printStackTrace(); } } //5.删除某个文件夹下所有的文件夹(不删除其他非文件夹的文件) public void deleteAllFolders(String filePath) { File myFolderPath = new File(filePath); File[] files = myFolderPath.listFiles(); //创建一个文件数组 try { for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { //判断文件数组中的文件是否是文件夹 files[i].delete(); } } } catch (Exception e) { System.out.println("删除目录下所有文件夹失败!"); e.printStackTrace(); } } //6.清空文件夹(不分文件类型,清空所有文件) public static void deleteAllFiles(String path) { File myFolderPath = new File(path); //判断待删除删除目录是否存在 if (!myFolderPath.exists()) { myFolderPath.mkdir(); //不存在该文件夹则新建一个空文件夹 return; } else { //待删除目录存在 File[] files = myFolderPath.listFiles(); if (!(files.length == 0)) { //文件夹中(即待删除目录下)有内容,遍历目录下文件并删除 for (File file : files) { if (file.isDirectory()) { //判断文件是否是目录(文件夹) deleteAllFiles(file.getAbsolutePath()); //文件是目录,递归调用该方法,删除子目录下文件 file.delete(); //删除空目录 } else { if (!file.delete()) { //文件不是目录(文件夹),直接删除 System.out.println("删除文件失败!"); } } } } } } //7.读取文件(逐行读取文件) public void readFile(String filePath) { try { FileReader fileReader = new FileReader(filePath); BufferedReader bufferedReader = new BufferedReader(fileReader); String str = bufferedReader.readLine(); while (str != null) { System.out.println(str); str = bufferedReader.readLine(); } bufferedReader.close(); fileReader.close(); } catch (Exception e) { System.out.println("读取文件失败!"); e.printStackTrace(); } } //8.写入文件 public void writeFile(String filePath) { try { FileWriter fileWriter = new FileWriter(filePath, true); //第二个参数为true表示追加内容,没有该参数则会重写文件中的内容 fileWriter.write("本行是追加的一行"); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { System.out.println("写入文件失败!"); e.printStackTrace(); } } //9.写入随机文件(可以指定写入位置,这里以追加到文件末尾为例,需要注意编码问题) //使用RandomAccessFile进行文件写入时是零内存写入,写入大文件时有优势 public void writeRandomFile(String filePath) { File myFilePath = new File(filePath); try { RandomAccessFile randomAccessFile = new RandomAccessFile(myFilePath, "rw"); long length = randomAccessFile.length(); randomAccessFile.seek(length); //跳转到文件末尾 randomAccessFile.write("\n再追加一行".getBytes("utf-8")); randomAccessFile.close(); } catch (Exception e) { System.out.println("写入随机文件失败!"); e.printStackTrace(); } } //10.读取文件的各项属性 public void printProperties(String filePath) { File myFilePath = new File(filePath); if (myFilePath.exists()) { System.out.println("文件" + myFilePath.getName() + "的属性如下,文件长度为" + myFilePath.length()); System.out.println(myFilePath.isFile() ? "是文件" : "不是文件"); System.out.println(myFilePath.isDirectory() ? "是目录" : "不是目录"); System.out.println(myFilePath.canRead() ? "可读" : "不可读"); System.out.println(myFilePath.canWrite() ? "可写" : "不可写"); System.out.println("文件的最后修改时间为:" + new Date(myFilePath.lastModified())); } else { System.out.println("指定目录不存在!"); } } //11.设置文件属性(修改文件属性) public void setProperties(String filePath) { File myFilePath = new File(filePath); myFilePath.setLastModified(new Date().getTime()); //设置文件的最后修改时间为当前系统时间 System.out.println("文件的最后修改时间为:" + new Date(myFilePath.lastModified())); } //枚举一个目录下的所有文件(包括该目录下子目录中的文件) public void enumerateAllFiles(String path) { //判断目录是否存在 if (!new File(path).exists()) { //如果目录不存在 System.out.println("当前目录不存在!"); return; } //传入的目录存在 File[] files = new File(path).listFiles(); for (File file : files) { if (file.isDirectory()) { //当前文件是一个文件夹(即一个子目录) System.out.println(file.getName() + "是一个文件夹(目录),其中的文件如下:"); enumerateAllFiles(file.getPath()); //递归调用直至不存在子目录 System.out.println(file.getName() + "中没有文件了。"); } else { System.out.println("文件名为:" + file.getName()); } } //迭代结束 System.out.println("指定目录:" + path + " 中的文件枚举结束!"); } //13.复制单个文件(复制多个文件可以多次调用该方法) public static void copyFile(String oldPath, String newPath) { //判断要复制的文件是否存在 if (!new File(oldPath).exists()) { System.out.println("该文件不存在!"); return; } else { //要复制的文件存在(可以复制) File oldFile = new File(oldPath); File newFile = new File(newPath + File.separator + oldFile.getName()); try { FileInputStream fis = new FileInputStream(oldFile); FileOutputStream fos = new FileOutputStream(newFile); byte[] bytes = new byte[5120]; int n = 0; while ((n = fis.read(bytes)) != -1) { fos.write(bytes, 0, n); //向新文件中写入bytes中的字节,从bytes数组偏移量为0开始,长度为n(bytes.length ) } fis.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } } //14.复制单个文件夹(连同文件夹中的文件一起复制) public static void copyFolder(String oldPath, String newPath) throws IOException { File oldFolder = new File(oldPath); //判断目录(文件夹)是否存在 if (oldFolder.exists() && oldFolder.isDirectory()) { //目录存在 File[] files = oldFolder.listFiles(); //还可以使用list()方法 File newFolder = new File(newPath + File.separator + oldFolder.getName()); //在给定新目录下创建同名文件夹 if (!newFolder.exists()) { newFolder.mkdirs(); } for (File file : files) { if (file.isDirectory()) { //如果是文件夹,递归调用本方法 copyFolder(file.getCanonicalPath(), newFolder.getCanonicalPath()); } if (file.isFile()) { //如果是普通文件 copyFile(file.getCanonicalPath(), newFolder.getCanonicalPath()); } } } else { System.out.println(oldFolder.getAbsolutePath() + "——这个目录不存在!"); } } //15.移动单个文件(原理是先复制该文件,然后将源文件删除) public static void moveFile(String oldPath, String newPath) { //首先复制文件 copyFile(oldPath, newPath); //然后删除源文件 new File(oldPath).delete(); } //16.移动单个文件夹 public static void moveFolder(String oldPath, String newPath) throws IOException { //首先复制文件夹 copyFolder(oldPath, newPath); //然后删除文件夹中的内容 deleteAllFiles(oldPath); //最后删除源文件夹 new File(oldPath).delete(); } public static void main(String[] args) throws IOException { FileOperate.moveFolder("G:/新建文件夹","G:/复制过来的内容"); } }

总结 Java中IO这一块儿比较复杂,没有其他语言那么简单明了,但是胜在可用的库比较多。 理解装饰起设计模式很重要。 平时多看看官方文档。

转载请注明原文地址: https://www.6miu.com/read-3000013.html

最新回复(0)