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