博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java读取txt文件和覆盖写入txt文件和追加写入txt
阅读量:4320 次
发布时间:2019-06-06

本文共 2345 字,大约阅读时间需要 7 分钟。

package thread; import java.io.*; public class hh {
/** * 创建文件 * @param fileName * @return */ public static boolean createFile(File fileName)throws Exception{
try{
if(!fileName.exists()){
fileName.createNewFile(); } }catch(Exception e){
e.printStackTrace(); } return true; } /** *读取TXT内容 * @param file * @return */ public static String readTxtFile(File file){
String result = ""; try {
InputStreamReader reader = new InputStreamReader(new FileInputStream(file),"gbk"); BufferedReader br = new BufferedReader(reader); String s = null; while((s=br.readLine())!=null){
result = result + s; System.out.println(s); } } catch (Exception e) {
e.printStackTrace(); } return result; } /** * 写入TXT,覆盖原内容 * @param content * @param fileName * @return * @throws Exception */ public static boolean writeTxtFile(String content,File fileName)throws Exception{
RandomAccessFile mm=null; boolean flag=false; FileOutputStream fileOutputStream=null; try {
fileOutputStream = new FileOutputStream(fileName); fileOutputStream.write(content.getBytes("gbk")); fileOutputStream.close(); flag=true; } catch (Exception e) {
e.printStackTrace(); } return flag; } /** * 写入TXT,追加写入 * @param filePath * @param content */ public static void fileChaseFW(String filePath, String content) {
try {
//构造函数中的第二个参数true表示以追加形式写文件 FileWriter fw = new FileWriter(filePath,true); fw.write(content); fw.close(); } catch (IOException e) {
System.out.println("文件写入失败!" + e); } } public static void main(String[] args) throws Exception{
File file = new File("D:\\123wu吴.txt"); createFile(file); readTxtFile(file); // writeTxtFile("我是写入的内容11",file); fileChaseFW("D:\\123wu吴.txt","66666666"); } }

转载于:https://www.cnblogs.com/Simeonwu/p/7565005.html

你可能感兴趣的文章
读《分布式一致性原理》CURATOR客户端3
查看>>
iOS 虚拟机测试出现的相关问题
查看>>
MySQL crash-safe replication(3): MySQL的Crash Safe和Binlog的关系
查看>>
mac 无法打开xx ,因为无法确认开发者身份
查看>>
简单的排序算法(冒泡、选择、插入)
查看>>
[剑指Offer] 11.二进制中1的个数
查看>>
重置报表输出选择
查看>>
ip代理池抓取qq音乐热歌前300
查看>>
Android面试题集合
查看>>
Android NDK开发
查看>>
Centos中安装和配置vsftp简明教程
查看>>
spring源码学习之AOP(一)
查看>>
AES加密算法动画演示
查看>>
三种方法实现调用Restful接口
查看>>
php第五节(字符串函数和时间、日期函数)
查看>>
magento主页限制某个目录的产品显示数量
查看>>
SpringBoot整合Netty
查看>>
MongoDB数据库的基本操作
查看>>
PAT乙级1014
查看>>
ORACLE wm_concat自定义
查看>>