读取TXT文件,和生成TXT文件的方法

来源:互联网 发布:mcgs组态软件下载 编辑:程序博客网 时间:2024/04/29 18:07

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jp.co.harborportal.bean.common.SelectItemBean;

public class CommonUtil
{
 
 /** STR_BLANK */
 private static final String STR_BLANK = "";
 
 /**
  * Nullチェック
  * <BR>Nullチェック
  * @param objValue
  * @return nullの場合:True
  */
 public static boolean isNull(Object objValue)
 { 
  //String
  if (objValue instanceof String ) {
   String tempStr = (String)objValue;
   
   if (null == tempStr || tempStr.trim().length() == 0) {
    return true;
   }

   return false;
  } else {
   if (objValue == null) {
    return true;
   }
  }
  
  return false;
  
 }
 
 /**
  * 半角文字列であるかどうかチェックする <BR>
  *
  * @param strin
  *            入力文字列
  * @return True: はい False: いいえ
  */
    public static boolean isValidLength(String strin, int max) {
        int count = 0;
        for(int i=0;i<strin.length();i++) {
            char a = strin.charAt(i);
            if(a < 256) {
                count++;
            } else {
                count += 2;
            }
        }
        if(count > max) {
            return true;
        }
       
        return false;

    }
   
 /**
  * FILEから取得文字<BR>
  *
  * @param strin
  *            入力文字列
  * @return True: はい False: いいえ
  */
    public static String readFileByLines(String fileName,String endcoding) {
     StringBuffer tmpStrBuf = new StringBuffer();
     
     File f;
     
     InputStreamReader read = null;
     
     try{
            f = new File(fileName);
            
            if(f.isFile() && f.exists()){
                read = new InputStreamReader (new FileInputStream(f),endcoding);

                BufferedReader   in   =   new   BufferedReader(read);  
                String   line;  
                while((line   =   in.readLine())!=null) { 
                 tmpStrBuf.append(line);
                 tmpStrBuf.append("/n");
                }  
               
                read.close();
            } else {
             return STR_BLANK;
            }
           
        }
        catch(Exception e){
            return STR_BLANK;
        } finally {
         try {
    read.close();
   } catch (Exception e2) {

   }
        }
       
        return tmpStrBuf.toString();
    }
   
    /**
  * 取得の文字をFILEに設定します<BR>
  *
  * @param strin
  *            入力文字列
  * @return True: はい False: いいえ
  */
    public static boolean writeFileByString(String fileName,String strValue,String endcoding) {
     FileOutputStream outputStream = null;
     try{
      int position = fileName.lastIndexOf("/");
      
      String fold = fileName.substring(0, position);
      
      File fileFold = new File(fold);
      
      if (!fileFold.exists()) {
       fileFold.mkdirs();
      }
      
            File f = new File(fileName);
            
            if(!f.exists()){
             f.createNewFile();
            }
           
            outputStream = new FileOutputStream(fileName,true);
         outputStream.write(strValue.getBytes(endcoding));
          
         outputStream.close();
        }
        catch(Exception e){
            return false;
        } finally {
         try {
    outputStream.close();
   } catch (Exception e2) {
    
   }
        }
       
        return true;
    }
   
    /**
  * 空チェック<BR>
  *
  * @param strin
  *            入力文字列
  * @return True: 空 False: 空でない
  */
    public static boolean checkNull(String strValue){
     if (STR_BLANK.equals(strValue.trim())){
      return true;
     }
     return false;
    }
   
    /**
  * 英数チェック<BR>
  *
  * @param strin
  *            入力文字列
  * @return True: はい False: いいえ
  */
    public static boolean checkEngNum(String strValue) {
     String regEx = "^[a-zA-Z0-9]+$"; 
     Pattern pat = Pattern.compile(regEx);  
     Matcher mat = pat.matcher(strValue);  
     boolean rs = mat.find(); 
     return rs;
    }
   
    /**
  * Numチェック<BR>
  *
  * @param strin
  *            入力文字列
  * @return True: はい False: いいえ
  */
    public static boolean checkNum(String strValue) {
     String regEx = "^[0-9]+$"; 
     Pattern pat = Pattern.compile(regEx);  
     Matcher mat = pat.matcher(strValue);  
     boolean rs = mat.find(); 
     return rs;
    }
   
