• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RandomAccessFile write() problem

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

i am using RandomAccessFile write(bytes b[]) method.
When tried to write multiple lines with multiple write() for the first time it is writing well to a file.
But if i run the same program for the second time first write() method is skpping and all other write() are writing text to the file.
Can any one let me know how to resolve this issue??

Thnaks & Regards
Aresh Babu
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For my part, I'm not smart enough to be able to figure out why your code is not working correctly given the information you've posted. If a smarter person doesn't give you the answer soon, you may wish to create a very small program that is compilable, runnable, and demonstrates this very problem that you're having and then posting it here. This way we can see for ourselves just what is not working and how.

Much luck!
 
aresh babu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually My problem is
File file=new File("one.txt");
RandomAccessfile raf=new RandomAccessfile(file,rw);
raf.seek(file.lenght());
raf.write("first"+getBytes());
raf.write("second"+getBytes());


When i tried to execute this program for the first time it is inserting the 2lines into the respective file.
But if i tried to execute this program for second time only raf.write("second"+getBytes()); is inserting into the file, first line is skipping.
I want to get that first line also in the file

please help me

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, I hope someone answers you soon. Again, if not, then I still strongly recommend that you create that small compilable program. Why the use of RandomAccessFile here, by the way?
 
aresh babu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class MyFile{
public static void main(String[] args)throws Exception{
File file=new File("one.txt");
RandomAccessfile raf=new RandomAccessfile(file,rw);
raf.seek(file.lenght());
raf.write("first"+getBytes());
raf.write("second"+getBytes());
}
}
I am using RandomAccessFile to give a chance to differant threads to access this file same time.

please help me

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aresh babu wrote:


Your code doesn't compile for me, so again as for me, I'm stuck. Many here find that they can better understand a poster's problem if they post code that can compile and run unmodified on a volunteer's own box at home or work. You may wish to create such a program and post this formatted code (with code tags so that it's readable) here on the forum.

I am using RandomAccessFile to give a chance to differant threads to access this file same time.


I've usually used RandomAccessFiles when I want to read or write fixed-length records to a file. I'm not sure that your requirement is a good reason for choosing to use this type of file.

Again, much luck!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) UseCodeTags. That makes your example easier to read.
2) Either PostRealCode or post an SSCCE. Your code is neither, it contains several compiler error (RandomAccessfile with lowercase f, rw not being a String, file.length being miss-spelled). Also, you probably meant "first"getBytes(), not "first" + getBytes().

If I fix those errors to the code below I get "firstsecond" appended each time. As such, the code is working as I expect it to.
Just a note: if you want to append Strings to a text file, you should consider creating a FileWriter with the append flag set to true:
reply
    Bookmark Topic Watch Topic
  • New Topic