How to use FileReader not to use BufferReader i want to use File,FileReader for this program of file reading from ftp
public class FileReader {
public final static String SERVER = "ftp://server.com";
public final static String USER_NAME = "user";
public final static String PASSWORD = "password";
public final static String FILE_NAME = "Sorting Cloumns Dynamically - Java Scripts.txt";
public static void main(String[] args) {
System.out.println("Connecting to FTP server...");
// Connection String
URL url;
try {
url = new URL("ftp://" + USER_NAME + ":" + PASSWORD + "@" + SERVER+ "/study/" + FILE_NAME +";type=i");
URLConnection con = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
System.out.println("Reading file start.");
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
catch (FileNotFoundException e) {
System.out.println("File not find on server.");
System.exit(0);
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("Read File Complete.");
}
}
this code for i have created
You have to convert the input stream into a file and then use File Reader.
The obove code creats a file object tmpFile from the input stream. You can use Filereader on this file object.
Notice that File Reader reads character by character.Thats why people prefer BufferedReader over it.
In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.