日志规范

LOG使用规范(整理)
Java日志规范
阿里Java开发规范守则解读二(日志篇)
日志规范实践

import com.google.common.collect.Lists;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
public class FileUtil {
  public static List<String> readFileByRandomAccessFile(String fileNameWithPath) {
    List<String> list = new ArrayList<>();
    File file = new File(fileNameWithPath);
    try {
      RandomAccessFile fileR = new RandomAccessFile(file, "r");
      //read whitelist by line
      String str = null;
      while((str = fileR.readLine()) != null) {
        list.add(str);
      }
      fileR.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return list;
  }

 public static List<String> readFileByInputStreamReader(String fileNameWithPath) {
   List<String> list = new ArrayList<>();
   File file = new File(fileNameWithPath);
   try {
     InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(fileNameWithPath));
     BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
     //read by line
     String str;
     while((str = bufferedReader.readLine()) != null) {
       list.add(str);
     }
     bufferedReader.close();
     inputStreamReader.close();
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
   return list;
 }

  public static List<String> readFileByFileReader(String fileNameWithPath) {
    // 使用ArrayList来存储每行读取到的字符串
    List<String> list = new ArrayList<>();
    try {
      FileReader fr = new FileReader(fileNameWithPath);
      BufferedReader bf = new BufferedReader(fr);
      String str;
      // 按行读取字符串
      while ((str = bf.readLine()) != null) {
        list.add(str);
      }
      bf.close();
      fr.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return list;
  }

  public static List<List<String>> readTwoDimensionFile(String fileNameWithPath) {
    List<List<String>> resultList = new ArrayList<>();
    List<String> tempList = new ArrayList<>();
    File file = new File(fileNameWithPath);
    try {
      InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
      String str;
      while((str=bufferedReader.readLine()) != null) {
        tempList.add(str);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    for (String s : tempList) {
      resultList.add(Lists.newArrayList(s.split(",")));
    }
    return resultList;
  }
}

你可能感兴趣的:(JAVA_learning)