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]
# 1

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

}

parthasarkar at 2007-6-29 9:31:57 > top of Java-index,Archived Forums,Java Programming...
# 2

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.

javaVivian at 2007-6-29 9:31:57 > top of Java-index,Archived Forums,Java Programming...
# 3

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.

javaVivian at 2007-6-29 9:31:57 > top of Java-index,Archived Forums,Java Programming...
# 4

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.

javaVivian at 2007-6-29 9:31:57 > top of Java-index,Archived Forums,Java Programming...
# 5

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

}

parthasarkar at 2007-6-29 9:31:57 > top of Java-index,Archived Forums,Java Programming...
# 6

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

}

parthasarkar at 2007-6-29 9:31:57 > top of Java-index,Archived Forums,Java Programming...
# 7
javaVivian seems to have had the same problem as me causing these multiple posts ;-)
parthasarkar at 2007-6-29 9:31:57 > top of Java-index,Archived Forums,Java Programming...
# 8

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.

DragonMan at 2007-6-29 9:31:57 > top of Java-index,Archived Forums,Java Programming...
# 9
thank you for the help :)
xascendent at 2007-6-29 9:31:57 > top of Java-index,Archived Forums,Java Programming...