import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.io.FileUtils; import org.apache.commons.io.LineIterator; /** * 文件工具类 * 2017-05-12 */ public class FileUtil { public static void createDir(String destDirName) { File dir = new File(destDirName); if (!dir.exists()) {// 判断目录是否存在 dir.mkdir(); //dir.mkdirs(); //多层目录需要调用mkdirs } } public static File writeToFile(String content, String filePath) throws IOException { FileWriter fw = null; BufferedWriter bw = null; File file = null; try { file = new File(filePath); if (!file.exists()) { file.createNewFile(); } fw = new FileWriter(file.getAbsoluteFile()); bw = new BufferedWriter(fw); bw.write(content); bw.flush();// 清空缓冲区 } catch (IOException e) { e.printStackTrace(); } finally { if(bw != null) { bw.close(); } if (fw != null) { fw.close(); } } return file; } public static List<String[]> readTxtFile(File file) { LineIterator lineIterator = null; List<String[]> list = null; try { lineIterator = FileUtils.lineIterator(file, "UTF-8"); list = new ArrayList<String[]>(); int lineNum = 0; while (lineIterator.hasNext()) { String line = lineIterator.nextLine(); // 行数据转换成数组 String[] custArray = line.split("\|"); list.add(custArray); lineNum++; if (lineNum % 1000 ==0) { Thread.sleep(10); } } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { if (lineIterator != null) { LineIterator.closeQuietly(lineIterator); } } return list; } /** * 统计文件行数 * @param file * @return * @throws IOException */ public static int getTextLines(File file) throws IOException { FileReader fr = new FileReader(file); //这里定义一个字符流的输入流的节点流,用于读取文件(一个字符一个字符的读取) BufferedReader br = new BufferedReader(fr); // 在定义好的流基础上套接一个处理流,用于更加效率的读取文件(一行一行的读取) int x = 0; // 用于统计行数,从0开始 while(br.readLine() != null) { // readLine()方法是按行读的,返回值是这行的内容 x++; // 每读一行,则变量x累加1 } return x; //返回总的行数 } public static Map<String, Object> readBaofuTxtFile(File file) { LineIterator lineIterator = null; List[] list = null; int startNum = 0; Map<String, Object> map = new HashMap<String, Object>(); try { lineIterator = FileUtils.lineIterator(file, "UTF-8"); list = new List[getTextLines(file)]; int lineNum = 0; while (lineIterator.hasNext()) { String line = lineIterator.nextLine(); // 行数据转换成数组 String[] custArray = line.split("\|"); List<Object> rowData = new ArrayList<Object>(); for (int i = 0; i < custArray.length; i++) { rowData.add(custArray[i]); } if ("商户号".equals(custArray[0])) { startNum = lineNum + 1; } list[lineNum]=rowData; lineNum++; if (lineNum % 500 ==0) { Thread.sleep(10); } } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { if (lineIterator != null) { LineIterator.closeQuietly(lineIterator); } } map.put("data", list); map.put("startLineNum", startNum); return map; } public static void main(String[] args) throws IOException { /*String content = "This is the content to write into file"; String filePath = "c:/test/filename.txt"; writeToFile(content, filePath);*/ File file = new File("c:/test/fi_20170515132029.txt"); /*List list = readTxtFile(file); if (list != null) { for (int i = 0; i < list.size(); i++) { String[] s = (String[]) list.get(i); for (int j = 0; j < s.length; j++) { System.out.println(s[j]); } } }*/ /*Map<String, Object> map = readBaofuTxtFile(file); List[] list = (List[]) map.get("data"); for (int i = 0; i < list.length; i++) { System.out.println(list[i]); }*/ } }