ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

文件写入

2021-05-28 23:02:57  阅读:140  来源: 互联网

标签:文件 obj filePath 写入 list buf


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 = "\r\n".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() + "\r\n");
      }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) + "\r\n");
      }
      }
      }
      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();
      }
      }
      }
      }

标签:文件,obj,filePath,写入,list,buf
来源: https://blog.51cto.com/u_8056676/2829083

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有