简体   繁体   English

如何将一行打印到文本文件中

[英]How to print one single line into a text file

I want to print out a single line to count how many line have been written into the text file, for example: 我想打印出一行以计算已将多少行写入文本文件,例如:

202.32.92.47 | 271 | 200
ix-or7-27.ix.netcom.com | 205908 | 200
ram0.huji.ac.il | 271 | 200
eagle40.sasknet.sk.ca | 1116 | 200
eagle40.sasknet.sk.ca | 49649 | 200
cdc8g5.cdc.polimi.it | 461 | 200
Total number of line: 6

Here's part of my code: 这是我的代码的一部分:

            Pattern string1 = Pattern.compile("usask.ca");
            Pattern string2 = Pattern.compile("128.233.");
            Pattern string3 = Pattern.compile("200");
            Matcher matcher1 = string1.matcher(sourceAddress);
            Matcher matcher2 = string2.matcher(sourceAddress);
            Matcher matcher3 = string3.matcher(responseCode);
            for (int i = 0; i < sourceAddress.length(); i++)
                if ((!matcher1.find() || !matcher2.find()) && matcher3.find()) {
                bw.write(sourceAddress + " | " + replySizeInBytes + " | " + responseCode);
                bw.newLine();
                bw.write("Total number of line: " + i);
                bw.newLine();
            }

but the above code is not actually doing the count, it just stay at 1 and keep printing out after each address. 但是上面的代码实际上并没有进行计数,它只是保持为1并在每个地址之后继续打印输出。 I am not sure what I've done wrong... 我不确定我做错了什么...

You can try using Scanner and calling nextLine() , along with File . 您可以尝试使用Scanner并与File一起调用nextLine() Here's the link. 这是链接。 This was really quick something like this would print out the number of lines: 这真的很快,类似这样的打印行数:

public void printLineCount() throws FileNotFoundException {
    File f = new File("filename.txt");
    Scanner s = new Scanner(f);
    int i = 0;
    while(s.hasNextLine()) {
        i++;
        s.nextLine();
    }
    System.out.println("Total number of line: " + i);
}

If you need to keep your code, let me know and I'll update my answer. 如果您需要保留自己的代码,请告诉我,我将更新答案。

Here is a example: 这是一个例子:

File file = new File("C:");
    try{
        PrintWriter output = new PrintWriter(file);
        output.println("ping www.google.cm");
        output.close();
    }catch(IOException ex){
        frame.setVisible(true);
        frame.setAutoRequestFocus(true);
    }

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

相关问题 如何在文本文件的单独行中打印arrayline - How to print a arrayline in seperate line in a TEXT FILE 如何从文本文件复制行并打印行和行号 - How to copy a line from a text file and print the line and line number 如何在一行上多次打印单个字符? - How do I print a single character multiple times on one line? 让 Java 能够从文本文件中读取一行并打印出来 - Having Java be able to read one line from a text file and print it Java:如何在单个文本文件中打印升序列,然后在该列的旁边打印同一组整数,除了按降序排列 - Java : How do I print an ascending column and next to that column the same set of integers except in descending order all in one single text file 如何在java中的输出文本文件中的每一行中打印stringbuilder的内容 - How to print the contents of stringbuilder in each line in a output text file in java 如何在Java中读取文本文件的第一行并打印出来? - How to read the first line of a text file in java and print it out? 如何从文本文件Java中读取单个单词(或行)? - How to read a single word (or line) from a text file Java? 如何将文本限制为Java源文件中的一行? - How to limit a text to a single line from java source file? 如何覆盖文本文件中的一行? - How can i overwrite a single line in a text file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM