-1

I am writing an application that writes to a text file. In this text file, at the beginning of each line, is the line number

How do I get the current line number so that I can write it in my file?

I've thought of a simple counter, but when I terminate and restart my project the counter would reset back to 1.

I've tried LineNumberReader, but that keeps on giving me 0.

Is there any way to get the current line number when writing to a file?

Thanks

try {
    FileWriter writer = new FileWriter("src\\book_store\\transaction.txt", true);
    read = new FileReader("src\\book_store\\transaction.txt");
    line = new LineNumberReader(read);

    for(int i = 0; i < output.size(); i++) {
        line.readLine();
        temp = line.getLineNumber() + " " + timeStamp2 + " " + output.get(i) + " " + timeStamp + "\n";
        output.set(i, temp);
        writer.write(output.get(i));
    }

    writer.close();
    read.close();
    line.close();
} catch (IOException ex) {
    System.out.println("File not found.");
}
shaqed
  • 342
  • 3
  • 17
KMS
  • 11
  • 3
  • 1
    please post some code – Kick Buttowski Jan 27 '19 at 22:43
  • Alright I posted – KMS Jan 27 '19 at 22:49
  • 1
    Files should be hardly described in "lines". Files are just data. – Antoniossss Jan 27 '19 at 22:57
  • 1
    I m not quite sure what you are trying to do here. You can do a simple program that parses your text file for \n or System.lineSeperator() and increase a counter whenever you found one. If you can write an example for your input and desired output, I can help you with that. – Paul Erlenmeyer Jan 27 '19 at 22:57
  • You could just save the one value (current line number) in another file. – 0xCursor Jan 27 '19 at 22:57
  • @Antoniossss Sorry, not sure what you mean by hardly described? – KMS Jan 27 '19 at 22:59
  • @PaulErlenmeyer The file I'm writing to is sort of like a log file. Whenever the program is run and executed, items are appended to the end of the log. For each item I wanted to have a number. This number would be the same as the line number – KMS Jan 27 '19 at 23:01
  • @LAD Is there any other way to do it? I was hoping there was some obscure method out there that could get whichever line my writer was currently on – KMS Jan 27 '19 at 23:01
  • I still dont understand your description and I have no idea what your file looks like and what it should look like after execution. In the code you posted it is not clear what the variables output and temp are. If I were you I would post something like this: A line in my input file looks like this: "Dogs eat food. \n Cats eat food. \n". I want the file to look like this: "1) Dogs eat food. \n 2) Cats eat food. \n 3)" or something like that ... – Paul Erlenmeyer Jan 27 '19 at 23:12
  • @PaulErlenmeyer Ah ok - Currently the output is formatted like "Date, Item \n" and I'm trying to get the output formatted to "1. Date, Item \n" "2. Date, Item \n" and so on – KMS Jan 27 '19 at 23:14
  • So why dont you just write 'i + 1 + ". " + timeStamp2 + " " + output.get(i) + " " + timeStamp + "\n";' – Paul Erlenmeyer Jan 27 '19 at 23:21
  • @PaulErlenmeyer I did that at first, but when I close and restart the program to continue appending to my file, i would reset back to 0 :( – KMS Jan 27 '19 at 23:22
  • Why? You don't need to number the lines in the file. They are already implicitly auto-numbered, and you can discover it while numbers when reading via a `LineNumberReader`. – user207421 Jan 27 '19 at 23:24
  • @user207421 Numbering mainly for formatting reasons – KMS Jan 27 '19 at 23:25
  • @KMS Do you mean that the changes are not saved or that you get lines like 1. 1. 1. timeStamp2 output.get(i) timeStamp + "\n";' – Paul Erlenmeyer Jan 27 '19 at 23:29
  • @PaulErlenmeyer The changes are saved, but the result looks sort of like this: 1. Date, Item 2. Date, Item 3. Date, Item 1. Date, Item (when it should be 4) 2. Date, Item (when it should be 5) etc. (Why don't line breaks on this site actually work? smh) – KMS Jan 27 '19 at 23:32
  • 1
    You'll have to either read the file and count the lines or store the counter externally. – shmosel Jan 27 '19 at 23:50
  • Have you already looked [at this](https://stackoverflow.com/questions/453018/number-of-lines-in-a-file-in-java)? – Siavas Jan 28 '19 at 00:34

1 Answers1

1

The mistake you are making is that while you are writing to a file through FileWriter, you are trying to read the same file using FileReader.

This could potentially work if you fsynced the file after each line. However, this is not what you really want to do. As you see from the comments, people get confused as you are complicating the problem.

You already have the counter. It’s the i loop variable. You might add +1, if you want to start from 1.

Mateusz Stefek
  • 3,478
  • 2
  • 23
  • 28