java聊天室读写工具类_java常用工具类(三)—— 文件读取的操作类

定义常用的文件类型

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值