文件写入

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();
      }
      }
      }
      }

posted @ 2016-11-07 12:36  kevinfuture  阅读(267)  评论(0编辑  收藏  举报