文件读取工具类
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class FileUtils { public final static FileUtils ME = new FileUtils(); private FileUtils(){} public String readFile(String path, String fileName) throws IOException { return readFile(path, fileName, System.getProperty("file.encoding")); } public String readFile(String path, String fileName, String encode) throws IOException { if (!path.endsWith("/")){ path += "/"; } if (fileName.startsWith("/")){ fileName = fileName.substring(1); } File file = new File(path+fileName); return readFile(file, encode); } public String readFile(File file, String encode) throws IOException { return new String(readFileBin(file), encode); } public byte[] readFileBin(File file) throws IOException { byte[] temp = new byte[1024]; byte[] result; try (FileInputStream in = new FileInputStream(file);ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();) { while (true){ int size = in.read(temp); if (size<0){ break; } arrayOut.write(temp, 0, size); } result = arrayOut.toByteArray(); } return result; } }
相关推荐
-
java读取excel java
2019-1-8
-
JavaBean与Map的相互转换 java
2019-1-7
-
java分页工具类 java
2019-1-13
-
基于apache-commons-email1.4 邮件发送 java
2019-1-8
-
springBoot版OSS上传功能【可检查魔数】 java
2019-1-7
-
RSA工具类 java
2019-1-8
-
配置文件读取类 java
2019-1-7
-
Java 多线程 下载 java
2019-1-12
-
Java zip 压缩 文件夹删除,移动,重命名,复制 java
2019-1-8
-
springMVC图片上传,删除 java
2019-1-13