嗨客网搜索
Java文件操作工具类

Java文件操作工具类

在平时编码过程中,对文件的操作时比较频繁的,下面我们总结了一些平时经常使用到的功能。可以将下面所有的方法都维护在一个类里面,在项目中当作公共类来使用。

创建目录

package net.haicoder; import java.io.File; /** * @author 嗨客网 * @description */ public class FileUtil { /** * 新建目录,会自动创建父目录(父目录不存在的时候) * * @param folderPath * @return boolean */ public static boolean mkDirs(String folderPath) { try { File folder = new File(folderPath); if (!folder.exists()) { return folder.mkdirs(); } } catch (Exception e) { e.printStackTrace(); } return false; } }

删除文件

package net.haicoder; import java.io.File; /** * @author 嗨客网 * @description */ public class FileUtil { /** * 删除文件夹(包括文件夹下所有文件)或者文件 * @param path * @return */ public static void delFile(String path) { File folder = new File(path); delFile(folder); } /** * 删除文件夹(包括文件夹下所有文件)或者文件 * @param file */ public static void delFile(File file) { if (file.isDirectory()) { for (File childFile : file.listFiles()) { if (childFile.isFile()) { childFile.delete(); } else if (childFile.isDirectory()) { delFile(childFile); } } } file.delete(); } }

读取行数据

package net.haicoder; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; /** * @author 嗨客网 * @description */ public class FileUtil { /** * 对文件一行一行处理 * @param filePathAndName 本地文件地址 * @param skipFirst 是否跳过第一行 * @param callback 处理接口 * @return */ public static void readLine(String filePathAndName, boolean skipFirst, LineCallback callback) { File file = new File(filePathAndName); if (!file.exists() || file.isDirectory()) { System.out.println("file is error, filePathAndName: " + filePathAndName); return; } FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(file); br = new BufferedReader(fr); String line = null; if (skipFirst) {// 跳过第一行 line = br.readLine(); } while ((line = br.readLine()) != null) { callback.callBack(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fr != null) { fr.close(); } if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 业务可以自己定义实现逻辑类来处理 */ public interface LineCallback { /** * @param line * @return */ void callBack(String line); } }

网络文件下载

package net.haicoder; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; /** * @author 嗨客网 * @description */ public class FileUtil { /** * 超时时间 */ public static final int CONN_TIMEOUT_PERIOD = 10 * 1000; public static final int READ_TIMEOUT_PERIOD = 10 * 1000; private static final String PATH = File.separator; private static int CACHE_BYTES = 1024 * 30; /** * 网络文件下载,不支持断点续传 * @param fileUrl 网络文件地址 * @param localPath 下载到本地位置 */ public static void downloadNetFile(String fileUrl, String localPath, String fileName) { URL url = null; try { url = new URL(fileUrl); } catch (MalformedURLException e) { e.printStackTrace(); } HttpURLConnection httpURLConnection = null; InputStream inputStream = null; RandomAccessFile outputStream = null; try { httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(CONN_TIMEOUT_PERIOD); httpURLConnection.setReadTimeout(READ_TIMEOUT_PERIOD); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setRequestProperty("User-Agent", "JD"); httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); //新建目录,在上面方法中已经定义 mkDirs(localPath); // 使用java中的RandomAccessFile 对文件进行随机读写操作 if (!localPath.endsWith(PATH)) { localPath = localPath + PATH; } File outFile = new File(localPath + fileName); outputStream = new RandomAccessFile(outFile, "rwd"); int code = httpURLConnection.getResponseCode(); String contentType = httpURLConnection.getContentType(); if (!contentType.toLowerCase().startsWith("application/")) { throw new IllegalArgumentException("content type error, mime: " + contentType); } if (code == 200) { inputStream = httpURLConnection.getInputStream(); byte[] buf = new byte[CACHE_BYTES]; int read = 0; while (true) { read = inputStream.read(buf); if (read == -1) { break; } outputStream.write(buf, 0, read); } } else { throw new IllegalArgumentException("response code error, code: " + 200); } } catch (Exception e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); if (outputStream != null) { outputStream.close(); } if (httpURLConnection != null) { httpURLConnection.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } } } }
嗨客网顶部