    /**
  * 全角チェック<BR>
  *
  * @param strin
  *            入力文字列
  * @return True: はい False: いいえ
  */
    public static boolean checkZenTaku(String strValue) {
     String regEx = ".*[/u0020-/u007e/uff61-/uffef].*"; 
     Pattern pat = Pattern.compile(regEx);  
     Matcher mat = pat.matcher(strValue);  
     boolean rs = mat.find(); 
     return rs;
    }
   
    /**
  * 半角チェック<BR>
  *
  * @param strin
  *            入力文字列
  * @return True: はい False: いいえ
  */
    public static boolean checkHanKaku(String strValue) {
     String regEx = "^[/u0020-/u007e/uff61-/uffef]+"; 
     Pattern pat = Pattern.compile(regEx);  
     Matcher mat = pat.matcher(strValue);  
     boolean rs = mat.find(); 
     return rs;
    }
   
    /**
  * 日本語チェック<BR>
  *
  * @param strin
  *            入力文字列
  * @return True: はい False: いいえ
  */
    public static boolean checkNihon(String strValue) {
     String regEx = "^[/u0800-/u4e00]+$"; 
     Pattern pat = Pattern.compile(regEx);  
     Matcher mat = pat.matcher(strValue);  
     boolean rs = mat.find(); 
     return rs;
    }
   
   
    /**
  * 中国語チェック<BR>
  *
  * @param strin
  *            入力文字列
  * @return True: はい False: いいえ
  */
    public static boolean checkZhong(String strValue){
     String regEx = "^[/u4e00-/u9fa5]+$";     
     Pattern pat = Pattern.compile(regEx); 
     Matcher mat = pat.matcher(strValue);  
     boolean rs = mat.find(); 
     return rs;
    }
   
    /**
  * Emailチェック<BR>
  *
  * @param strValue 入力文字列
  * @return True: はい False: いいえ
  */
    public static boolean checkEmail(String strValue){
     String regEx = "//w+([-+.]//w+)*@//w+([-.]//w+)*//.//w+([-.]//w+)*";     
     Pattern pat = Pattern.compile(regEx); 
     Matcher mat = pat.matcher(strValue);  
     boolean rs = mat.find(); 
     return rs; 
    }
   
    /**
  * ファイル種類チェック<BR>
  *
  * @param strin
  *            入力文字列
  * @return True: はい False: いいえ
  */
    public static boolean checkFileStyle(String strValue){
     
        int position =strValue.lastIndexOf(".");   
        String imgUrlExtention = strValue.substring(position+1).toLowerCase();
     
     if ("jpeg".equals(imgUrlExtention)
      || "bmp".equals(imgUrlExtention)
      ||"gif".equals(imgUrlExtention)
      ||"jpg".equals(imgUrlExtention)) {
      return true;
     }
     return false;
    }
   
    /**
  * バイトレンスによって、入力文字列を切り捨てる<BR>
  *
  * @param  str 入力文字列
  * @param  byteLength バイトレンス
  * @return 切り捨てられる文字列
  */
 public static String truncateString(String str, int byteLength) {
  if (str.getBytes().length < byteLength) {
   return str;
  } else {
   while (str.getBytes().length > byteLength) {
    str = str.substring(0, str.length() - 1);
   }
   StringBuffer sb = new StringBuffer(byteLength);
   sb.append(str);
   return sb.toString();
  }
 }
 
    /**
  * folder存在チェックします<BR>
  *
  * @param strin
  *            入力文字列
  * @return True: はい False: いいえ
  */
 public static boolean checkFolder (String strValue) {
  File folderFile = new File(strValue);
  
  if (!folderFile.exists()) {
   return folderFile.mkdirs();
  }
  
  return true;
 }
 
 /**
  * 種別名称を取得処理
  */
 public static String getSyubeuMeisyo(String strValue) {
  
  if ("1".equals(strValue)) {
   return "ALL";
  } else if ("2".equals(strValue)) {
   return "実入り搬入";
  } else if ("3".equals(strValue)) {
   return "実入り搬出";
  } else if ("4".equals(strValue)) {
   return "空搬入";
  } else if ("5".equals(strValue)) {
   return "空搬出";
  } else if ("6".equals(strValue)) {
   return "カット日搬入";
  } else if ("7".equals(strValue)) {
   return "搬入";
  } else if ("8".equals(strValue)) {
   return "搬出";
  } else if ("9".equals(strValue)) {
   return "CY";
  } else if ("10".equals(strValue)) {
   return "VP";
  } else {
   return "";
  }
 }
 
