简体   繁体   English

在输入文本文件上写行号

[英]Writing line numbers on an input text File

I need to write a program that will produce a new file where each line of text has a line number, given an input file with no line numbers. 我需要编写一个程序,该程序将产生一个新文件,其中给定一个没有行号的输入文件,文本的每一行都有一个行号。 For example: 例如:

Given line: "sixteen" 给定行:“十六”

Output line: " 1 | sixteen", 输出行:“ 1 |十六”,

Line numbers are formatted right-aligned in 3 spaces, followed by a | 行号的格式为3个空格,右对齐。 character (with one space before 字符(前面有一个空格
and after), and followed by the text in that line. 和之后),然后是该行中的文字。

public class LineNumbers {
public static void process(File input, File output) {
    Scanner scanner;
    PrintWriter writer;
    try {
        scanner = new Scanner(input);
    } catch (FileNotFoundException e) {
        return;
    }
    try {
        writer = new PrintWriter(output);
    } catch (FileNotFoundException e) {
        return;
    }

    for (int i = 1; scanner.hasNextLine(); i++) {
        String s1 = scanner.nextLine();
        String s2 = "   " + i + " | " + s1;
        writer.print(s2);
    }
    scanner.close();
    writer.close();
}
}

This is the code I have, where the string s2 == the output I need, but how do I transition this into an output file? 这是我的代码,其中字符串s2 ==我需要的输出,但是如何将其转换为输出文件?

*****EDIT***** *****编辑*****

This is one of the tests that needs to be run, where only one line is input in the file. 这是需要运行的测试之一,其中在文件中仅输入一行。 However, in the tester, it only has two spaces in front of the line number, and when I change my code to match, the test passes. 但是,在测试器中,它在行号前面只有两个空格,并且当我更改代码以使其匹配时,测试通过。 All the other testers are written similarly and I think that may be causing the issue 其他所有测试人员的写作方式也相似,我认为这可能是导致问题的原因

    @Test
public void testOneLine() {
    try {
        // create file
        File        input  = folder.newFile( "input.txt" );
        File        output = folder.newFile( "output.txt" );

        PrintWriter write  = new PrintWriter( input );
        write.println( "Lorem ipsum dolor sit amet, consectetur adipiscing elit." );
        write.close();

        // invoke program
        LineNumbers.process( input, output );

        // verify file results
        assertTrue  ( "Output file does not exist", output.exists() );
        Scanner scan     = new Scanner( output );
        String[] result = new String[] { 
                "  1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit."
        };
        for (String expected : result) {
            if (scan.hasNext()) {
                String actual = scan.nextLine();
                assertEquals( "Incorrect result", expected, actual );
            }
            else {
                fail( String.format( "Unexpected end of file: expected \"%s\"", expected ));
                break;
            }
        }
        assertFalse ( "File contains more data than expected", scan.hasNext() );
        scan.close();
    } 
    catch (IOException e) {
        fail( "No exception should be thrown" );
    }
}

You have too many spaces preceding your line number. 行号前的空格过多。 When you are told to provide the line number in three spaces, it should be formatted as in one of these examples: 当要求您三个空格中提供行号 ,应按照以下示例之一进行格式设置:

__1 __1

_22 _22

333 333

So that there are three characters total, including any spaces. 这样一共有三个字符,包括任何空格。

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

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