FileUtils(文件读写操作工具类)
生活随笔
收集整理的這篇文章主要介紹了
FileUtils(文件读写操作工具类)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
File Write and Read
import java.io.*; import java.nio.channels.FileChannel;/*** @author 董云川* @version 1.0* @date 2021/7/28 16:59* 文件操作工具類* 對文件的相關操作* 文件讀取等操作*/ public class FileUtils {private static String TAG = "FileUtils";/*判斷文件是否存在*/public static boolean isExists(String filePath) {File file = new File(filePath);return file.exists();}/*判斷是否是文件夾*/public static boolean isDir(String path) {File file = new File(path);if(file.exists()){return file.isDirectory();}else{return false;}}/*** 文件或者目錄重命名* @param oldFilePath 舊文件路徑* @param newName 新的文件名,可以是單個文件名和絕對路徑* @return*/public static boolean renameTo(String oldFilePath, String newName) {try {File oldFile = new File(oldFilePath);//若文件存在if(oldFile.exists()){//判斷是全路徑還是文件名if (newName.indexOf("/") < 0 && newName.indexOf("\\") < 0){//單文件名,判斷是windows還是Linux系統String absolutePath = oldFile.getAbsolutePath();if(newName.indexOf("/") > 0){//Linux系統newName = absolutePath.substring(0, absolutePath.lastIndexOf("/") + 1) + newName;}else{newName = absolutePath.substring(0, absolutePath.lastIndexOf("\\") + 1) + newName;}}File file = new File(newName);//判斷重命名后的文件是否存在if(file.exists()){System.out.println("該文件已存在,不能重命名");}else{//不存在,重命名return oldFile.renameTo(file);}}else {System.out.println("原該文件不存在,不能重命名");}} catch (Exception e) {e.printStackTrace();}return false;}/*文件拷貝操作*/public static void copy(String sourceFile, String targetFile) {File source = new File(sourceFile);File target = new File(targetFile);target.getParentFile().mkdirs();FileInputStream fis = null;FileOutputStream fos = null;FileChannel in = null;FileChannel out = null;try {fis = new FileInputStream(source);fos = new FileOutputStream(target);in = fis.getChannel();//得到對應的文件通道out = fos.getChannel();//得到對應的文件通道in.transferTo(0, in.size(), out);//連接兩個通道,并且從in通道讀取,然后寫入out通道} catch (IOException e) {e.printStackTrace();} finally {try {if (out != null){out.close();}if (in != null){in.close();}if (fos != null){fos.close();}if (fis != null){fis.close();}} catch (IOException e) {e.printStackTrace();}}}/*讀取Text文件操作*/public static String readText(String filePath) {String lines = "";try {FileReader fileReader = new FileReader(filePath);BufferedReader bufferedReader = new BufferedReader(fileReader);String line = null;while ((line = bufferedReader.readLine()) != null) {lines += line + "\n";}} catch (Exception e) {e.printStackTrace();}return lines;}/*寫入Text文件操作*/public static void writeText(String filePath, String content,boolean isAppend) {FileOutputStream outputStream = null;OutputStreamWriter outputStreamWriter = null;BufferedWriter bufferedWriter = null;try {outputStream = new FileOutputStream(filePath,isAppend);outputStreamWriter = new OutputStreamWriter(outputStream);bufferedWriter = new BufferedWriter(outputStreamWriter);bufferedWriter.write(content);} catch (IOException e) {e.printStackTrace();}finally {try{if(bufferedWriter != null){bufferedWriter.close();}if (outputStreamWriter != null){outputStreamWriter.close();}if (outputStream != null){outputStream.close();}}catch(Exception e){e.printStackTrace();}}}/*** 如果目錄不存在,就創建文件* @param dirPath* @return*/public static String mkdirs(String dirPath) {try{File file = new File(dirPath);if(!file.exists()){file.mkdirs();}}catch(Exception e){e.printStackTrace();}return dirPath;}}總結
以上是生活随笔為你收集整理的FileUtils(文件读写操作工具类)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 淘宝商品微信返利助手开发-(三)返
- 下一篇: mysql指定时间_MySQL查询指定时