 /**
  * 種別リスト取得処理
  */
 public static List<SelectItemBean> getSyubetuListValue() {
  //種別リスト
  List<SelectItemBean> tmpSyubetuList = new ArrayList<SelectItemBean>();
  
  //ALL
  SelectItemBean timeSyubetuBean1 = new SelectItemBean();
  
  timeSyubetuBean1.setKey("1");
  
  timeSyubetuBean1.setValue("ALL");
  
  tmpSyubetuList.add(timeSyubetuBean1);
  
  //実入り搬入
  SelectItemBean timeSyubetuBean2 = new SelectItemBean();
  
  timeSyubetuBean2.setKey("2");
  
  timeSyubetuBean2.setValue("実入り搬入");
  
  tmpSyubetuList.add(timeSyubetuBean2);
  
  //実入り搬出
  SelectItemBean timeSyubetuBean3 = new SelectItemBean();
  
  timeSyubetuBean3.setKey("3");
  
  timeSyubetuBean3.setValue("実入り搬出");
  
  tmpSyubetuList.add(timeSyubetuBean3);
  
  //空搬入
  SelectItemBean timeSyubetuBean4 = new SelectItemBean();
  
  timeSyubetuBean4.setKey("4");
  
  timeSyubetuBean4.setValue("空搬入");
  
  tmpSyubetuList.add(timeSyubetuBean4);
  
  //空搬出
  SelectItemBean timeSyubetuBean5 = new SelectItemBean();
  
  timeSyubetuBean5.setKey("5");
  
  timeSyubetuBean5.setValue("空搬出");
  
  tmpSyubetuList.add(timeSyubetuBean5);
  
  
  //カット日搬入
  SelectItemBean timeSyubetuBean6 = new SelectItemBean();
  
  timeSyubetuBean6.setKey("6");
  
  timeSyubetuBean6.setValue("カット日搬入");
  
  tmpSyubetuList.add(timeSyubetuBean6);
  
  //搬入
  SelectItemBean timeSyubetuBean7 = new SelectItemBean();
  
  timeSyubetuBean7.setKey("7");
  
  timeSyubetuBean7.setValue("搬入");
  
  tmpSyubetuList.add(timeSyubetuBean7);
  
  //搬出
  SelectItemBean timeSyubetuBean8 = new SelectItemBean();
  
  timeSyubetuBean8.setKey("8");
  
  timeSyubetuBean8.setValue("搬出");
  
  tmpSyubetuList.add(timeSyubetuBean8);
  
  //CY
  SelectItemBean timeSyubetuBean9 = new SelectItemBean();
  
  timeSyubetuBean9.setKey("9");
  
  timeSyubetuBean9.setValue("CY");
  
  tmpSyubetuList.add(timeSyubetuBean9);
  
  //VP
  SelectItemBean timeSyubetuBean10 = new SelectItemBean();
  
  timeSyubetuBean10.setKey("10");
  
  timeSyubetuBean10.setValue("VP");
  
  tmpSyubetuList.add(timeSyubetuBean10);
  
  return tmpSyubetuList;
 }
 
 /**
  * 時間HOURリストの値を取得
  */
 public static List<SelectItemBean> getHourList() {
  //時間HOURリストの値を設定します
  int index = 0;
  
  List <SelectItemBean> tmpTimelist = new ArrayList<SelectItemBean>();
  
  while (index < 24) {
   SelectItemBean timeBean = new SelectItemBean();
   
   if (index < 10) {
    String tmpValue = "0" + String.valueOf(index);
    timeBean.setValue(tmpValue);
    timeBean.setKey(tmpValue);
   }else {
    timeBean.setValue(String.valueOf(index));
    timeBean.setKey(String.valueOf(index));
   }
   
   tmpTimelist.add(timeBean);
   
   index++;
  }
  return tmpTimelist;
 }
 
 /**
  * 時間MINUTEリストの値を取得
  */
 public static  List<SelectItemBean> getMinuteList() {
  //時間MINUTEリストの値を設定します
  int indexDate = 0;
  
  List <SelectItemBean> tmpDateList = new ArrayList<SelectItemBean>();
  
  while (indexDate < 60) {
   SelectItemBean timeBean = new SelectItemBean();
   
   if (indexDate < 10) {
    String tmpValue = "0" + String.valueOf(indexDate);
    timeBean.setValue(tmpValue);
    timeBean.setKey(tmpValue);
   } else {
    timeBean.setValue(String.valueOf(indexDate));
    timeBean.setKey(String.valueOf(indexDate));
   }
   
   tmpDateList.add(timeBean);
   
   indexDate++;
  }
  return tmpDateList;
 }
 
