简体   繁体   English

如何使用FileReader不使用BufferReader

[英]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 如何使用FileReader不使用BufferReader我想使用File,FileReader进行从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. FileReader从文件系统读取文件。 It doesn't read from an FTP connection. 它不会从FTP连接读取。

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. obove代码从输入流创建文件对象tmpFile。 You can use Filereader on this file object. 您可以在此文件对象上使用Filereader。

  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. 注意File Reader逐个字符地读取字符,这就是为什么人们更喜欢BufferedReader的原因。

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. 因此,建议将BufferedReader包装在其read()操作可能会很昂贵的任何Reader周围,例如FileReaders和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. 如果不进行缓冲,则每次调用read()或readLine()都可能导致从文件中读取字节,将其转换为字符,然后返回,这可能会非常低效。

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. 您可以将所有输入写入文件,然后打开FileReader并尝试记住完成后删除该文件,但这是一个巨大的时间浪费:两次读取数据。 Simpler to adjust your API so you can supply a Reader or an InputStream. 调整API更简单,因此您可以提供Reader或InputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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