Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

just a try simplified File access

Thread view: 
raptor - 20 May 2006 20:51 GMT
hi,

I'm tring to make some simplified file access i.e. instead of making
different type of objects for reading, writing and appending to file.
To have just one type of object that will deal with the details...
something like file access in scripting lang like Perl.
This is just exercise so that i can getting deeper in Java, I may make
some stupid mistake and make some presumption, so pls point me to them
or correct me.
Below is the code up to now..I'm getting the following error :

file.java:59: unreported exception java.io.IOException; must be caught
or declared to be thrown
   System.out.println(f.slurp());
                             ^
1 error
Exception in thread "main" java.lang.NoClassDefFoundError: file

But I have some more fundamental questions too i.e.
The parent for Writer/Reader is Object.. my problem is that i dont want
to have 2 separate var's and doing this check to see which one to use ?
Direct casting /i.e. (BufferedReader)f.getLine()/ doesnt work either
that is why I made getFile(). But there I also have some very weird
problem i.e. 'cause I have to return Object I have to do "return rfile"
at the end which is unnececary 'cause I'm doing this in the
switch-statement!!! Yes I can do :

public Object getFile() {
   if( mode == 'r') {return rfile;}
   return wfile;
}

but this still doesnt look too good to me. So what i'm doing wrong.

=====the code===============
import java.io.*;

class fileSimple  {

private BufferedReader rfile;
private BufferedWriter wfile;
char mode;

public Object getFile() {
   switch (mode) {
       case 'r' : return rfile;
       case 'w' : return wfile;
   }
   return rfile;
}

public void open(char mode,String fileName) {
   try {
       switch (mode) {
           case 'r' : rfile = new BufferedReader(new
FileReader(fileName)); break;
           case 'w' : wfile = new BufferedWriter(new
FileWriter(fileName, false)); break;
       };
       this.mode = mode;
   } catch (IOException e) {
       System.out.println("Can't open : " + fileName);
   }
}

public void open(String fileName) {
   this.open('r', fileName);
}

public String slurp() throws IOException {
   String str = "", line = "";
   if (mode == 'w') { throw new IOException("U cant read from file
opened for writing"); };

   BufferedReader f = (BufferedReader)this.getFile();
   try {
       while((line = f.readLine()) != null)
           {  str += line + "\n"; }
   } catch (IOException e) {}
   return str;
}

public void close() {
   BufferedReader f = (BufferedReader)this.getFile();
   try { f.close(); } catch (IOException e) {};
}

}

class file {

public static void main(String[] str) {
   fileSimple f = new fileSimple();
   f.open('w',"hello2.java");
   System.out.println(f.slurp());
   f.close();
}

}
Chris Smith - 20 May 2006 21:01 GMT
> I'm tring to make some simplified file access

With all due respect, you might want to learn Java before you go any
further.  There are general concepts and techniques in the language that
you are violating.  Type-safety is a big one.  You are essentially
providing some that might be an InputStream, might be an OutputStream,
can only be one at a time, and doesn't fail until runtime if you treat
it as the wrong one.  This is really a rather pointless exercise.  Just
used Reader or InputStream when you want to read, and Writer or
OutputStream when you want to write.

If you need an object that you can use to BOTH read and write a file and
preserve a single file pointer as you do it, then see RandomAccessFile.  
However, RandomAccessFile by necessity works only with binary data,
since character encodings can have variable lengths.

A more minor point along the same lines: classes in Java always start
with capital letters.  If nothing else, call your classes FileSimple and
File, rather than fileSimple and file.  Otherwise, you will just be
confusing people for no reason.

> file.java:59: unreported exception java.io.IOException; must be caught
> or declared to be thrown
>     System.out.println(f.slurp());

You need to handle any checked exceptions that may arise.  If this is
just simple test code where you want the program to terminate when
there's an error, you can declare that by adding "throws Exception" to
the declaration of main.

> But I have some more fundamental questions too i.e.
> The parent for Writer/Reader is Object.. my problem is that i dont want
[quoted text clipped - 11 lines]
>
> but this still doesnt look too good to me. So what i'm doing wrong.

If you insist on breaking type safety, your code is going to be ugly.  
That's really all there is to it.  You aren't writing in a scripting
language any more.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

raptor - 21 May 2006 00:28 GMT
With all due respect, you might want to learn Java before you go any
further.  There are general concepts and techniques in the language
that
you are violating.  Type-safety is a big one.  ....

]- thanx, you are right.. but if I want to sacrifice type safety. I
know that i have to follow
strong type rules.. ;] but in many other languages (not only scripting)
i can do this w/o bothering too much ;).
The "super-descriptivness" of java bothers me more than the loose
types.
I'm not tring to argue (or offend you) that my way is better, u know
when u come from other languages u first try to explain yourself the
things in their way.. and after a while u got it right.
I'm still in the phase where I better break things up to learn the way
java expect me to behave ;)
I have been programming in other strong type languages too, but
following the java rules are much unpleasant and in some cases make me
'nuts'. ;)

thanx again for the reply...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.