简体   繁体   English

需要计算文本文件中的字符

[英]Need to count characters in text file

i have to write code that reads a text file and tells me how many lines and characters are in the file. 我必须编写代码来读取文本文件,并告诉我文件中有多少行和字符。 I had it working but then i realized i had to ignore whitespace gaps so i wrote a method to do it. 我可以使用它,但是后来我意识到我必须忽略空白间隙,所以我写了一种方法来做到这一点。 It works fine for one line but if i have more than one line it seems to count any whitespace. 它对于一行来说效果很好,但是如果我多于一行,它似乎可以算出任何空格。 Any help would be appreciated 任何帮助,将不胜感激

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class Inputfile {

public static void main(String[] args) {
    System.out.println("file name:");
    Scanner sc = new Scanner(System.in);
    String fn = sc.next();

    int nrl = 0, nChar = 0;// nrl for number of lines
    String line;// get line content variable

    try {
        File fs = new File("C:/" + fn);
        nChar = length_ws(fs);
        FileReader fr;// for reading file

        fr = new FileReader(fs);
        LineNumberReader lnr = new LineNumberReader(fr);
        while (lnr.readLine() != null) {
            nrl++;// count number of lines
        }
        JOptionPane.showMessageDialog(null, "number of lines:" + nrl + "\ntotal number of chars:" + nChar);

        lnr.close();
        fr.close();// close file
    } catch (FileNotFoundException ex) {
        System.err.println("File not found");
        System.exit(0);
    } catch (IOException ex) {

    }
}

public static int length_ws(File f) throws IOException {
    FileReader fr = null;
    fr = new FileReader(f);
    int i;
    i = 0;
    int c = 0;
    do {

        c = fr.read();// read character

        if (c!= ' ') // count character except white space
            i++;
    } while (c != -1);
    return i - 1;// because the is counted even the end of file
}
}

I don't think it is reading the space but the line feed (since these are char to). 我不认为它正在读取空格,而是换行符(因为这些字符是字符)。

I suggest that you do only read the file once (now it seems that you read it twice). 我建议您只读取一次文件(现在看来您读取了两次)。

As char arrives 当char到达时

  c = fr.read()

you evalute which char it is check out the asci table ASCII TABLE , you have space,tabs and line feeds (watch out depending on format you can have two chars LF and CR for line feed) 您评估是哪个字符,请检查asci表ASCII TABLE ,您有空格,制表符和换行符(请注意,根据格式,可以有两个字符LF和CR作为换行符)

If you have valid char you advance your char counter. 如果您有有效的字符,请提前输入字符计数器。 If you have valid char for linefeed you advance your line count. 如果您有有效的换行字符,则可以增加行数。

Hope this help and improves your coding, good luck 希望对您有所帮助,并改善您的编码,祝您好运

Seeing your comment I added this code, its not perfect but a start 看到您的评论,我添加了此代码,它不是完美的,只是一个开始

int LF = 10; // Line feed
    int CR = 13; // Chr retrun
    int SPACE = 32;// Space
    int TAB = 9; // Tab

     FileReader fr = null;
    int numberOfChars = 0;
    int numberOfLines = 0;
    int c = 0;
    try {
        do {

            fr = new FileReader(new File("fileName.txt"));
            c = fr.read();// read character
            if (c > SPACE) { // space (ignoring also other chars 
                numberOfChars++;
            }
            if (c == LF) { // if does not have LF try CR
                numberOfLines++;
            }

        } while (c != -1);

    } catch (Exception e) {
        e.printStackTrace();
        if (fr != null) {
            try {
                fr.close();
            } catch (IOException e1) {
            }
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM