Java:将文本文件输出到控制台 [英] Java: Outputting text file to Console

查看:1036
本文介绍了Java:将文本文件输出到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Java输出一个文本文件到控制台。我想知道什么是最有效的方式这样做?



我研究了几种方法,但很难辨别哪个是性能影响最小的解决方案。



将文本文件输出到控制台将涉及读取文件中的每一行,然后将其写入控制台。



最好使用:


  1. 缓冲阅读器与FileReader,读取行,做一堆系统.out.println调用?

      BufferedReader in = new BufferedReader(new FileReader(C:\\logs\\)); 
    while(in.readLine()!= null){
    System.out.println(blah blah blah);
    }
    in.close();扫描器读取文件中的每一行并执行system.print调用?



      while(scanner.hasNextLine()){
    System.out.println(blah blah blah);
    }


p>

解决方案

如果你对文本文件包含的基于字符的数据不感兴趣,只需将它作为字节流raw。 / p>

  InputStream input = new BufferedInputStream(new FileInputStream(C:/logs.txt)); 
byte [] buffer = new byte [8192];

try {
for(int length = 0;(length = input.read(buffer))!= -1;){
System.out.write 0,length);
}
} finally {
input.close();
}

这节省了字节和字符之间不必要的按摩成本,



至于效果,您可能会发现这篇文章有趣。根据文章, FileChannel 与256K字节数组,通过包装的 ByteBuffer 并直接从字节数组写入是最快的方式。

  FileInputStream input = new FileInputStream(C:/logs.txt); 
FileChannel channel = input.getChannel();
byte [] buffer = new byte [256 * 1024];
ByteBuffer bytesBuffer = ByteBuffer.wrap(buffer);

try {
for(int length = 0;(length = channel.read(byteBuffer))!= -1;){
System.out.write 0,length);
byteBuffer.clear();
}
} finally {
input.close();
}


I'm attempting to output a text file to the console with Java. I was wondering what is the most efficient way of doing so?

I've researched several methods however, it's difficult to discern which is the least performance impacted solution.

Outputting a text file to the console would involve reading in each line in the file, then writing it to the console.

Is it better to use:

  1. Buffered Reader with a FileReader, reading in lines and doing a bunch of system.out.println calls?

    BufferedReader in = new BufferedReader(new FileReader("C:\\logs\\")); 
    while (in.readLine() != null) {
          System.out.println(blah blah blah);          
    }         
    in.close();
    

  2. Scanner reading each line in the file and doing system.print calls?

    while (scanner.hasNextLine()) { 
        System.out.println(blah blah blah);   
    }
    

Thanks.

解决方案

If you're not interested in the character based data the text file is containing, just stream it "raw" as bytes.

InputStream input = new BufferedInputStream(new FileInputStream("C:/logs.txt"));
byte[] buffer = new byte[8192];

try {
    for (int length = 0; (length = input.read(buffer)) != -1;) {
        System.out.write(buffer, 0, length);
    }
} finally {
    input.close();
}

This saves the cost of unnecessarily massaging between bytes and characters and also scanning and splitting on newlines and appending them once again.

As to the performance, you may find this article interesting. According the article, a FileChannel with a 256K byte array which is read through a wrapped ByteBuffer and written directly from the byte array is the fastest way.

FileInputStream input = new FileInputStream("C:/logs.txt");
FileChannel channel = input.getChannel();
byte[] buffer = new byte[256 * 1024];
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);

try {
    for (int length = 0; (length = channel.read(byteBuffer)) != -1;) {
        System.out.write(buffer, 0, length);
        byteBuffer.clear();
    }
} finally {
    input.close();
}

这篇关于Java:将文本文件输出到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