如何使用java代码读取文件的所有行? [英] How to read all the lines of a file using java code?

查看:250
本文介绍了如何使用java代码读取文件的所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,我有一个名为transactionHandler.log的日志文件。它是一个非常大的文件,有17102行。这是我在linux机器上执行以下操作时获得的:

  wc -l transactionHandler.log 
17102 transactionHandler.log

但是,当我运行下面的java代码,并打印我得到的2040作为o / p的行数。

  import java.io. *; 
import java.util.Scanner;
import java.util.Vector;

public class Reader {

public static void main(String [] args)throws IOException {
int counter = 0;
String line = null;

//要读取的文件的位置
文件file = new File(transactionHandler.log);

尝试{

扫描仪扫描仪=新扫描仪(文件); $()

while(scanner.hasNextLine()){
line = scanner.nextLine();
System.out.println(line);
counter ++;
}
scanner.close();
} catch(FileNotFoundException e){
e.printStackTrace();
}
System.out.println(counter);




$ b $ p
$ b $ p $你可以告诉我原因。

解决方案

据我所知, Scanner 使用 \ n 作为分隔符。也许你的文件有 \r\\\
。你可以通过调用 scanner.useDelimiter 或(并且好得多)来修改这个方法:

  import java.io. *; 
$ b $ public class IOUtilities
{
public static int getLineCount(String filename)throws FileNotFoundException,IOException
{
LineNumberReader lnr = new LineNumberReader(new FileReader文件名)); ((lnr.readLine())!= null){}

返回lnr.getLineNumber();


根据 LineNumberReader


一行被认为是以换行符
('\ n'),一个回车符('\r')或一个回车符
$立即换行。


所以适用于具有不同行结束符的文件。



试一下,看看它有什么用。


I have a strange problem where I have a log file called transactionHandler.log.It is a very big file having 17102 lines.This i obtain when i do the following in the linux machine:

wc -l transactionHandler.log
17102 transactionHandler.log

But when i run the following java code and print the number of lines i get 2040 as the o/p.

import java.io.*;
import java.util.Scanner;
import java.util.Vector;

public class Reader {

    public static void main(String[] args) throws IOException {     
        int counter = 0; 
        String line = null;

         // Location of file to read
        File file = new File("transactionHandler.log");

        try {

            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) {
                line = scanner.nextLine();
                System.out.println(line);
                counter++;                    
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }           
        System.out.println(counter);        
    }
}

Can you please tell me the reason.

解决方案

From what I know, Scanner uses \n as delimiter by default. Maybe your file has \r\n. You could modify this by calling scanner.useDelimiter or (and this is much better) try using this as an alternative:

import java.io.*;

public class IOUtilities
{
    public static int getLineCount (String filename) throws FileNotFoundException, IOException
    {
        LineNumberReader lnr = new LineNumberReader (new FileReader (filename));
        while ((lnr.readLine ()) != null) {}

        return lnr.getLineNumber ();
    }
}

According to the documentation of LineNumberReader:

A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

so it's very adaptable for files that have different line terminating characters.

Give it a try, see what it does.

这篇关于如何使用java代码读取文件的所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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