2025年Java读取本地文件

Java读取本地文件Java 读取本地文件 1 java 读取按行本地 txt 文件 public static void readTxt String filePath try File file new File filePath if file isFile amp amp file exists

大家好,我是讯享网,很高兴认识大家。

Java读取本地文件

1、java读取按行本地txt文件

 public static void readTxt(String filePath) { 
    try { 
    File file = new File(filePath); if(file.isFile() && file.exists()) { 
    InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8"); BufferedReader br = new BufferedReader(isr); String lineTxt = null; int num =0; long time1 = System.currentTimeMillis(); while ((lineTxt = br.readLine()) != null) { 
    System.out.println(lineTxt); num++; System.out.println("总共"+num+"条数据!"); } //System.out.println("总共"+num+"条数据!"); long time2 = System.currentTimeMillis(); long time = time1 - time2; System.out.println("共花费"+time+"秒"); br.close(); } else { 
    System.out.println("文件不存在!"); } } catch (Exception e) { 
    System.out.println("文件读取错误!"); } } public static void main(String[] args) { 
    String filePath = "/Users/work/name.txt"; System.out.println(filePath); readTxt(filePath); } 

讯享网

2、java的NIO按行读取本地文件

Java NIO(New IO)是一个可以替代标准Java IO API的IO API(从Java 1.4开始),Java NIO提供了与标准IO不同的IO工作方式。
经测试,速度比流的方式耗时少一半时间;

Java NIO


讯享网

