0
 public void urlWriter(int y) throws IOException
            {
           File file = new File("C:\\DRIVE\\datas.txt");
           FileWriter fw = new FileWriter(file);
           BufferedReader dummyReader = new BufferedReader(new FileReader(file));

           BufferedWriter latestSource = new BufferedWriter(fw);


          //dummy string to read line by line
          String dooms =null;


         //loop for reading line by line
            for(i=0;i<y;i++) 
            {   
            while ((dooms=dummyReader.readLine())!=null)
            {


               //y line
                latestSource.write(CommonData.entered_direct);
                latestSource.newLine();
                //y+1 line
                latestSource.write(CommonData.entered_cellar);
                latestSource.newLine();
                //y+2 line
                latestSource.write(CommonData.entered_tele);
                latestSource.close();

            }
            }

Tried to read line by line and then write at the specific locations. But the same issue exists.. Can some one help me out...????

Scenario : if y value is passed as '4', the 4th,5th,6th line in the text file should be replaced with new data.Highly appreciable if some one helps me. Thanks in advance!!

Issue faced :All the data is getting written at the bottom of the text file instead of sticking to the specified line number

Deepak Prabhu
  • 219
  • 1
  • 4
  • 15

4 Answers4

1

What you want can probably only achieved by reading the file line by line and then

  • write the new text if it is one of the lines to be replaced.
  • otherwise, write the original line.

You will then have the original file and a new file. You can rename them accordingly, this way you also have a backup of the last change.

Coarse outline:

 open input file (i.e. BufferedReader)
 open new outfile file (i.e. some Writer)
 line = 0
 data = read line from input
 while (there is data) 
    line = line + 1
    if (line >= y && line <= y+2)
        write replaced data
    else
        write data
    data = read line from input
close output file
close input file
Ingo
  • 36,037
  • 5
  • 53
  • 100
0

There's a class called RandomAccessFile, that's what your looking for.

Typo
  • 1,875
  • 1
  • 19
  • 32
0

With help of RandomAccessFileseek() you can achieve that.

Seek Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.

RandomAccessFile ra = new RandomAccessFile("abc.txt","rw" );// (file name, mode of file)
          ra.seek(15);// set the poss to overwrite
          ra.writeUTF("garbage");
AJ.
  • 4,526
  • 5
  • 29
  • 41
0

This question has been asked and there is an answer which was accepted by the user. Please see following link how to write content in a Specific position in a File

Community
  • 1
  • 1
Jabir
  • 2,776
  • 1
  • 22
  • 31