java文件写操作


package com.test2;

import java.io.*;

/**
* @desc java文件写操作
* @author zhaozhi3758
*
*/
public class FileWriteT {
//以下各方法文件会自动创建
public void writeToFile1(String path,String str) throws FileNotFoundException{
PrintWriter pw = new PrintWriter(new FileOutputStream(path));
pw.println(str);
pw.flush();
pw.close();
}

public void writeToFile2(String path,String str) throws IOException{
FileOutputStream pw = new FileOutputStream(path);//以覆盖的方式
//FileOutputStream pw = new FileOutputStream(path,true);//以追加的方式
byte abyte[] = str.getBytes();
pw.write(abyte);
pw.flush();
pw.close();
}

public void writeToFile3(String path,String str) throws IOException{
BufferedWriter bwrite = new BufferedWriter(new FileWriter(path,true));//去掉true,非追加
//BufferedWriter bwrite = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path,true)));//OutputStreamWriter 是字符流通向字节流的桥梁,用BufferedWriter再次封装是为了提高效率
bwrite.write(str);
bwrite.flush();
bwrite.close();
}
public static void main(String[] args) throws IOException {

FileWriteT ft=new FileWriteT();
ft.writeToFile3("f:/login.properties","哈哈==sdasd"+"\n"+"#dddd"+"\n");//此种文件加换行符是换行的,但txt格式不行

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值