简体   繁体   中英

How to use FileReader not to use BufferReader

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 can't. A FileReader reads a file from the file system. It doesn't read from an FTP connection.

You have to convert the input stream into a file and then use File Reader.

        URL url;
        try {
            url = new URL("ftp://" + USER_NAME + ":" + PASSWORD + "@" + SERVER
                    + "/study/" + FILE_NAME + ";type=i");
            URLConnection con = url.openConnection();
            File tmpFile = new File("tmpFile.java");
            OutputStream out = new FileOutputStream(f);

            InputStream inputStream = con.getInputStream();

            byte buf[] = new byte[1024];
            int len;
            while ((len = inputStream.read(buf)) > 0)
                out.write(buf, 0, len);
            out.close();
            inputStream.close();

        } catch (IOException e) {
        }

The obove code creats a file object tmpFile from the input stream. You can use Filereader on this file object.

  FileReader fileReader=new FileReader(tmpFile);
    int ch= fileReader.read();
    while(ch != -1){
    System.out.print((char)ch);
    ch = fileReader.read();
    }
    fileReader.close();

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,

BufferedReader in
   = new BufferedReader(new FileReader("foo.in"));

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.

Why? The input isn't a file. You could write all the input to a file and then open a FileReader and try to remember to delete the file when finished, but what a colossal waste of time: reading the data twice. Simpler to adjust your API so you can supply a Reader or an InputStream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM