just a try simplified File access

R

raptor

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();
}

}
 
C

Chris Smith

raptor said:
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
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.

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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

raptor

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top