• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

RandomAccessFile

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Folks,
I got a small hack which I seem to have problems with, any help would be much appreciated.
code:
import java.io.*;

class Write {
public static void main(String[] args) throws IOException {
RandomAccessFile out = new RandomAccessFile("Ran.test", "rw");
out.writeBytes("Ninotchka");
out.close();
RandomAccessFile out1 = new RandomAccessFile("Ran.test", "rw");
while(out1.read() != -1)
{
System.out.println(out1.readLine());
}
out.close();
out1.close();
}
}
Output:
Ninotchka
inotchka
inotchka
inotchka
inotchka
inotchka
inotchkaNinotchka
All the N's are missing when System.out.println(out1.readLine()) is called.
Any suggestions so that I read the file intact, with all the the line breaks.
Thank you very much

 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am moving this thread to the Java beginner section where it can get more attention.
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In:
<PRE>
while(out1.read() != -1)
{
System.out.println(out1.readLine());
}
</PRE>
your
<CODE>out1.read()</CODE>
is eating out the first character. SO the next reads begins at "i".
The output is just one
<CODE>inotchka</CODE>
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Tony,
Sorry to be dum,
But any suggestions as to what I can do to get the N in?
Thank you

 
Sheriff
Posts: 7000
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To both test and use a value (such as the result of read()) you are going to have to put it in a variable. You can use almost any form of loop, but the key point is to read it into a variable, then test it.
Here are some examples:
The "Traditional C" way:

The "For Loop" way:

The "Do While" way:


The "Do Forever" way:

If you study all the loop constructs in Java, you can probably find even more ways of doing this. All these ways have their advantages and disadvantages, but they should all work.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Frank.
Your indepth explaination was super.
Thanks once again
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since we are on the RandomAccessFile class, there is a method that came to mind because of the original question:
seek(long pos)
Parameters: pos - the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.
It's functionality includes the equivalent of "rewind" and "fast forward".
[This message has been edited by Tony Alicea (edited February 04, 2000).]
 
Slime does not pay. Always keep your tiny ad dry.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic