Transfer from http://aronlulu.iteye.com/blog/1018370
Read File size: 1.45G
The first kind, Oldio:
Java code
- Public static void Oldioreadfile () throws ioexception{
- BufferedReader br = new BufferedReader (new FileReader ("G://lily_947.txt"));
- PrintWriter pw = new PrintWriter ("g://oldio.tmp");
- char[] C = new char[100*1024*1024];
- for (;;) {
- if (Br.read (c)!=-1) {
- Pw.print (c);
- }else{
- Break ;
- }
- }
- Pw.close ();
- Br.close ();
- }
Time Consuming 70.79s
The second kind, Newio:
Java code
- Public static void Newioreadfile () throws ioexception{
- FileChannel read = new Randomaccessfile ("G://lily_947.txt","R"). Getchannel ();
- FileChannel writer = new Randomaccessfile ("g://newio.tmp","RW"). Getchannel ();
- Bytebuffer BB = bytebuffer.allocate (200*1024*1024);
- While (Read.read (BB)!=-1) {
- Bb.flip ();
- Writer.write (BB);
- Bb.clear ();
- }
- Read.close ();
- Writer.close ();
- }
Time Consuming 47.24s
The Third Kind, randomaccessfile:
Java code
- Public static void Randomreadfile () throws ioexception{
- Randomaccessfile read = new Randomaccessfile ("G://lily_947.txt","R");
- Randomaccessfile writer = new Randomaccessfile ("g://random.tmp","RW");
- byte[] B = new byte[200*1024*1024];
- While (Read.read (b)!=-1) {
- Writer.write (b);
- }
- Writer.close ();
- Read.close ();
- }
Time 46.65
The fourth kind, Mappedbytebuffer:
Java code
- Public static void Mappedbuffer () throws ioexception{
- FileChannel read = new FileInputStream ("G://lily_947.txt"). Getchannel ();
- FileChannel writer = new Randomaccessfile ("g://buffer.tmp","RW"). Getchannel ();
- long i = 0;
- Long size = Read.size ()/30;
- Bytebuffer BB,CC = null;
- While (I<read.size () && (Read.size ()-i) >size) {
- bb = Read.map (FileChannel.MapMode.READ_ONLY, I, size);
- CC = Writer.map (FileChannel.MapMode.READ_WRITE, I, size);
- Cc.put (BB);
- I+=size;
- Bb.clear ();
- Cc.clear ();
- }
- bb = Read.map (FileChannel.MapMode.READ_ONLY, I, read.size ()-i);
- Cc.put (BB);
- Bb.clear ();
- Cc.clear ();
- Read.close ();
- Writer.close ();
- }
Time: 36
The resource occupancy graphs for the first three readings are as follows:
Compared to the last memory direct mapping method before the test actually meaningless, basic second kill ....
There is not enough memory for a large file block map directly, because Mappedbytebuffer is not released, Sun does not provide a way to reclaim the Mappedbytebuffer area directly, and this time there are two ways to solve it, the first of which is rather stupid:
Java code
- System.GC ();
- System.runfinalization ();
- try {
- Thread.Sleep (3000);
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
The second kind of online search, using reflection to call the Clean method:
Java code
- Public static void Unmap (final mappedbytebuffer buffer) {
- if (buffer = = null) {
- return;
- }
- Accesscontroller.doprivileged (new privilegedaction<object> () {
- Public Object Run () {
- try {
- Method Getcleanermethod = Buffer.getclass (). GetMethod ("cleaner", new class[0]);
- if (getcleanermethod! = null) {
- Getcleanermethod.setaccessible (true);
- Object cleaner = getcleanermethod.invoke (buffer, new object[0]);
- Method Cleanmethod = Cleaner.getclass (). GetMethod ("clean", new class[0]);
- if (cleanmethod! = null) {
- Cleanmethod.invoke (cleaner, new object[0]);
- }
- }
- } catch (Exception e) {
- E.printstacktrace ();
- }
- return null;
- }
- });
- }
The above two methods feel awkward, there is the ability to separate into physical files to recycle calls, this is not too beautiful.
The speed will also slow down a lot.
Java read large file operation "Go"