java读取txt文件

 

 

public static void readTxtFileByFileUtils(String fileName) {
        File file = new File(fileName);
        
        LineIterator lineIterator = null;
        List list = null;
        try {
            /*//一次读取全部内容
            List lines = FileUtils.readLines(file, "UTF-8");
            System.out.println(lines);*/
            lineIterator = FileUtils.lineIterator(file, "UTF-8");
            list = new ArrayList();
            int lineNum = 0;
            while (lineIterator.hasNext()) {
                String line = lineIterator.nextLine();
                // 行数据转换成数组
                String[] custArray = line.split("\\|");
                list.add(custArray);
                lineNum++;
                System.out.println("---" + 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);
            }
        }
        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]);
                }
            }
        }
    }

 

posted @ 2017-05-10 14:54  这个名字想了很久~  阅读(272)  评论(0编辑  收藏  举报