zoukankan      html  css  js  c++  java
  • 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();
    }
    }
    }
    }
  • 相关阅读:
    vscode, cmake编译多个C++文件
    pytorch下的lib库 源码阅读笔记(1)
    pytorch学习笔记
    SSH无密码登录及远程拷贝命令SCP的使用
    Hadoop启动方式
    Hadoop出现的错误及处理
    Hadoop安装
    Hadoop生态系统图解
    shell脚本入门基础知识
    python中迷茫的编码问题
  • 原文地址:https://www.cnblogs.com/cristin/p/7645531.html
Copyright © 2011-2022 走看看