📄 fileutil.java
字号:
package cn.js.fan.util.file;import java.io.*;import org.apache.log4j.Logger;import cn.js.fan.util.StrUtil;import java.net.URI;public class FileUtil extends Object { private String path; public FileUtil() { } public static String ReadFile(String filePath) throws FileNotFoundException { return ReadFile(filePath, "UTF-8"); } public static String ReadFile(String filePath, String charset) throws FileNotFoundException { String returnStr = ""; BufferedReader reader = null; try { InputStreamReader read = new InputStreamReader (new FileInputStream(filePath),charset); reader = new BufferedReader(read); String line = null; while ((line = reader.readLine()) != null) { returnStr += line + "\r\n"; } read.close(); } catch (IOException e) { System.out.println(FileUtil.class.getName() + " " + e.getMessage()); } finally { try { reader.close(); } catch (Exception e) { } } return returnStr; } public static void WriteFile(String filePath, String str) throws FileNotFoundException { try { PrintWriter pw = new PrintWriter(new FileOutputStream(filePath)); pw.println(str); pw.close(); } catch (IOException e) { System.out.println("写入文件错误:" + e.getMessage()); e.printStackTrace(); } } public static void WriteFile(String filefullpath, String str, String charset) { try { FileOutputStream fo = new FileOutputStream(filefullpath); OutputStreamWriter osw = new OutputStreamWriter(fo, charset); PrintWriter out1 = new PrintWriter(osw); out1.println(str); out1.close(); osw.close(); fo.close(); } catch (IOException e) { System.out.println("FileUtil.java writeFile:" + e.getMessage()); } } public static void WriteFileUTF8(String filefullpath, String str) { WriteFile(filefullpath, str, "UTF-8"); } public static boolean CopyFile(String filePathSrc, String filePathDes) { boolean re = false; File fSrc = new File(filePathSrc); if (!fSrc.exists()) return false; try { if (fSrc.isFile()) { FileInputStream input = new FileInputStream(fSrc); FileOutputStream output = new FileOutputStream(filePathDes); byte[] b = new byte[1024 * 5]; int len; while ((len = input.read(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); re = true; } else System.out.print("Error:" + filePathSrc + " is not found!"); } catch (IOException e) { System.out.print(e.getMessage()); } return re; } public static boolean AppendFile(String desFilePath, String srcFilePath) { boolean re = true; try { RandomAccessFile rf = new RandomAccessFile(desFilePath, "rw"); rf.seek(rf.length()); FileInputStream fis = new FileInputStream(srcFilePath); BufferedInputStream bis = new BufferedInputStream(fis); byte[] buf = new byte[1024]; int len = 0; int totalNum = 0; while ((len=bis.read(buf)) != -1) { rf.write(buf, 0, len); totalNum += len; } bis.close(); fis.close(); rf.close(); } catch (IOException e) { re = false; System.out.println(FileUtil.class.getName() + " AppendFile:" + e.getMessage()); } return re; } public static String getFileExt(String fileName) { if (fileName==null) return ""; int dotindex = fileName.lastIndexOf("."); String extName = fileName.substring(dotindex + 1, fileName.length()); extName = extName.toLowerCase(); return extName; } public static String getFileNameWithoutExt(String fileName) { int dotindex = fileName.lastIndexOf("."); String fName = fileName.substring(0, dotindex); fName = fName.toLowerCase(); return fName; } public static void del(String filepath) throws IOException { File f = new File(filepath); if (f.exists()) { if (f.isDirectory()) { if (f.listFiles().length == 0) { f.delete(); } else { File delFile[] = f.listFiles(); int i = f.listFiles().length; for (int j = 0; j < i; j++) { if (delFile[j].isDirectory()) { del(delFile[j].getAbsolutePath()); } delFile[j].delete(); } } del(filepath); } } else { f.delete(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -