0

I'm working on an assignment that asks to request a file name, then take the file and output it to a .txt file with numbered line formatting like:

[001]

[002]

I have piece milled code together to get the program to work but I can't seem to get it to write in the requested format. Any help would be appreciated.

Here is my code so far.

    try {

        System.out.println("Enter your source code file name: ");
        Scanner scanner = new Scanner(System.in);
        String file = scanner.nextLine();

        in = new FileInputStream(file);
        out = new FileOutputStream(file + ".txt");

        int c;
        while ((c = in.read()) != -1) {
            out.write(c);
        }

    } catch (IOException e) {
        System.out.println("Error!");

    }
  • You shouldn't read and write to a file at the same time, it's better to read it, do your changes in memory and then write it to the file – Draken May 17 '16 at 15:47
  • @Draken They are not the same file... – assylias May 17 '16 at 15:47
  • Good point, missed the appended .txt. Rather than using `FileInputStream`, it should be better to use `BufferedReader` and `FileReader`. That allows easy reading of lines. [Check here for an example](http://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java). Biggest problem you'll have at the moment is that you are reading a line character by character, which is slow and difficult. There are better techs to read a whole line at a time, rather than you having to do the work – Draken May 17 '16 at 15:51
  • If the input file has more than 999 (and less than 10,000) lines, should the numbering start with `[0001]`? In other words, are the leading 0's required? – Kedar Mhaswade May 17 '16 at 16:26

2 Answers2

2

You only need a counter to count your rows:

BufferedReader in = new BufferedReader(new FileReader(fileName));
BufferedWriter out = new BufferedWriter(new FileWriter(fileName + '.txt');
String line;
while ((line = in.read()) != -1) {
     counter++;
     out.write(String.format(..., counter, line)+"\r\n");
}

For additional information on the String.format() method look at this link

Draken
  • 3,134
  • 13
  • 34
  • 54
ZeusNet
  • 710
  • 9
  • 25
1

Try to use BufferedReader/BufferedWriter to read and write the lines of your text easier like this:

    String txtName = "test";
    String txtNumbered = "test1";
    try
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(txtName+".txt")));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(txtNumbered+".txt")));
        int count = 0;

        String line = br.readLine();
        while(line != null)
        {
            count++;
            bw.write(count+" "+line+"\r\n");
            line = br.readLine();
        }

        br.close();
        bw.close();

    }catch(IOException e)
    {
        System.out.println(e);
    }
theVoid
  • 743
  • 4
  • 14
  • Please be careful that you don't provide the full answer to the OP, remember it is a homework question so they do need to do some figuring out! – Draken May 17 '16 at 16:13
  • Should i delete this answere then?I am new here and i would appreciate your guidance. – theVoid May 17 '16 at 16:30
  • 1
    I think they would have already figured it out from the previous answer, although it's a little less intuitive as they've forced them to use `String.format()` (Which they should learn anyway). Leave your answer up, just take it into consideration for next time – Draken May 17 '16 at 16:33
  • Ok, thanks man i will take your words into consideration next time. – theVoid May 17 '16 at 16:35
  • 1
    Thanks @theVoid, I've been struggling with this problem for 2 days now and I did modify your code to include the String.format() to get the counter to display in the right format. –  May 17 '16 at 18:22
  • No problem man, although i hope i didn't solve all your homework,i mean homework is meant to help you improve your skills and not just for a grade. – theVoid May 17 '16 at 18:32