java常用工具類(三)—— 文件讀取的操作類
阿新 • • 發佈:2018-11-26
() utf-8 cati 解壓縮 操作 char ringbuf 路徑 except
- 定義常用的文件類型
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(); } }
java常用工具類(三)—— 文件讀取的操作類