写了一个工具类
/**
* 文件一些基本操作工具类
*
* @author LC
*
*/
public class FileOperateUtil {
/**
* 读取文件(字节流)
*
* @param filePath 文件路径(绝对路径)
* @throws IOException
*/
public static String readFile(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new NullPointerException("IN" + filePath + " NO FILE!");
}
InputStream ins = new FileInputStream(file);
byte[] bs = new byte[(int) file.length()];
ins.read(bs);
ins.close();
return new String(bs, StandardCharsets.UTF_8);
}
/**
* 读取文件 (字符流)
* @param filePath 文件路径(绝对路径)
* @return
* @throws IOException
*/
public static String readFileByChar(String filePath) throws IOException {
Reader reader = new FileReader(filePath);
BufferedReader br = new BufferedReader(reader);
String data = "";
StringBuilder sb = new StringBuilder();
while ((data = br.readLine()) != null) {
sb.append(data);
}
br.close();
reader.close();
return sb.toString();
}
/**
* 写入数据(字节流方式)
*
* @param filePath 文件绝对路劲
* @param data 需写入的数据
* @param isCover true:在文件末尾新增内容。 false:直接覆盖(替换)文件
* @throws IOException
*/
public static void writeFileByByte(String filePath, String data, boolean isCover) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
OutputStream os = new FileOutputStream(file, isCover);
byte[] b = data.getBytes(StandardCharsets.UTF_8);
os.write(b);
os.close();
}
/**
* 写入数据(字符流方式)
*
* @param filePath 文件绝对路劲
* @param data 需写入的数据
* @param isCover true:在文件末尾新增内容。 false:直接覆盖(替换)文件
* @throws IOException
*/
public static void writeFileByChar(String filePath, String data, boolean isCover) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
Writer writer = new FileWriter(filePath, isCover);
writer.write(data);
writer.close();
}
}
单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class FileOperateUtilTest {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Test
public void readFileTest() throws IOException {
String filePath = "D:\\document" + File.separator + "aaa.txt";
String readStr1 = FileOperateUtil.readFile(filePath);
String readStr2 = FileOperateUtil.readFileByChar(filePath);
log.info("==content1:" + readStr1);
log.info("==content2:" + readStr2);
Assert.assertEquals(readStr1, readStr2);
}
@Test
public void writeFileTest() throws IOException {
String filePath = "D:\\document" + File.separator + "aaa.txt";
String readStr= FileOperateUtil.readFileByChar(filePath);
String writeFilePath1 = "D:\\document" + File.separator + "bbb.txt";
FileOperateUtil.writeFileByByte(writeFilePath1, readStr, false);
String writeFilePath2 = "D:\\document" + File.separator + "ccc.txt";
FileOperateUtil.writeFileByChar(writeFilePath2, readStr, false);
String str = "\n你是爱,是暖";
FileOperateUtil.writeFileByChar(writeFilePath2, str, true);
}
}
音频或者图片文件读取操作(BASE64,读取,返回String 给前端读取):
String filePath = "" + markTaskDataDTO.getDataUrl() + markTaskDataDTO.getDataSource();
String suffix = markTaskDataDTO.getDataSource().split("\\.")[1];
String fileSuffix = "data:audio/" + suffix + ";base64,";
if ("3".equals(markTaskDataDTO.getDataType())) { // 图片
fileSuffix = "data:image/" + suffix + ";base64,";
}
String content = fileSuffix + readFileByByte(filePath);
/**
* 读取文件 (字符流)
* @param filePath 文件路径(绝对路径)
* @return
* @throws IOException
*/
private static String readFileByByte(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new NullPointerException("IN" + filePath + " NO FILE!");
}
InputStream ins = new FileInputStream(file); // 得到图片数据
byte[] insByte = new byte[ins.available()];
ins.read(insByte);
ins.close();
Base64 base64 = new Base64();
String encodeData = base64.encodeAsString(insByte);
return encodeData;
}
一些基本的文件操作,可参考以下链接中的博客链接,很全。