文件操作

  1 package com.iam.utils;
  2 
  3 import java.io.DataOutputStream;
  4 import java.io.File;
  5 import java.io.FileNotFoundException;
  6 import java.io.FileOutputStream;
  7 import java.io.FileReader;
  8 import java.io.IOException;
  9 import java.io.LineNumberReader;
 10 import java.util.LinkedList;
 11 import java.util.Queue;
 12 
 13 import com.iam.constants.CommonConsts;
 14 import com.iam.constants.ToolConsts;
 15 import com.iam.exceptions.MonitorException;
 16 
 17 public class FileUtils {
 18     /**
 19      * @since 2017-06-28
 20      */
 21     public static void main(String[] args) throws MonitorException {
 22         String filePath = FileUtils.class.getResource("/logs/log.txt").getPath();
 23         System.out.println(filePath);
 24         Queue<String> lines = readAppointedLines(new File(filePath), 1, 20);
 25         for (String line : lines) {
 26             System.out.println(line);
 27         }
 28     }
 29 
 30     public static Queue<String> readLinesByFilters(String filter, File localFile) throws MonitorException {
 31         if (!CommonUtils.isFile(localFile)) {
 32             return null;
 33         }
 34         Queue<String> lines = new LinkedList<String>();
 35         LineNumberReader lineReader = null;
 36         FileReader fileReader = null;
 37         try {
 38             fileReader = new FileReader(localFile);
 39             lineReader = new LineNumberReader(fileReader);
 40             String text = lineReader.readLine();
 41             String[] filters = filter.split(ToolConsts.DEFAULT_SPLITTER);
 42             while (null != text) {
 43                 for (String filtor : filters) {
 44                     if (text.contains(filtor)) {
 45                         lines.add(text);
 46                         break;
 47                     }
 48                 }
 49                 text = lineReader.readLine();
 50             }
 51         } catch (FileNotFoundException e) {
 52             e.printStackTrace();
 53             throw new MonitorException("FileNotFoundException occured while read file. "+e.getMessage());
 54         } catch (IOException e) {
 55             e.printStackTrace();
 56             throw new MonitorException("IOException occured while read file. "+e.getMessage());
 57         } finally {
 58             if (null != lineReader) {
 59                 try {
 60                     lineReader.close();
 61                 } catch (IOException e) {
 62                     e.printStackTrace();
 63                 }
 64             }
 65 
 66             if (null != fileReader) {
 67                 try {
 68                     fileReader.close();
 69                 } catch (IOException e) {
 70                     e.printStackTrace();
 71                 }
 72             }
 73         }
 74         return lines;
 75     }
 76 
 77     public static Queue<String> readAppointedLines(File localFile, int from, int to) throws MonitorException {
 78 
 79         if (CommonConsts.INT_ONE > from) {
 80             from = CommonConsts.INT_ONE;
 81         }
 82         if (CommonConsts.INT_ONE > to) {
 83             to = CommonConsts.INT_ONE;
 84         }
 85         if (from > to) {
 86             int temp = to;
 87             to = from;
 88             from = temp;
 89         }
 90         if (!CommonUtils.isFile(localFile)) {
 91             throw new MonitorException("Parameter File Object is NOT a File instance.");
 92         }
 93         return getFileQueue(localFile, from, to);
 94     }
 95 
 96     private static Queue<String> getFileQueue(File file, int from, int to) throws MonitorException {
 97         Queue<String> queue = new LinkedList<String>();
 98         if (!CommonUtils.isFile(file)) {
 99             return null;
100         }
101         FileReader fileReader = null;
102         LineNumberReader lineReader = null;
103         try {
104             fileReader = new FileReader(file);
105             lineReader = new LineNumberReader(fileReader);
106             while (lineReader.getLineNumber() < from - 1) {
107                 lineReader.readLine();
108             }
109             String text = lineReader.readLine();
110             do {
111                 queue.offer(text);
112                 from++;
113                 text = lineReader.readLine();
114             } while (from < to && null != text);
115         } catch (FileNotFoundException e) {
116             e.printStackTrace();
117             throw new MonitorException("FileNotFoundException occured while read file. " + e.getMessage());
118         } catch (IOException e) {
119             e.printStackTrace();
120             throw new MonitorException("IOException occured while read file. " + e.getMessage());
121         }finally {
122             if (null != lineReader) {
123                 try {
124                     lineReader.close();
125                 } catch (IOException e) {
126                     e.printStackTrace();
127                 }
128             }
129 
130             if (null != fileReader) {
131                 try {
132                     fileReader.close();
133                 } catch (IOException e) {
134                     e.printStackTrace();
135                 }
136             }
137         }
138         return queue;
139     }
140 
141     public static void write2File(String filePath, String content) throws Exception {
142         if (CommonUtils.isNotEmpty(filePath) && CommonUtils.isNotEmpty(content)) {
143             File file = new File(filePath);
144             if (CommonUtils.isFile(file)
145                     || (CommonUtils.isNotDirectory(file) && CommonUtils.isDirectory(file.getParentFile()))) {
146                 DataOutputStream output = new DataOutputStream(new FileOutputStream(file, true));
147                 output.write(content.getBytes());
148                 output.write("
".getBytes());
149                 output.close();
150             }
151 
152         }
153     }
154 
155     public static void deleteAllFiles(String Path) throws Exception {
156         if (CommonUtils.isNotEmpty(Path)) {
157             File file = new File(Path);
158             if (file.isDirectory()) {
159                 File[] files = file.listFiles();
160                 for (int i = 0; i < files.length; i++) {
161                     if (files[i].isFile())
162                         files[i].delete();
163                 }
164             }
165         }
166 
167     }
168 
169 }
原文地址:https://www.cnblogs.com/itachy/p/7202719.html