java用IO流操作文件工具类(自己写的)

时间:2022-09-16 09:30:29
文件结构目录:
java用IO流操作文件工具类(自己写的)


package com.tool;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
public class FileIOUtil
{
 /**
  * tablename.properties文件路径
  */
 private static String pathName= System.getProperty("user.dir").replace("\\", "/") +"/config/";
 /**
  * tableName文件
  */
 private File tableFile = null;
 //配置文件中保存表名的最大个数
 /*HS_ConfigInfo hs_ConfigInfo = HS_ConfigInfo.getInstance();
 private int maxTableNameNum = Integer.parseInt(HS_ConfigInfo.getDataSaveTimes().trim());*/
 
 HS_ConfigInfo hs_ConfigInfo = null;
 private int maxTableNameNum = 0;
 
 
 public static void main(String[] args)
 {
  List<String> a = new ArrayList<String>();
  a.add("r_e_mp_read_14_03_01");
  a.add("r_e_mp_read_14_03_03");
  a.add("r_e_mp_read_14_03_05");
  a.add("r_e_mp_read_14_03_02");
  a.add("r_e_mp_read_14_03_06");
  a.add("r_e_mp_read_14_03_04");
  a.add("r_e_mp_read_14_03_08");
  a.add("r_e_mp_read_14_03_07");
 
  try
  {
   //删除一个表名
   //fileIOUtil.tableNameDel(a.get(2));
   //增加一个表名
   //fileIOUtil.tableNameAdd(a.get(3));
   FileIOUtil fileIOUtil = new FileIOUtil("r_e_mp_day_demand_tablename");
   fileIOUtil.writeFile(a);
   
   System.out.println(fileIOUtil.tableNameList().toString());
   
  }
  catch (IOException e)
  {
   e.printStackTrace();
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }
 //构造函数
 public FileIOUtil(String filename) throws IOException
 {
  filename = pathName+ filename +".properties";
  tableFile = new File(filename);
  hs_ConfigInfo = HS_ConfigInfo.getInstance();
  maxTableNameNum = Integer.parseInt(hs_ConfigInfo.getDataSaveTimes().trim());
  filename = null;
 }
 
 //从文件中读取数据
 public List<String> readFile() throws IOException{
  FileInputStream inputStream = null;
  InputStreamReader inputStreamReader = null;
  BufferedReader bufReader = null;
  ArrayList<String> tableNameList = new ArrayList<String>();
  try
  {
   inputStream = new FileInputStream(tableFile);
   inputStreamReader = new InputStreamReader(inputStream);
   bufReader = new BufferedReader(inputStreamReader);
   String str = null;
   //循环按行读取文件中的内容
   while((str= bufReader.readLine()) != null){
    if(str.trim().length()>0){
     tableNameList.add(str);
    }
    str = null;
   }
   
   //对结果集降序排列后返回
   return sortStr(tableNameList);
  }
  catch (FileNotFoundException e)
  {
   throw e;
  }
  catch (IOException e)
  {
   throw e;
  }finally{
   try
   {
    if(inputStream != null){
     inputStream.close();
     inputStream = null;
    }
    if(inputStreamReader != null){
     inputStreamReader.close();
     inputStreamReader = null;
    }
    if(bufReader != null){
     bufReader.close();
     bufReader = null;
    }
   }
   catch (IOException e)
   {
    throw e;
   }
  }
 }
 
 //往文件中写入数据
 public boolean writeFile(List<String> tableNameList) throws Exception{
  FileOutputStream outputStream = null;
  OutputStreamWriter outputStreamWriter = null;
  BufferedWriter bufWriter = null;
 
  try
  {
   //FileOutputStream的第二个参数为true,就是将写入的内容追加到文件末尾
   outputStream = new FileOutputStream(tableFile,true);
   outputStreamWriter = new OutputStreamWriter(outputStream);
   bufWriter = new BufferedWriter(outputStreamWriter);
   tableNameList = sortStr(tableNameList);
   //在向文件中写入内容之前首先将文件中的内容清空
   if(clearFile(tableFile)){
    int size = (tableNameList.size()<=maxTableNameNum)?tableNameList.size():maxTableNameNum;
    for(int i=0; i<size; i++){
     bufWriter.write(tableNameList.get(i));
     bufWriter.newLine();
    }
   }
   
   bufWriter.flush();
   return true;
   
  }
  catch (FileNotFoundException e)
  {
   throw e;
  }
  catch (IOException e)
  {
   throw e;
  }
  catch (Exception e)
  {
   throw e;
  }finally{
   if(outputStream != null){
    outputStream.close();
    outputStream = null;
   }
   
   if(outputStreamWriter != null){
    outputStreamWriter.close();
    outputStreamWriter = null;
   }
   if(bufWriter != null){
    bufWriter.close();
    bufWriter = null;
   }
  }
 }
 
 //字符串排序算法(降序排列)
 public List<String> sortStr(List<String> strList){
  String tempStr = null;
  for(int i=0; i<strList.size(); i++){
   for(int j=0; j<(strList.size()-1) - i; j++){
    //降序排列
    if((strList.get(j).compareTo(strList.get(j+1)))<0 ){
     tempStr = strList.get(j);
     strList.set(j, strList.get(j+1));
     strList.set(j+1, tempStr);
    }
    tempStr = null;
   }
  }
  return strList;
 }
 
 public boolean clearFile(File file) throws Exception{
  FileOutputStream outputStream = null;
  OutputStreamWriter outputStreamWriter = null;
  BufferedWriter bufWriter = null;
 
  try
  {
   outputStream = new FileOutputStream(file);
   outputStreamWriter = new OutputStreamWriter(outputStream);
   bufWriter = new BufferedWriter(outputStreamWriter);
   bufWriter.write("");
   bufWriter.flush();
   return true;
  }
  catch (FileNotFoundException e)
  {
   throw e;
  }
  catch (IOException e)
  {
   throw e;
  }finally{
   try
   {
    if(outputStream != null){
     outputStream.close();
     outputStream = null;
    }
   
    if(outputStreamWriter != null){
     outputStreamWriter.close();
     outputStreamWriter = null;
    }
    if(bufWriter != null){
     bufWriter.close();
     bufWriter = null;
    }
   }
   catch (Exception e)
   {
    throw e;
   }
  }
 }
 //给配置文件中增加表名
 public boolean tableNameAdd(String tableName) throws Exception{
  try
  {
   List<String> fileList = readFile();
   int fileSize = 0;
   fileSize = fileList.size();
   
   if(fileSize >= maxTableNameNum){
    if(!fileList.contains(tableName)){
     fileList.set((maxTableNameNum-1), tableName);
    }
   }else{
    if(!fileList.contains(tableName)){
     fileList.add(fileSize, tableName);
    }
   
   }
   return writeFile(fileList);
  }
  catch (Exception e)
  {
   throw e;
  }
 }
 
 //删除配置文件中的表名
 public boolean tableNameDel(String tableName) throws Exception {
 
  boolean result = false;
  try
  {
   List<String> tableNameList = readFile();
   if(tableNameList.contains(tableName)){//删除的表名在配置文件中存在
    tableNameList.remove(tableName);
    result = writeFile(tableNameList);
   }else{//删除的表名不存在
    result = false;
   }
   return result;
  }
  catch (IOException e)
  {
   throw e;
  }
  catch (Exception e)
  {
   throw e;
  }
 }
 
 //得到配置文件中的tablename集合列表
 public List<String> tableNameList() throws IOException{
  return readFile();
 }
}