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.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.util.Collection; import java.util.Scanner; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.io.LineIterator; public class HandleTextFile { /** * commons.io * @param path */ public static void testReadFile(String path) { try { LineIterator lineIterator = FileUtils.lineIterator(new File(path), "UTF-8"); while (lineIterator.hasNext()) { String line = lineIterator.nextLine(); System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } /** * commons.io */ public static void writeLines(File file, Collection lines, String lineEnding) throws IOException { OutputStream out = null; try { out = new FileOutputStream(file) ; IOUtils.writeLines(lines, lineEnding, out, "UTF-8"); } finally { IOUtils.closeQuietly(out); } } public static void testScannerReadFile(String path) { FileInputStream fileInputStream = null; Scanner scanner = null; try { fileInputStream = new FileInputStream(path); scanner = new Scanner(fileInputStream, "UTF-8"); while (scanner.hasNext()) { String line = scanner.nextLine(); System.out.println(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (scanner != null) { scanner.close(); } } } public static void write() throws IOException{ File file = new File("i:/test/"+File.separator+"hello.txt") ; if(!file.exists()){ file.createNewFile() ; } OutputStream out = new FileOutputStream(file) ; byte[] bt = new byte[1024] ; bt = "aa|bb|cc|dd|ee|ff|gg|hh|jj".getBytes() ; for (int j = 0; j < bt.length; j++) { out.write(bt) ; } out.close(); } public static void readCache(String path) { String filename = path; File file = new File(filename); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file), 10 * 1024 * 1024); //读大文件 设置缓存 String tempString = null; while ((tempString = reader.readLine()) != null) { System.out.println(tempString); } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void write2() throws IOException{ //判断目录是否存在 File file = new File("i:/test/"+File.separator+"hello.txt"); if(!file.exists()){ file.createNewFile() ; } FileWriter fw = new FileWriter("i:/test/"+File.separator+"hello.txt", false) ; BufferedWriter bw = new BufferedWriter(fw) ; for (int i = 0; i < 1000000; i++) { bw.write("aa|bb|cc|dd|ee|ff|gg|hh|jj") ;//文件开始标识 长度20,左对齐,右补空格 bw.newLine(); } bw.flush() ; //将数据更新至文件 bw.close() ; fw.close() ; } public static void main(String[] args) { String path = "i:/test/"+File.separator+"hello.txt" ; long b1 = System.currentTimeMillis() ; testReadFile(path) ; long e1 = System.currentTimeMillis() ; long b2 = System.currentTimeMillis() ; testScannerReadFile(path) ; long e2 = System.currentTimeMillis() ; long b3 = System.currentTimeMillis() ; readCache(path) ; long e3 = System.currentTimeMillis() ; System.out.println("|"+(b1-e1)+"|"+(b2-e2)+"|"+(b3-e3)+"|"); } }
22301|22814|-22760