7 RandomAccessFile读取文件内容保存--简单例子(需要验证)

  1 import org.slf4j.Logger;
  2 import org.slf4j.LoggerFactory;
  3 
  4 import java.io.*;
  5 
  6 /**
  7  * 读取动态产生的文件内容
  8  */
  9 public class RandomAccessRead {
 10     public static Logger logger= LoggerFactory.getLogger(RandomAccessRead.class);
 11 
 12     //文件默认读取位置为从开始读取
 13     public static final long DEFAULT_START_POSITION=0;
 14     // 读取文件内容输出时采用的编码格式  默认采用ISO-8859-1编码格式,因为RandAccess读取文件内容为该格式
 15     public static final String DEFAULT_OUTPUT_CHARSET="ISO-8859-1";
 16     //读取文件超时时间,默认-1 没有超时时间
 17     public static final long TIMEOUT=-1;
 18     // 相邻两次读取文件内容时间间隔,默认20ms
 19     public static final long DEFAULT_READ_INTERVAL=20;
 20 
 21     //私有构造方法,防止该对象通过new实例化
 22     private RandomAccessRead(){};
 23 
 24     /**
 25      * 动态读取文件内容
 26      * @param inputPath  输入路径
 27      * @param inputFileName 输入文件名称
 28      * @param outputPath 输出路径
 29      * @param endFlag 文件内容中读取结束标志
 30      * @return 文件读取结束位置
 31      */
 32     public static long read(String inputPath,String inputFileName,String outputPath,String endFlag){
 33        return read(inputPath,inputFileName,outputPath,"",DEFAULT_START_POSITION,DEFAULT_OUTPUT_CHARSET,-1,DEFAULT_READ_INTERVAL,endFlag);
 34     }
 35 
 36     /**
 37      * 读取动态产生的文件内容  执行输入路径、输入文件名称,输出路径,输出文件名称、读取开始位置,输出时使用的编码格式,超时时间,两次相邻读取文件时间间隔
 38      * @param inputPath  读取文件路径
 39      * @param inputFileName  读取文件名称
 40      * @param outputPath 输出文件路径
 41      * @param outputFileName 输出文件名称  不设置默认为源文件名称
 42      * @param startPosition 文件读取开始位置  不设置默认从文件开始位置读取,其值为0
 43      * @param outputCharset 读取文件内容输出时采用的编码格式  不设置默认采用ISO-8859-1编码格式,因为RandAccess读取文件内容为该格式
 44      * @param timeOut 超时时间 读取的文件长时间没有内容产生,则超时,读取结束,不设置默认没有超时时间
 45      * @param readInterval  相邻两次读取文件内容时间间隔,默认20ms
 46      * @param endFlag 读取文件结束标志
 47      * @return 返回本次读取文件内容的长度,方便下次通过该位置读取文件内容
 48      */
 49 
 50     public static long read(String inputPath,String inputFileName,String outputPath,String outputFileName,long startPosition,String outputCharset,long timeOut,long readInterval,String endFlag) {
 51         //判断路径后面是否带“/” linux下为”“
 52         inputPath=inputPath.endsWith(File.separator)==true?inputPath:inputPath+File.separator;
 53         String filePath=inputPath+inputFileName;
 54         long endPosition=startPosition;
 55         //判断文件是否存在
 56         RandomAccessFile randomAccess=null;
 57         try {
 58             randomAccess = new RandomAccessFile(filePath, "r");
 59         } catch (FileNotFoundException e) {
 60             logger.error("读取文件:{} 不存在,请检查文件路径", filePath);
 61             e.printStackTrace();
 62             return endPosition;
 63         }
 64         //初始化输出流
 65         outputPath=outputPath.endsWith(File.separator)==true?outputPath:outputPath+filePath;
 66         outputFileName=outputFileName.equals("")==true?inputFileName:outputFileName;
 67         String outputFilePath=outputPath+outputFileName;
 68         if(!new File(outputFilePath).exists()){
 69             try {
 70                 new File(outputFilePath).createNewFile();
 71             } catch (IOException e) {
 72                 logger.error("输出路径:{}创建失败",outputFilePath);
 73                 e.printStackTrace();
 74                 return endPosition;
 75             }
 76         }
 77         BufferedWriter bw=null;
 78         try {
 79             bw= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outputFilePath)),outputCharset));
 80         } catch (IOException e) {
 81             e.printStackTrace();
 82         }
 83         long startTime=System.currentTimeMillis();
 84         //上次更新数据时间
 85         long preTime=startTime;
 86         //读取结束标识  读到结束标志
 87         Boolean readEnd=false;
 88 
 89         try {
 90             String tempStr=null;
 91             StringBuilder sb=null;
 92             while(true) {
 93                 randomAccess.seek(startPosition);
 94                 sb=new StringBuilder();
 95                 while(null!=(tempStr=randomAccess.readLine())){
 96                      preTime=System.currentTimeMillis();
 97                      //RandomAccessFile读取的文件内容编码为ISO-8859-1,需要转化为指定编码格式
 98                      if(!"".equals(outputCharset)){
 99                         tempStr=new String(tempStr.getBytes(DEFAULT_OUTPUT_CHARSET),outputCharset);
100                      }
101                      if(!tempStr.contains(endFlag)) {
102                          sb.append(tempStr);
103                          sb.append("
");
104                      }else{
105                          readEnd=true;
106                          break;
107                      }
108                     //清空tempStr
109                     tempStr=null;
110                 }
111                 //输出到文件中
112                 bw.write(sb.toString());
113                 bw.flush();
114                 //清空sb
115                 sb=null;
116                 endPosition=randomAccess.length();
117                 if(readEnd)
118                 {
119                     break;
120                 }
121                 //判断超时时间
122                 if(timeOut!=-1&&System.currentTimeMillis()-preTime>timeOut) {
123                     logger.info("读取文件超时,读取退出,输入文件路径:{},输出文件路径:{}",filePath,outputFilePath);
124                     break;
125                 }
126                 //读取文件内容间隔时间  -1表示未设置 则使用默认的时间间隔
127                 try {
128                      if(-1==readInterval) {
129                         readInterval=DEFAULT_READ_INTERVAL;
130                     }
131                     Thread.sleep(readInterval);
132                 } catch (InterruptedException e) {
133                     logger.error("线程挂起出现异常,异常信息为:{}",e.getMessage());
134                     e.printStackTrace();
135                 }
136             //break;
137          }
138         } catch (IOException e) {
139             logger.error("读取文件:{},发生IO异常,异常信息:{}",filePath,e.getMessage());
140             e.printStackTrace();
141         }finally {
142             if(null!=randomAccess)
143             {
144                 try {
145                     randomAccess.close();
146                 } catch (IOException e) {
147                     e.printStackTrace();
148                 }
149             }
150             if(null!=bw)
151             {
152                 try {
153                     bw.close();
154                 } catch (IOException e) {
155                     e.printStackTrace();
156                 }
157             }
158             long endTime=System.currentTimeMillis();
159             logger.info("读取文件时长为:{} ms",(endTime-startTime));
160             logger.info("文件读取结束,输入文件路径:{},输出文件路径:{}",filePath,outputFilePath);
161             return endPosition;
162         }
163     }
164 
165     public static void main(String[] args) {
166         //test1();
167         //test2();
168         test3();
169     }
170 
171     /**
172      * 验证情况1  正常参数
173      */
174     public static void test1(){
175         String inputPath="F:\IDE\DesignPatternWebServer\src\main\java\com\main\etl\server";
176         String inputFileName="RandomAccessRead.java";
177         String outputPath="F:\";
178         String outputFileName="test.java";
179         long startPosition=0;
180         String outputCharset="UTF-8";
181         long timeOut=1000;
182         long readInterval=50;
183         String endFlag="END";
184         long endPosition= RandomAccessRead.read(inputPath,inputFileName,outputPath,outputFileName,startPosition,outputCharset,timeOut,readInterval,endFlag);
185     }
186 
187     /**
188      * 验证情况2  正常参数 不填输出文件名称
189      */
190     public static void test2(){
191         String inputPath="F:\IDE\DesignPatternWebServer\src\main\java\com\main\etl\server";
192         String inputFileName="RandomAccessRead.java";
193         String outputPath="F:\";
194         String outputFileName="";
195         long startPosition=0;
196         String outputCharset="UTF-8";
197         long timeOut=1000;
198         long readInterval=50;
199         String endFlag="END";
200         long endPosition= RandomAccessRead.read(inputPath,inputFileName,outputPath,outputFileName,startPosition,outputCharset,timeOut,readInterval,endFlag);
201     }
202 
203     /**
204      * 验证情况3 简化参数形式
205      */
206     public static void test3(){
207         String inputPath="F:\IDE\DesignPatternWebServer\src\main\java\com\main\etl\server";
208         String inputFileName="RandomAccessRead.java";
209         String outputPath="F:\";
210         String endFlag="END";
211         long endPosition= RandomAccessRead.read(inputPath,inputFileName,outputPath,endFlag);
212     }
213 
214 
215 }

