java常用工具類(三)—— 檔案讀取的操作類
阿新 • • 發佈:2018-11-26
- 定義常用的檔案型別
public class FileType { /** * 檔案頭型別 */ public static final String XML_FILE = "text/xml;charset=UTF-8"; public static final String PDF_FILE = "application/pdf"; public static final String PDG_FILE = "application/x-png"; public static final String JPG_FILE = "application/x-jpg"; public static final String WORD_FILE = "application/msword"; public static final String EXCEL_FILE = "application/x-xls"; /** * 檔案型別 */ public static final String XLS = "xls"; public static final String XLSX = "xlsx"; public static final String DOC = "doc"; public static final String TXT = "txt"; public static final String XML = "xml"; public static final String PNG = "png"; public static final String JPG = "jpg"; public static final String PDF = "pdf"; }
- 檔案操作類
import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.zip.DataFormatException; import java.util.zip.Deflater; import java.util.zip.Inflater; /** * @author Lius * @date 2018/10/26 15:58 * @description 檔案工具類 */ public class FileUtils { /** * 關閉檔案流 * * @author Lius * @date 2018/10/26 16:32 */ public static void closeBufferedReader(BufferedReader bufferedReader) { if (bufferedReader != null) { try { bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * 判斷檔案是否存在 * * @author Lius * @date 2018/10/27 10:38 */ public static boolean isExistFile(String filePath) { File file = new File(filePath); if (!file.exists()) { return false; } return true; } /** * 讀檔案 * * @param filePath 讀取檔案路徑 * @return 返回字串 * @author Lius * @date 2018/10/26 15:59 */ public static String readFile(String filePath) { File file = new File(filePath); if (!file.exists()) { return null; } StringBuffer stringBuffer = null; BufferedReader bufferedReader = null; try { bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)); stringBuffer = new StringBuffer(); String line = null; while ((line = bufferedReader.readLine()) != null) { if ("\r".equals(line)) { continue; } stringBuffer.append(line).append("\n"); } } catch (Exception e) { e.printStackTrace(); return null; } finally { closeBufferedReader(bufferedReader); } return stringBuffer.toString(); } /** * 檔案讀取 * * @param filePath 問價路徑 * @return 返回二進位制 * @author Lius * @date 2018/10/26 16:45 */ public static byte[] readFileByByte(String filePath) { File file = new File(filePath); if (!file.exists()) { return null; } byte[] bytes = null; try { bytes = Files.readAllBytes(file.toPath()); } catch (IOException e) { e.printStackTrace(); return null; } return bytes; } /** * 根據檔案路徑獲得檔名 * * @author Lius * @date 2018/10/27 10:19 */ public static String getFileName(String filePath) { String[] splits = filePath.split("\\\\"); return splits[splits.length - 1]; } /** * 通過檔名獲得檔案型別 * * @param fileName 檔名 * @author Lius * @date 2018/10/26 17:44 */ public static String getFileTypeByName(String fileName) { String[] splits = fileName.split("\\."); return splits[splits.length - 1]; } /** * 通過檔案路徑獲得檔案型別 * * @param filePath 檔案路徑 * @author Lius * @date 2018/10/27 10:27 */ public static String getFileTypeByPath(String filePath) { return getFileTypeByName(getFileName(filePath)); } /** * 檔案壓縮 * @author Lius * @date 2018/10/29 13:55 */ public static byte[] compress(byte input[]) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Deflater compressor = new Deflater(1); try { compressor.setInput(input); compressor.finish(); final byte[] buf = new byte[2048]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } } finally { compressor.end(); } return bos.toByteArray(); } /** * 檔案解壓縮 * @author Lius * @date 2018/10/29 13:56 */ public static byte[] uncompress(byte[] input) throws DataFormatException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Inflater decompressor = new Inflater(); try { decompressor.setInput(input); final byte[] buf = new byte[2048]; while (!decompressor.finished()) { int count = decompressor.inflate(buf); bos.write(buf, 0, count); } } finally { decompressor.end(); } return bos.toByteArray(); } }