 /**
  * ゲートオープン時間リストの値を取得
  */
 public static  List<SelectItemBean> getGateOpenTimeList() {
  //種別リスト
  List<SelectItemBean> tmpSyubetuList = new ArrayList<SelectItemBean>();
  
  //ゲートオープン時間
  SelectItemBean timeSyubetuBean1 = new SelectItemBean();
  
  timeSyubetuBean1.setKey("1");
  
  timeSyubetuBean1.setValue("ゲートオープン時間");
  
  tmpSyubetuList.add(timeSyubetuBean1);
  
  //CY内作業時間
  SelectItemBean timeSyubetuBean2 = new SelectItemBean();
  
  timeSyubetuBean2.setKey("2");
  
  timeSyubetuBean2.setValue("CY内作業時間");
  
  tmpSyubetuList.add(timeSyubetuBean2);
  
  return tmpSyubetuList;
 }
 /**
  * 日付チェック
  */
 public static boolean isDate(String dateStr){
  if (dateStr.contains("/")) {
   return checkDateFormat1(dateStr);
  } else {
   return checkDateFormat2(dateStr);
  }
 }
 
 
 /**
  * 日付チェック
  */
 public static boolean checkDateFormat1(String dateStr){
  String datePat = "^(//d{4})(///)(//d{1,2})(///)(//d{1,2})$";
   Pattern pat = Pattern.compile(datePat); 
  Matcher mat = pat.matcher(dateStr);  
  if (!mat.find()) return false;
  int month = Integer.parseInt(dateStr.split("/")[1]);
  int day = Integer.parseInt(dateStr.split("/")[2]);
  int year = Integer.parseInt(dateStr.split("/")[0]);
  if (month < 1 || month > 12) return false;
  if (day < 1 || day > 31) return false;
  if ((month==4 || month==6 || month==9 || month==11) && day==31) return false;
  if (month == 2)
  {
   boolean isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
   if (day > 29 || (day==29 && !isleap)) return false;
  }
  return true;
 }
 
 /**
  * 日付チェック
  */
 public static boolean checkDateFormat2(String dateStr){
  String datePat = "^(//d{8})$";
   Pattern pat = Pattern.compile(datePat); 
  Matcher mat = pat.matcher(dateStr);  
  if (!mat.find()) return false;
  int month = Integer.parseInt(dateStr.substring(4,6));
  int day = Integer.parseInt(dateStr.substring(6,8));
  int year = Integer.parseInt(dateStr.substring(0,4));
  if (month < 1 || month > 12) return false;
  if (day < 1 || day > 31) return false;
  if ((month==4 || month==6 || month==9 || month==11) && day==31) return false;
  if (month == 2)
  {
   boolean isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
   if (day > 29 || (day==29 && !isleap)) return false;
  }
  return true;
 }
}

// 调用方法

public String init() throws SystemException {
  String strContentFile = CommonUtil.readFileByLines(PropertiesUtil
    .getValue("FQ01001TextPath"), "utf-8");
  String[] arrContentFile = strContentFile.split("/n/n");
  int j;
  textList = new ArrayList<TextBean>();
  
  for (int i = 1; i < arrContentFile.length; i++) {

   if (arrContentFile[i].trim().length() > 0) {
    TextBean txtBean = new TextBean();

    // head
    String temp = arrContentFile[i].replace("<","&lt;");
    temp = temp.replace("/n", "<br>");
    txtBean.setSubHead(temp);

    i++;
    for (int ii = i; ii < arrContentFile.length; ii++) {

     if (arrContentFile[ii].trim().length() > 0) {
      temp =arrContentFile[ii].replace("<","&lt;");
      temp = temp.replace("/n", "<br>");
      txtBean.setSubContent(temp);
      textList.add(txtBean);
      i = ii;
      break;
     }
    }

   }
  }

  return PAGE_THIS;
 }

//jsp页面

<s:iterator value="textList" status="stat">
  <h3><span>
  <s:text
   name="%{textList[#stat.index].subHead}" /> </span></h3>
  <p class="h3-style bottom-20">
  <s:text
   name="%{textList[#stat.index].subContent}" /></p>
 </s:iterator>