讯享网 public static void readTxtByNIO(String filePath) { 
    long time1 = System.currentTimeMillis(); FileInputStream fis = null; FileChannel inChannel = null; int bufSize = 1024*10; try { 
    fis = new FileInputStream(filePath); inChannel = fis.getChannel(); System.out.println("file size --->"+inChannel.size()); ByteBuffer buffer = ByteBuffer.allocate(bufSize); // System.out.println("buffer init position --->"+buffer.position()+"---- buffer init remaining --->"+buffer.remaining()); //这里标记了后面才可以调用buffer.reset(), 而且只能调用一次, //不然会抛出java.nio.InvalidMarkException //buffer.mark(); String enterStr = "\n"; StringBuffer strBuf = new StringBuffer(""); int lineNum = 0; while(inChannel.read(buffer) != -1){ 
    int rSize = buffer.position(); buffer.clear(); String tempString = new String(buffer.array(), 0, rSize); if(fis.available() ==0){ 
   //最后一行,加入"\n分割符" tempString+="\n"; } int fromIndex = 0; int endIndex = 0; while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) { 
    String line = tempString.substring(fromIndex, endIndex); line = new String(strBuf.toString() + line); System.out.println(line); strBuf.delete(0, strBuf.length()); fromIndex = endIndex + 1; lineNum++; } if (rSize > tempString.length()) { 
    strBuf.append(tempString.substring(fromIndex, tempString.length())); } else { 
    strBuf.append(tempString.substring(fromIndex, rSize)); } System.out.println("lineNum ="+ lineNum); } } catch (Exception e) { 
    System.out.println("文件读取错误!"); } finally { 
    if(fis != null){ 
    try { 
    fis.close(); } catch (IOException e) { 
    e.printStackTrace(); } } if(inChannel != null){ 
    try{ 
    inChannel.close(); }catch (IOException e){ 
    e.printStackTrace(); } } long time2 = System.currentTimeMillis(); long time = (time1-time2)/1000; System.out.println("共花费"+time+"秒"); } } public static void main(String[] args) { 
    String filePath = "/Users/work/name.txt"; System.out.println(filePath); readTxtByBuffer(filePath); //readTxtByNIO(filePath); } 

3、MappedByteBuffer

博客链接:https://www.cnblogs.com/lyftest/p/6564282.html

package test; import java.io.*; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.util.List; / * Created by qxr4383 on 2018/3/9. */ public class importTest2 { 
    public static void main(String[] args) { 
    String filePath = "/Users/qxr4383/Documents/work/logger/prod/id5/id5-weather/2018/weather.txt"; String filePath1 = "/Users/qxr4383/Documents/work/logger/prod/access/2018/01/1.txt"; System.out.println(filePath1); readTxt(filePath); } static int length = 0x; // 128 Mb public static void readTxt(String filePath){ 
    long timeStar = System.currentTimeMillis();// 得到当前的时间 // FileInputStream fis = null; // FileChannel inChannel = null; // int bufSize = 1024 * 14 * 1024; File file = new File(filePath); importTest2 mf = new importTest2(); try { 
    mf.structXML(file); // fis = new FileInputStream(filePath); // inChannel = fis.getChannel(); // ByteBuffer buffer = ByteBuffer.allocate(bufSize); // MappedByteBuffer mbb = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size()); // new Thread(new CompareThread(buffer)).start(); // for (int i = 0; i < length; i++) { 
    // mbb.put((byte) 'x'); // } // System.out.println("Finished writing"); // //读取文件中间6个字节内容 // for (int i = length / 2; i < length / 2 + 6; i++) { 
    // System.out.print((char) mbb.get(i)); // } } catch (Exception e) { 
    System.out.println("文件读取错误!"); e.printStackTrace(); } finally { 
    // if(fis != null){ 
    // try { 
    // fis.close(); // } catch (IOException e) { 
    // e.printStackTrace(); // } // } // if(inChannel != null){ 
    // try{ 
    // inChannel.close(); // }catch (IOException e){ 
    // e.printStackTrace(); // } // } long timeEnd = System.currentTimeMillis(); long time = (timeEnd-timeStar); System.out.println("共花费"+time+"ms"); } } private List structXML(File file) throws IOException{ 
    // for(int i=1;i<=8;i++){ 
    try { 
    if(file.length()<length){ 
    length = (int)file.length(); } MappedByteBuffer buffer = new RandomAccessFile(file, "r") .getChannel().map(FileChannel.MapMode.READ_ONLY, // file.length()*7/8,file.length()/8); 0, length); new Thread(new CompareThread(buffer)).start(); } catch (FileNotFoundException e) { 
    e.printStackTrace(); } catch (IOException e) { 
    e.printStackTrace(); } // } return null; } class CompareThread implements Runnable{ 
    private MappedByteBuffer buffer = null; final int BUFFER_SIZE = 0x1000; byte[] dst = new byte[BUFFER_SIZE]; public CompareThread(MappedByteBuffer buffer) { 
    this.buffer = buffer; // this.length = length; } public void run(){ 
    long start = System.currentTimeMillis(); String enterStr = "\n"; StringBuffer strBuf = new StringBuffer(""); long total=0; for (int offset = 0; offset < this.buffer.capacity(); offset += BUFFER_SIZE) { 
    if (this.buffer.capacity() - offset >= BUFFER_SIZE) { 
    for (int i = 0; i < BUFFER_SIZE; i++) dst[i] = this.buffer.get(offset + i); } else { 
    for (int i = 0; i < this.buffer.capacity() - offset; i++) dst[i] = this.buffer.get(offset + i); } int length = (this.buffer.capacity() % BUFFER_SIZE == 0) ? BUFFER_SIZE : this.buffer.capacity() % BUFFER_SIZE; String tempString = new String(dst, 0, length); int fromIndex = 0; int endIndex = 0; while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) { 
    total++; String line = tempString.substring(fromIndex, endIndex); line = new String(strBuf.toString() + line); //System.out.println(line); fromIndex = endIndex + 1; strBuf.delete(0, strBuf.length()); } // if (rSize > tempString.length()) { 
    // strBuf.append(tempString.substring(fromIndex,tempString.length())); // } else { 
    // strBuf.append(tempString.substring(fromIndex, rSize)); // } } System.out.println("total----->"+total+"====耗时==="+(System.currentTimeMillis()-start)); } } } 
小讯
上一篇 2025-03-15 20:47
下一篇 2025-01-07 20:49

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/100728.html