IO

来源:互联网 发布:telnet ping 端口 编辑:程序博客网 时间:2024/05/19 06:49

                        BufferedReader br = new BufferedReader(new FileReader(fileName));
                        String line;
                        long starttime = System.currentTimeMillis();
                        while((line=br.readLine())!=null)
                        {
                                //System.out.println(line);
                        }
                       
                        long endtime = System.currentTimeMillis();
                        System.out.println(endtime - starttime);
                        br.close();

--------------

public static void main(String[] args) throws Exception {
    int bufSize = 1024;
    byte[] bs = new byte[bufSize];
    // 这里是分配缓存大小。也就是用来存放从硬盘中度出来的文件
    // 什么叫一次把文件读出来?其实就是当缓存大小和在硬盘中文件大小一样,
    // 只通过一个read指令把整个文件都扔到缓存里面。例如要一次读一个2G的文件,把缓存设为2G就能一次读出来。
    // 不过当分配空间的时候,这个缓存根本是分配不出来的,因为内存不足。
    ByteBuffer byteBuf = ByteBuffer.allocate(bufSize);
    FileChannel channel = new RandomAccessFile("d://filename","r").getChannel();
    int size;
    // 因为这里缓存大小是1K,所以每个channel.read()指令最多只会读到文件的1K的内容。
    // 如果文件有1M大小,这里for会循环1024次,把文件分开1024次读取出来
    while((size = channel.read(byteBuf)) != -1) {
      byteBuf.rewind();
      byteBuf.get(bs);
      // 把文件当字符串处理,直接打印做为一个例子。
      System.out.print(new String(bs, 0, size));
      byteBuf.clear();
    }
    channel.close();
  }
--------------------------------

1   JAVA中可以使用内存映射文件来操作大文件.
最大可达2GB.
下面是个简单的示例,更具体的自己看Java API DOCS或相关资料
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class LargeMappedFiles {
   static int length = 0x8FFFFFF; // 128 Mb
   public static void main(String[] args) throws Exception {
     MappedByteBuffer out = 
       new RandomAccessFile("test.dat", "rw").getChannel()
       .map(FileChannel.MapMode.READ_WRITE, 0, length);
     for(int i = 0; i < length; i++)
       out.put((byte)'x');
     System.out.println("Finished writing");
     for(int i = length/2; i < length/2 + 6; i++)
       System.out.print((char)out.get(i));    //read file
   }
} ///


2 RandomAccessFile对于过大文件,不会一次性全部读出,而是一次一小段。读取365M的大文件,内存占用也只有51KB
//------------------------
import help.Functions;
import java.io.RandomAccessFile;

public class A {
   public static void main(String[] args) {
     try {
       long start = Runtime.getRuntime().freeMemory();
       RandomAccessFile rF = new RandomAccessFile("D:/cxz/压缩软件/S60-SDK-200634-3.1-Cpp-f.1090b.zip", "r");
       for (int i = 0; i < 100; i++) {
         if ((i % 16) == 0)
           System.out.print("/r/n");
         System.out.print(Functions.getByteHexStr(rF.readByte()) + " ");
       }
       long end = Runtime.getRuntime().freeMemory();
       System.out.println("/n/nused   menory:" + (start - end) / 1024 + "KB");
       rF.close();
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
}


3      BufferedReader // 每次读出文件的一行,写你的处理代码,
try
{
BufferedReader
bin = new BufferedReader(new InputStreamReader(new FileInputStream(File file)));
String str= null;                                                  
while((str= bin.readLine()) != null)
{
     // 每次读出文件的一行,写你的处理代码,
}
 bin.close();
}catch(IOException ioe){}


4         BufferedInputStream(InputStream in, int size)
           创建具有指定缓冲区大小的 BufferedInputStream,并保存其参数,即输入流 in,以便将来使用

------------------------------

 

原创粉丝点击