zoukankan      html  css  js  c++  java
  • 文件写入

    package com.bj58.vip.infomgr.utils;

    import java.io.*;
    import java.util.List;
    import org.apache.log4j.Logger;
    import com.sun.org.glassfish.gmbal.Description;

    /**

    • 写文件操作
    • /
      public class FileWriteUtil {
      private final static Logger log = Logger.getLogger(FileWriteUtil.class);
      /
      • 是否存在该文件,无则创建

      • **/
        private static void isExist(String filePath){
        try {
        File file = new File(filePath);
        File fileDir = new File(filePath.replace(file.getName(),""));
        if(!fileDir.exists()){
        fileDir.mkdirs();
        }
        if(!file.exists()){
        file.createNewFile();
        }

        } catch (IOException e) {
        log.debug("...创建文件失败!!!");
        System.out.println("...创建文件失败!!!");
        e.printStackTrace();
        }
        }
        /**

      • 字节形式写入文件

      • @param filePath 文件路径

      • @param obj 写入文件的内容,若list对象表示一行

          • 注意:此方法中文等情况会乱码
      • /
        @Description("字节写入形式!!!支持String、List两种类型")
        public static void charWrite(String filePath,Object obj){
        //文件不存在则创建
        isExist(filePath);
        if(obj==null){
        System.out.println("....写入内容为null!!!!");
        return;
        }
        try {
        //创建写文件器,同时以追加方式写文件
        BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream(filePath,true));
        if(obj instanceof String){
        charWriteDoing(buf,obj.toString());
        }else if(obj instanceof List){
        List list = (List)obj;
        for(int i=0;i<list.size();i++){
        charWriteDoing(buf,list.get(i).toString());
        }
        }
        closeStream(buf);
        } catch (IOException e) {
        log.debug("...写文件失败!!!");
        e.printStackTrace();
        }
        }
        /

      • 字节形式写入

      • /
        private static void charWriteDoing(BufferedOutputStream buf,String val){
        try {
        char[] param = val.toCharArray();
        for (int m = 0; m < param.length; m++) {
        buf.write(param[m]);
        }
        char[] enter = " ".toCharArray();//回车键
        for (int n = 0; n < enter.length; n++) {
        buf.write(enter[n]);
        }
        }catch (Exception e){
        log.debug("...写入过程失败!!!");
        e.printStackTrace();
        }
        }
        /

      • 字符形式写入文件

      • @param filePath 文件路径

      • @param obj 写入文件的内容,若list对象表示一行

      • /
        @Description("字符写入形式!!!支持String、List两种类型")
        public static void byteWrite(String filePath,Object obj){
        //文件不存在则创建
        isExist(filePath);
        if(obj==null){
        System.out.println("....写入内容为null!!!!");
        return;
        }
        try {
        //创建写文件器,同时以追加方式写文件
        BufferedWriter buf = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(filePath,true))));
        if(obj instanceof String){
        buf.write(obj.toString() + " ");
        }else if(obj instanceof List){
        List list = (List)obj;
        if(list!=null&&list.size()>0) {
        for (int i = 0; i < list.size(); i++) {
        buf.write(list.get(i) + " ");
        }
        }
        }
        closeWriter(buf);
        } catch (Exception e) {
        log.debug("...写文件失败!!!");
        e.printStackTrace();
        }
        }
        /

      • 关闭写入流

      • /
        private static void closeWriter(BufferedWriter buf) {
        if (buf != null) {
        try {
        buf.flush();
        buf.close();
        log.debug("...输入流关闭成功");
        } catch (IOException e) {
        log.error(e);
        e.printStackTrace();
        }
        }
        }
        /

      • 关闭写入流

      • **/
        private static void closeStream(BufferedOutputStream buf) {
        if (buf != null) {
        try {
        buf.flush();
        buf.close();
        log.debug("...输入流关闭成功");
        } catch (IOException e) {
        log.error(e);
        e.printStackTrace();
        }
        }
        }
        }

  • 相关阅读:
    iOS开发数据库篇—SQLite的应用
    iOS开发数据库篇—SQL代码应用示例
    iOS开发数据库篇—SQL
    iOS开发数据库篇—SQLite简单介绍
    iOS开发网络篇—NSURLConnection基本使用
    iOS开发网络篇—数据安全
    iOS开发网络篇—GET请求和POST请求
    WordPress主题开发:开启文章缩略图功能
    WordPress主题开发:开启feed功能
    WordPress主题开发:循环代码
  • 原文地址:https://www.cnblogs.com/kevinfuture/p/6038339.html
Copyright © 2011-2022 走看看