java將文件讀取到List 對象集合


import org.springframework.util.ReflectionUtils;
import java.io.*;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;
public class FileLoder{

  

  /**
    *按行讀取txt文件並返回一個List<String>集合
    *@param path
    *@return List<String>
   */
  public static List<String> getFileContent(String path,String enccdingType)throws Exception {
    List<String> strList = new ArrayList(~>();
    File file = new File(path);
    InputStreamReader inputStreamReader = null;
    BufferedReader bufferedReader = null;
    FileInputStream fileInputStream= null;
    try {
      fileInputStream = new FileInputStream(file);
      //將字節流向字符流轉換
      inputStreamReader = new InputStreamReader(fileInputStream, encodingType);
      //創建字符流緩沖區
      bufferedReader = nevw BufferedReader (inputStreamReader);
      String line;
      //按行讀取
      while ((line = bufferedReader.readLine()) != null) {
        strlist.add(line);
    }catch(Exception e)
      e.printStackirace();
   }finally{
      if(bufferedReader != null){
        try{
          bufferedReader.close();
        } catch(IOException e){
          e.printStackirace()

        }
      }
      if(inputStreamReader !=null){
        try{
          inputStreamReader.close();
        } catch(IOException e){
          e.printStackirace()
        }
      }
      if(fileInputStream !=null){
        try{
          fileInputStream.close();
        } catch(IOException e){
          e.printStackirace()
        }
      }
    }
  }

  /**
    *@Description 將數組轉換為對象
    *@param class 對象
    *@param argas 數組
    *@return T
    */
  public static<T> T setParam(Class<I> clazz, Object[] args) throws Exception {
    if (clazz == null || args == null) {
      throw new IllegalArgumentException();
    }
    T t = clazz.newInstance();
    Field[] fields = clazz.getDeclaredFields();
    if (fields == null || fields.length > args.length+1){
      throw new IndexOutOfBoundsException();
    }
    for (int i= 0; i< fields.length; i++)
      ReflectionUtils.makeAccessible(fields[i]);
      if(fields[i].getType().toString().contains("Integer")){
        fields[i].set(t,Integer.valueOf(args[i].toString().equal("")?"0":args[i].toString()));
      }else if(fields[i].getIype().toString().contains("BigDecinal")){
        fields[i].set(t,new BigDecimal(args[i].toStzing().equals("")?"0":args[i].toString()));
      }else{
        fields[i].set(t,args[i]);
      }
    }
    return t;
  }

  /**
    *使用StringTokenizer類將字符串安分隔符轉換成數組
    *@param string 字符串
    *@param divisionChar 分隔符
    *@return 字符串數組
    */
  public static String[] stringAnalytical(String string, String divisionChar){
    int i=0;
    StringTokenizer tokenizer = new StringTokenizer(string, divisionChar);
    Stzing[] str = newString[tokenizer.countTokens()];
    while(tokenizer.hasMoreTokens()){
      str[i] = new String();
      str[i] = tokenizer.nextToken();
        i++;
    }
    return str;
  }

  /**

   *使用StringTokenizer類將字符串按int數組長度轉換成數組

   *@Param str 字符串 ints 字符串長度數組

   *@Return 字符串數組

   */
  public static String[] stringAnalytical(String str, int[] ints) throws Exception{
    String[] strs = null;
    List<String> stringList = new ArrayList();
    for(int i = 0;i < ints.length;i++)(
      String strn= CutString.bSubstring(str,ints[i]);
      if(il=ints.length-1){
        str = str.substring(strn.length());
        str = str.substring(1);
      }
      stringList.add(strn);
    }
    strs = stringList.toArray(new String[0]);
    return strs;
  }

        /**

   *@Description 將文件按分隔符讀取到List<Objiect> 對象集合

   *@Param path 文件全路徑 fgf 分隔符  class 對象.class  encodingType 編碼格式

   *@Return List<T> 對象集合

   */

  public static <T> List<T> FileToList(String path,String fgf,Class<T> class,String encodingType) throws Exception{

    List<String> strlist = FileLoder.getFileContent(path,encodingType);

    List<T> list = new ArrayList<>();

    for(String str1 : strlist){

      String[] strz = FileLoder.stringAnalytical(str1,fgf);

      T Object = FileLoder.setParam(class,strz);

      list.add(Object );

    }

    return list;

  }

 

   /**

   *@Description 將文件按各字段定長讀取到List<Objiect> 對象集合

   *@Param path 文件全路徑 ints 各字段長度數組  class 對象.class  encodingType 編碼格式

   *@Return List<T> 對象集合

   */

  public static <T> List<T> FileToList(String path,int[] ints,Class<T> class,String encodingType) throws Exception{

    List<String> strlist = FileLoder.getFileContent(path,encodingType);

    List<T> list = new ArrayList<>();

    for(String str1 : strlist){

      String[] strz = FileLoder.stringAnalytical(str1,ints);

      T Object = FileLoder.setParam(class,strz);

      list.add(Object );

    }

    return list;

  }


}

 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class CutString{
  /**按字節截取字符串
   *@param s 原始字符串
   *@param length 截取的字節長度
   *@return 截取后的字符串
  */
  public static String bSubstring(String s, int length) throws Exception{
    byte[] bytes = s.getBytes("Unicode");
    int n = 0;// 表示當前的字節數
    int i= 2;// 要截取的字節數,從第3個字節開始
    for (;i < bytes.length && n < length;i++){
      //奇數位置,如3、5、7等,為UCS2編碼中兩個字節的第二個字節
      if (i % 2 == 1){
        n++;// 在UCS2第二個字節時n加1
      }else{
        //當UCS2編碼的第一個字節不等於0時,該UCS2字符為漢字,一個漢字算兩個字節
        if(bytes[i] != 0){
          n++;
        }
    }
    //如果i為奇數時,處理成偶數
    if(i%2 == 1){
      //該UCS2字符是漢字時,去掉這個截掉一半的漢字
      if(bytes[i-1]!=0){
        i=i-1;
      }else{
        //該UC52字符是字母膠裝字,保雪該學符
        i=i+1;
      }
    }
    return new String(bytes,0,i,"Unicode");
  }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM