【封装】WriteToFile——数据写入文件

import com.luluteam.main.Statistics;
import org.omg.CORBA.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
 * Created by guan on 11/1/16.
 */
public class WriteToFile {

    /***
     *把数据写入文件
     *
     */
    public static void writeToFile(String fileName, String s, boolean isAppend) {
        try {
            //String pathName = Environment.getExternalStorageDirectory().getPath() + "/BetterUse/webview";
            String pathName= Statistics.DIR;
            //String fileName="json.txt";
            File path = new File(pathName);
            File file = new File(pathName + "/" + fileName);
            if(!path.exists()) {
                path.mkdir();
            }
            if(!file.exists()) {
                file.createNewFile();
            }


            FileOutputStream stream = new FileOutputStream(file, isAppend);
            Writer out = new OutputStreamWriter(stream, "UTF-8");
            //byte[] buf = s.getBytes();
            //stream.write(buf);
            out.write(s);
            out.close();
            stream.close();

            //ApmLogger.info("write to sd card success, fileName: " + fileName);
            //System.out.println("write to sd card success, fileName: " + fileName);

        } catch(Exception e) {
            e.printStackTrace();
        }
    }


}

抓住Demo一只

为了方便以后调用,本人已经将上述方法封装到了.aar文件中。以后调用时,只需要导入第三方包即可。
说明:基于

https://github.com/shanxu100/GUtils

欢迎大家补充交流。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值