log4j.properties内容如下:

 1  ### set log levels ###
 2 log4j.rootLogger = debug,stdout,R,E
 3 
 4 ### 输出到控制台 ###
 5 log4j.appender.stdout = org.apache.log4j.ConsoleAppender
 6 log4j.appender.stdout.Target = System.out
 7 log4j.appender.stdout.Threshold = INFO
 8 log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
 9 log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} %p [%t] %c{2} (%M:%L) - %m%n
10 
11 ### 输出etl-web日志到文件 ###
12 log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
13 log4j.appender.R.ImmediateFlush=true
14 log4j.appender.R.Append = true
15 log4j.appender.R.File =F:/logs/etl_web.log
16 log4j.appender.R.encoding=UTF-8
17 log4j.appender.R.Threshold = INFO
18 log4j.appender.R.DatePattern='.'yyyy-MM-dd
19 log4j.appender.R.layout = org.apache.log4j.PatternLayout
20 log4j.appender.R.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} %p [%t] %c{2} (%M:%L) - %m%n
21 
22 ### 保存异常信息到单独文件 ###
23 log4j.appender.E = org.apache.log4j.RollingFileAppender
24 log4j.appender.E.File = F:/logs/etl_error.log
25 log4j.appender.E.encoding=UTF-8
26 log4j.appender.E.Append = true
27 log4j.appender.E.Threshold = ERROR
28 log4j.appender.E.layout = org.apache.log4j.PatternLayout
29 log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} %p [%t] %c{2} (%M:%L) - %m%n
原文地址:https://www.cnblogs.com/yangh2016/p/5721478.html