java将⽂件读取到ListT对象集合import org.springframework.util.ReflectionUtils;
import java.io.*;
import flect.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 = adLine()) != 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 = wInstance();
Field[] fields = DeclaredFields();
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 = untTokens()];
while(tokenizer.hasMoreTokens()){
str[i] = new String();
str[i] = Token();
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 = Array(new String[0]);
return strs;
}
/**
*@Description 将⽂件按分隔符读取到List<Objiect> 对象集合
*@Param path ⽂件全路径 fgf 分隔符 class 对象.class encodingType 编码格式
*@Return List<T> 对象集合
java中字符串转数组 */
public static <T> List<T> FileToList(String path,String fgf,Class<T> class,String encodingType) throws Exception{
List<String> strlist = FileContent(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 = FileContent(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");
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论