File操作-将数据库里的数据写入到指定路径的txt文件里

package com.Cristin.File;//将数据库里的数据写入到指定路径的txt文件里

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

/**
* Created by cristin on 2017/8/2.
*/

public class FileWriter {

/**
* 将查询出来的内容写入指定路径的文件里
* @param c 内容
* @param path 路径
* @param isAppend 是否写入
* @return
*/
public static boolean writeContent (String c , String path , boolean isAppend){
File file= new File(path);
try{
//是OutputStream的子类,提供了文件的基本写入能力,成为文件字节输出流
FileOutputStream fos = new FileOutputStream(path , isAppend);
//将字节流转换为字符流。如果不指定字符集编码,该解码过程将使用平台默认的字符编码
OutputStreamWriter writer = new OutputStreamWriter(fos , "UTF-8");
writer.write(c);
writer.close();
fos.close();
}catch (IOException e){
e.printStackTrace();
return false;
}
return true;
}

/**
* 查看文件,有则删除再创建,没有则直接创建
*/
public static void checkFile(String filepath){
File file = new File(filepath);
if(!file.exists()){
try{
file.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
return;
}else {
file.delete();
try {
file.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
原文地址:https://www.cnblogs.com/cristin/p/7645531.html