file I/O troubles.
I have a function called getFileContents that opens a file and returns a string, but I'm getting this error and wondering if someone can help me out. (i'm using forte as my IDE, and a jButton is the trigger event for the getFileContents.) When I run the program I get this error
Open.java [57:1] Exception java.io.IOException thrown from getFileContents must be caught, or it must be declared in the throws clause of this method
rStr = getFileContents(a);
^
1 error
below is the function, and the call.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String a;
String rStr;
a = "c:\\windows\\desktop\\Java.txt";
rStr = getFileContents(a);
}
public String getFileContents(String name)throws IOException {
BufferedReader br = new BufferedReader(
new FileReader( name ) );
StringBuffer contents = new StringBuffer();
String line = "";
while( ( line = br.readLine() ) != null )
{
contents.append( line );
}
br.close();
return contents.toString();
}
can someone tell me what is wrong?
[1186 byte] By [
xascendent] at [2007-9-26 2:23:37]

because this method throws an exception you must have provision for catching or propagating this exception (if it occurs at runtime).
public String getFileContents(String name)throws IOException {
do this:
[code]try {
rStr = getFileContents(a);
}
catch( IOException ioe ) {
System.out.println("Exception occured: " + ioe);
}
Very simple, you have 2 choices:
either put a try and catch block to in the method as such
private void jButton1ActionPerformed java.awt.event.ActionEvent evt) {
String a;
String rStr;
a = "c:\\windows\\desktop\\Java.txt";
try {
rStr = getFileContents(a);
} catch (IOException ioe) {
ioe.printStackTrace();
System.out.println("IOException: " + ioe.getMessage());
}
}
or throw IOException by indicate it on the method line:
private void jButton1ActionPerformed java.awt.event.ActionEvent evt) throws IOException {
hope it helps.
Very simple, you have 2 choices:
either put a try and catch block to in the method as such
private void jButton1ActionPerformed java.awt.event.ActionEvent evt) {
String a;
String rStr;
a = "c:\\windows\\desktop\\Java.txt";
try {
rStr = getFileContents(a);
} catch (IOException ioe) {
ioe.printStackTrace();
System.out.println("IOException: " + ioe.getMessage());
}
}
or throw IOException by indicate it on the method line:
private void jButton1ActionPerformed java.awt.event.ActionEvent evt) throws IOException {
hope it helps.
Very simple, you have 2 choices:
either put a try and catch block to in the method as such
private void jButton1ActionPerformed java.awt.event.ActionEvent evt) {
String a;
String rStr;
a = "c:\\windows\\desktop\\Java.txt";
try {
rStr = getFileContents(a);
} catch (IOException ioe) {
ioe.printStackTrace();
System.out.println("IOException: " + ioe.getMessage());
}
}
or throw IOException by indicate it on the method line:
private void jButton1ActionPerformed java.awt.event.ActionEvent evt) throws IOException {
hope it helps.
because this method throws an exception you must have provision for catching or propagating this exception (if it occurs at runtime).
public String getFileContents(String name)throws IOException {
do this:
[code]try {
rStr = getFileContents(a);
}
catch( IOException ioe ) {
System.out.println("Exception occured: " + ioe);
}
because this method throws an exception you must have provision for catching or propagating this exception (if it occurs at runtime).
public String getFileContents(String name)throws IOException {
do this:
try {
rStr = getFileContents(a);
}
catch( IOException ioe ) {
System.out.println("Exception occured: " + ioe);
}
javaVivian seems to have had the same problem as me causing these multiple posts ;-)
The declaration of your getFileContents() method states that it can throw IOExceptions. When you invoke that method in your jButton1ActionPerformed() method, you must do one of two things:
place it within a try/catch block to handle the possible IOException: try {
rStr = getFileContents(a);
} catch (IOException e) {
System.err.println("Exception trying to get file contents:"+e.getMessage()) ;
}
or add a "throws IOException" to the declaration of your "jButton1ActionPerformed()" method, and let whatever is calling that method deal with the IOException.
thank you for the help :)