3

I am trying to generate random numbers as ids, and save them in a file to easily access them. I am currently using BufferedWriter in order to write these to the file, but the problem is that I am not too sure about how to go about finding where I should start writing into the file. I am currently trying to use BufferedReader to figure out where the next line is to write, but I am not sure how I am supposed to save this offset or anything, or how a new line is represented.

void createIds(){
    File writeId = new File("peopleIDs.txt");
    try {
        FileReader fr = new FileReader(writeId);
        BufferedReader in = new BufferedReader(fr);
        FileWriter fw = new FileWriter(writeId);
        BufferedWriter out = new BufferedWriter(fw);
        String line;
        while((line = in.readLine()) != null){
            //How do I save where the last line of null is?
            continue;
        }
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}
bob
  • 135
  • 1
  • 4
  • 14
  • use append() method... it appends data to the existing file... – TheLostMind Dec 19 '13 at 05:04
  • Will it append it to a new line? or how might I go about doing that @TheLostMind – bob Dec 19 '13 at 05:05
  • not sure what you are trying to do, you are writing to the same file as you are reading - is that right? – Scary Wombat Dec 19 '13 at 05:06
  • 1
    @TheLostMind `append()` behaves exactly the same as `write().` Your suggestion is pointless. – user207421 Dec 19 '13 at 05:11
  • @bob I don't understand the question. `write()` writes at the current position and then advances it. I don't see what `BufferedReader` has to do with it. If you're trying to construct a random access file, use `RandomAccessFile.` – user207421 Dec 19 '13 at 05:12
  • @EJP - I believe bob wants to write ids into a newline everytime... – TheLostMind Dec 19 '13 at 05:13
  • @TheLostMind I think we had all managed to grasp that much. The question is what's the problem with 'finding where I should start writing into the file', and what does the `BufferedReader` have to do with it. – user207421 Dec 19 '13 at 05:25
  • 1
    @EJP - He is reading all the lines till he gets a line which is not written to.. then he is trying to write his id in that line... Basically he is trying to append/write his data at the end... I think this is the case.. – TheLostMind Dec 19 '13 at 05:27
  • @TheLostMind It's up to him to clarify that. I don't consider that it's up to us to guess at each other. – user207421 Dec 19 '13 at 06:19

3 Answers3

4

If you simply want to add IDs to the end of the file, use the following FileWriter constructor:

FileWriter fw = new FileWriter(writeId, true);

This opens the FileWriter in append mode, allowing you to write output to a pre-existing file.

If you would like to write the IDs to a particular location within an existing file rather than just to the end, I am not sure if this is possible without first parsing the file's contents.

For more information, see the JavaDoc for FileWriter.

x4nd3r
  • 855
  • 1
  • 7
  • 20
0

We need more information about the file itself: what are you searching for with BufferedReader?

If the file is empty/newly created then you don't need BufferedReader at all. Just create the PrintWriter and save your numbers.

I'm just guessing here, but I think the real problem is that you're not sure how to generate random numbers (since this doesn't appear in your example code).

Here's some example code that'll write random numbers into a text file:

import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class Example
{
    public static void main(String[] args)
    {
        Random r;
        PrintWriter writer;

        r = new Random();

        try
        {
            writer = new PrintWriter(new BufferedWriter(new FileWriter("Examplefile.txt")));
            for (int i = 0; i < 10; i++)
                writer.println(Integer.toString(r.nextInt(10)));

            writer.close();
        }
        catch (IOException e)
        {

        }
    }
}
Kap
  • 3
  • 4
-1

You can do

    try {
        PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(new File("abc.txt"),true)));
        writer.append("test");
    } catch (IOException e) {
        e.printStackTrace();
    }
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289