Basically the java.io.File class represents a file in the file system, but doesn't contains methods to manipulate them ( other than create them or delete them )
When you have to work with them, you use other classes from the java.io package, BufferefReader and InputStreamReader are among them, but thare are others like FileInputStream.
Depending on the operation you want to perform you may use a reader or a stream ( classes ending with "Reader" or "Writer" are for text content, classes ending with "Stream" are for binary content, but of course, you can always read/write text as binary ).
Most of the times you have to "chain" a couple of these classes to perform a work. It is relevant also to notice, that most of the times, you can have very similar code to write to a sockets.
A naive example could be this:
package demo;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
public class ReadLines {
public static void main( String [] args ) throws IOException {
File toRead = new File( args[0]);
if( toRead.exists() ) {
List<String> lines = readLines( toRead );
} else {
System.out.println( toRead.getName() + " doesn't exists" );
}
}
private static List<String> readLines( File fromFile ) throws IOException {
BufferedReader reader = null;
List<String> lines = new ArrayList<String>();
try {
reader = new BufferedReader( new FileReader( fromFile ) ); // chaining
String line = null;
while( ( line = reader.readLine() ) != null ) {
lines.add( line );
}
} finally {
// always close the file
if( reader != null ) try {
reader.close();
} catch( IOException ioe ) {
// it's ok to ignore an exception while closing a file
}
}
return lines;
}
}
I hope this code, make it clearer for you and compiles ( :P I don't have a compiler at hand )
See also:
http://download.oracle.com/javase/tutorial/essential/io/
http://download.oracle.com/javase/6/docs/api/java/io/package-summary.html
http://download.oracle.com/javase/6/docs/api/java/io/Reader.html
http://download.oracle.com/javase/6/docs/api/java/io/Writer.html
http://download.oracle.com/javase/6/docs/api/java/io/InputStream.html
http://download.oracle.com/javase/6/docs/api/java/io/OutputStream.html