• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RandomAccessFile

 
amrit singh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
constructing like
RandomaccessFile raf=new RandomAccessFile("xxx","rw");
what will happen if the file xxx is read-only and we try to write.
if the file xxx exist it is opened in append mode true or false
Also if we wrap a string fxx
as
File f=new File("fxx");
it will compile but when run it will throw exception if fxx
doesnot exist true or false
if true where the file fxx is looked for in current directory or
else
pls clarify my confusion.or i am missing something.
 
Munish Dabra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--------------------------------------------------------------------------------
constructing like
RandomaccessFile raf=new RandomAccessFile("xxx","rw");
what will happen if the file xxx is read-only and we try to write.
if the file xxx exist it is opened in append mode true or false
Also if we wrap a string fxx
as
File f=new File("fxx");
it will compile but when run it will throw exception if fxx
doesnot exist true or false
if true where the file fxx is looked for in current directory or
else
pls clarify my confusion.or i am missing something.
-----------------------------------------------------------------------
Ans. File f=new File("fxx");
Yeah if there is n't any such file exists then at run time iit will throw an IOException.
ya amrit ofcourse fxx file is looked in current directory by default.
if u wanna this it to look in some particular directory.then u have to use a different
constructor of File-----
File f=new File(String path,String filename)
or
File f=new File(File dir , String filename)
here plz note that File object can be a directory or file.ucan check it by
calling boolean method of File class
boolean isFile() or isDirectory().
i hope it will serve the purpose.
------
Munish Dabra
------
 
nan sh
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
answer your first question;
RandomaccessFile raf=new RandomAccessFile("xxx","rw");
and if the file xxx is read-only and we try to write,
then FileNotFoundException is thrown.
also for random access file, all read or write to current
position, u can use seek() to search, so there is no such
thing "append mode."
 
Zheng Huang
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To your 2nd q. No exception will be thrown. run code below.
import java.io.*;
import java.awt.*;
public class TestApp {
public static void main(String argv[]) {
File f;
f=new File("fxx");
System.out.println(f.exists() + " " + f.isFile());
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic