Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java.io.* and methods 1

Status
Not open for further replies.

stormbind

Technical User
Mar 6, 2003
1,165
0
0
GB
This example comes from Sun's website, and I have absolutely no problems with it.

Code:
import java.io.*;

public class Copy {
    public static void main(String[] args) throws IOException {
	File inputFile = new File("farrago.txt");
	File outputFile = new File("outagain.txt");

        FileReader in = new FileReader(inputFile);
        FileWriter out = new FileWriter(outputFile);
        int c;

        while ((c = in.read()) != -1)
           out.write(c);

        in.close();
        out.close();
    }
}

However, I do not comprehend why the compiler complains when the same source is used in a custom method and called from the main method, as follows:

Code:
public class Copy {
    public static void main(String[] args){
    doCopy();
    }

    public static void doCopy() throws IOException {
	File inputFile = new File("farrago.txt");
	File outputFile = new File("outagain.txt");

        FileReader in = new FileReader(inputFile);
        FileWriter out = new FileWriter(outputFile);
        int c;

        while ((c = in.read()) != -1)
           out.write(c);

        in.close();
        out.close();
    }
}

I would appreciate it if someone could share the solution and point me in the direction of an explanation [sadeyes]

Thanks,

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Hi?stormbind. I have amended you code as the following: I think the java claims that you don't catch the exception.
I have tested the following code. It works.
Hope it can help you!
Code:
public class Copy {
    public static void main(String[] args){
    doCopy();
    }

    public static void doCopy() {
    File inputFile = new File("farrago.txt");
    File outputFile = new File("outagain.txt");
    	try{
    		FileReader in = new FileReader(inputFile);
    		FileWriter out = new FileWriter(outputFile);
    		int c;

    		while ((c = in.read()) != -1)
    			out.write(c);

    		in.close();
        	out.close();
    	}catch(FileNotFoundException e){
    	}catch(IOException ex){}
    }
}
 
... and did you omit the 'import java.io.*;'?

@wangdong: You should never, never, never silently catch Exceptions!

Code:
catch(FileNotFoundException e)
{       // at least do:
        System.err.println (e.getMessage ());
}
catch(IOException ex)
{       // or:
        ex.printStackTrace ();
}

@stormbind: "do not comprehend why the compiler complains"
how does it complain?
Those generic informations tell you a lot, so show it to us!

seeking a job as java-programmer in Berlin:
 
Also, your doCopy() method throws an IOException - so any code calling that method must either catch the exception, or rethrow it.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thanks wangdong for making it work.

No, stefan, I did not forget to import java.io.* - just forgot to include it in the post ;)

sedj, I think you spotted the original error. I'll mess with it and return later. Thanks all,

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
It worked :)

So then I tried using it with strings, and it broke again [sadeyes]

Code:
import java.io.*;

.
.
.

public static void file() throws IOException {
	File inputFile = new File("farrago.txt");
	File outputFile = new File("outagain.txt");

        FileReader in = new FileReader(inputFile);
        FileWriter out = new FileWriter(outputFile);
        String data;

        while (((data = in.readLine()) != null)) {
           out.write(data);
	   }

        in.close();
        out.close();
}

cannot find symbol
symbol : method readLine()
location: class java.io.FileReader
while (((data = in.readLine()) != null)) {
^

I am aware of the existence of BufferedReader but it seems messy :(

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
If it were me, I'd write it like this :

Code:
public static void file() throws IOException {
	File inputFile = new File("farrago.txt");
	File outputFile = new File("outagain.txt");

	BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));
	FileOutputStream out = new FileOutputStream(outputFile);
	String data = "";

	while ((data = br.readLine()) != null) {
		out.write(data.getBytes());
		out.flush();
	}

	br.close();
	out.close();
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Well, if you want to use the readLine method, then you're gonna have to use a BufferedReader.
Code:
BufferedReader in = new BufferedReader(new FileReader(inputFile));

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
I did actually get it working, but I am grateful for the replies :)

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
sedj, it might be worth telling us why you'd write it like that. I suppose your way copes with files that are not in the default encoding.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
File IO is a native activity, and when working with objects that deal with native jobs, then I prefer to work with the closest, most unwrapped object that is feasible to use.

It also means that you could use that code to read from any stream - not just a FileInputStream, so if you overloaded the method so that it accepted an InputStream you'd get some useful reusable code :)

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top