定义常用的文件类型

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文件读写的基本类_java常用工具类(三)—— 文件读取的操作类相关推荐

  1. java文件读写的基本类_Java读写文件常用方法

    一.字符流:读写纯文本(txt,csv等), 1 字符流写文件主要用:FileWriter,BufferedWriter,PrintWriter 1.1 测试 FileWriter 写入 privat ...

  2. java中拼接xml方法_Java常用工具类,拼接XML格式的字符串,非常好用

    import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.u ...

  3. 常用类 (三) ----- BigDecimal和BigInteger大数类

    相关文章: <常用类 (一) ----- Arrays数组工具类> <常用类 (二) ----- Math类> <常用类 (三) ----- BigDecimal和Big ...

  4. java 取pdf 文本域_java – 使用iText从pdf文件中提取文本列

    我需要使用iText从pdf文件中提取文本. 问题是:一些pdf文件包含2列,当我提取文本时,我得到一个文本文件,其中列被合并为结果(即同一行中两列的文本) 这是代码: public class pd ...

  5. putty怎么进入文件夹_如何利用putty工具上传文件 - 卡饭网

    图解使用FTP工具上传文件以FlashFXP为例 图解使用FTP工具上传文件以FlashFXP为例 现以FlashFXP为例,图解使用FTP工具上传文件. 1: 运行 LeapFTP,点击1处(站点- ...

  6. dll文件32位64位检测工具以及Windows文件夹SysWow64的坑

    自从操作系统升级到64位以后,就要不断的需要面对32位.64位的问题.相信有很多人并不是很清楚32位程序与64位程序的区别,以及Program Files (x86),Program Files的区别 ...

  7. linux文件读写 c语言,Linux 下 C语言大文件读写(大于4G)

    以下的做法整理自论坛上的帖子. 如何create大文件 要大就非常大,1T吧. 有两种方法: 一.dd dd if=/dev/zero of=1T.img bs=1G seek=1024 count= ...

  8. java jdbc操作类_Java-编写一个jdbc操作类

    package数据库操作类;/** Db.java Created on 2007年8月20日, 上午 8:37*/ import java.io.*;import java.sql.*;import ...

  9. java文件读写不完整_Java读取文件内容不完整

    我想把文件一行一行地读出来存入到ArrayList中,countChar是计算文件中的字符个数用的.countChar的结果是对的,可是我在while循环里面逐行打印字符串s后发现,只打印了文件的后半 ...

最新文章

  1. python营销骗局_python案例:金融营销活动中欺诈用户行为分析
  2. 技术人员如何转型为产品经理
  3. java 编码二进制写法、十六进制用源代码表示
  4. SAP标准培训课程C4C10学习笔记(一)第一单元
  5. Qt-调用dll动态链接库
  6. webstorm环境安装配置(less+autoprefixer)
  7. 【Spring】Spring Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required
  8. 在Android中利用iText生成PDF
  9. R 保存包含中文的 eps 图片--showtext
  10. 牛客网_Wannafly模拟赛1
  11. 挑战性题目DSCT103:客观指标评价问题
  12. 犀牛插件-插件开发-rhino-插件开发形式
  13. 计算机中级培训感言,计算机中级培训培训感言
  14. vant swipe 三图一屏
  15. 别了甲骨文,别了拉里·埃里森!
  16. SaaS到底是什么,如何做?——这份我亲手整理的笔记您收好了!
  17. java魔剑天使_大天使之剑大神教你打造牛逼魔剑士
  18. IT人的学习方法论-2,讨论学习的误区
  19. 美中嘉和赴港IPO背后:毛利率大幅下滑,杨建宇控股2家公司均亏损
  20. js canvas绘制 一箭双心

热门文章

  1. 山东大学青岛计算机学院贺平,计算机学院学子在全国数学建模竞赛中再获佳绩...
  2. html字符串替换src,替换html字符串中img标签src的值.
  3. 使用Keras实现鸢尾花分类
  4. 计算机英语讲课笔记04
  5. 2018 ACM/ICPC 沈阳站小结
  6. 【codevs1166】【noip07TG】矩阵取数游戏,新的开始
  7. 10.傅里叶变换——达利画家、基集(Basis Set)、傅里叶级数(Fourier Series)_1
  8. e300氛围灯哪里调节_黑色的奔驰E300有了亚光膜的加持 一出场就惊艳四座
  9. 2017.9.18 数颜色 思考记录
  10. java excel 取值_java实现Excel 单元格取值工具类