java 文件的一些基本操作

24135428_rmwl.jpg

写了一个工具类

/**
 * 文件一些基本操作工具类
 * 
 * @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;
	}

一些基本的文件操作,可参考以下链接中的博客链接,很全。

https://www.cnblogs.com/fnlingnzb-learner/p/6010165.html

转载于:https://my.oschina.net/u/3856404/blog/1813394

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值