日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

FileUtils(文件读写操作工具类)

發布時間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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(文件读写操作工具类)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。