一
老問這問題,兩個都答出來算加分項?
二
具體代碼如下,沒什么好說的直接說對比。
BufferedReader和RandomAccessFile的區別
RandomAccessFile 在數據越大,性能越差。因為他是數據文件的一個channel,支持讀改原數據文件。
BufferedReader是讀改數據文件的一個在內存的副本。
那RandomAccessFile的優點?
1.RandomAccessFile忽略了字符編碼的處理,加快了處理速度
2.若是對數據操作在BufferedReader創立buffer的時候就做完了,RandomAccessFile自然就快了。
ps.
小文件RandomAccessFile,大文件BufferedReader
按行生成文件和按大小生成文件都實現了
BufferedReader提供處理字符編碼的方式,使用InputStreamReader或者DataInputStream之類的。
private static void fileRead() throws IOException { long time = System.currentTimeMillis(); int bufSize = 10 * 1024 * 1024; byte[] bs = new byte[bufSize]; ByteBuffer byteBuf = ByteBuffer.allocate(bufSize); FileChannel channel = new RandomAccessFile(input_path, "r").getChannel(); FileWriter fw = null; for (int i = 0; channel.read(byteBuf) != -1; i++) { byteBuf.rewind(); int size = byteBuf.limit(); byteBuf.get(bs); fw = new FileWriter(String.format(output_path_format1, i)); String line = new String(bs, 0, size); fw.append(line + System.getProperty("line.separator")); fw.flush(); byteBuf.clear(); } fw.close(); time = System.currentTimeMillis() - time; System.out.println("file read time = " + time); } private static void bufferRead() throws IOException { long time = System.currentTimeMillis(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(input_path))); int bufferSize = 10 * 1024 * 1024; BufferedReader in = new BufferedReader(new InputStreamReader(bis, "utf-8"), bufferSize); FileWriter fw = new FileWriter(String.format(output_path_format2, 0)); for (int i = 0; in.ready(); i++) { if (i % 100 == 0) { fw = new FileWriter(String.format(output_path_format2, i / 100)); } String line = in.readLine(); fw.append(line + System.getProperty("line.separator")); if (i % 100 == 0) { fw.flush(); } } in.close(); fw.close(); time = System.currentTimeMillis() - time; System.out.println("buffer read time = " + time); }
這是生成大文件的代碼,修改for循環次數控制文件大小,下面生成的文件大小是2G左右
ps.
操作文件時,盡量使用以下動態的的符號
File.separator是分隔符不同系統是不一樣的
System.getProperty("line.separator")是換行符不同系統是不一樣的
private static String input_path = System.getProperty("user.dir") + File.separator + "data" + File.separator + "bigdata.txt"; private static String output_path_format1 = System.getProperty("user.dir") + File.separator + "data" + File.separator + "part_1_%s.txt"; private static String output_path_format2 = System.getProperty("user.dir") + File.separator + "data" + File.separator + "part_2_%s.txt"; private static int bufSize = 10 * 1024 * 1024; private static void makeBigData() throws IOException { FileWriter fw = new FileWriter(input_path); String line = "start "; for (int i = 0; i < 20000; i++) { line += i; fw.append(line + System.getProperty("line.separator")); } fw.flush(); fw.close(); System.out.println("end"); }
三
因為是自己琢磨的,總感覺寫的有點丑,特別是生成大文件那里,希望各位指正一番。
源碼地址 https://github.com/247292980/spring-boot 。fork的比star還多什么道理啊。