读文件:使用new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
StringBuffer strBuf = new StringBuffer(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); int tempchar; while ((tempchar = bufferedReader.read()) != -1) { strBuf.append((char) tempchar); } bufferedReader.close(); return strBuf.toString();
写文件:使用new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
File file = new File(filePath + "/" + fileName); if (!file.exists()) {// 如果文件不存在则创建 file.createNewFile(); } else { throw new Exception("文件已存在"); } // 获取该文件的缓冲输出流 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); // 写入信息 bufferedWriter.write(content); bufferedWriter.flush();// 清空缓冲区 bufferedWriter.close();// 关闭输出流
原文地址:https://www.cnblogs.com/shengulong/p/9955522.html
时间: 2024-11-02 15:34:34