简体   繁体   English

Java NIO。 是否可以模拟基本下载应用程序的有效解决方案?

[英]Java NIO. An effective solution for simulating a basic download application or not?

I'm trying to simulate a simple download application by copying a file like myBook.txt and saving it as newBook.txt within the same Java Package. 我正在尝试通过复制文件myBook.txt并将其newBook.txt为同一Java包中的newBook.txt来模拟一个简单的下载应用程序。

I've managed to get it working on my workstation but how would I go about getting it to work on another? 我已经设法使其在我的工作站上工作,但是我将如何使其在其他工作站上工作呢? The path's that I supply will be different up until the project name eg "SimpleDownloadCentre\\\\..." 在项目名称之前,我提供的路径将有所不同,例如"SimpleDownloadCentre\\\\..."

private final String SOURCE_PATH = "C:\\Users\\...\\Documents\\NetBeansProjects\\"
            + "SimepleDownloadCentre\\src\\simpledownloadcentre\\"
            + "myBook.txt";

private final String DESTINATION_PATH = "C:\\Users\\...\\Documents\\NetBeansProjects\\"
            + "SimepleDownloadCentre\\src\\simpledownloadcentre\\"
            + "newBook.txt";

public void startDownload() {
        FileInputStream fis = null;
        try {
            String destination = DESTINATION_PATH;
            String source = SOURCE_PATH;

            fis = new FileInputStream(source);
            FileOutputStream fos = new FileOutputStream(destination);

            FileChannel fci = fis.getChannel();
            FileChannel fco = fos.getChannel();

            ByteBuffer buffer = ByteBuffer.allocate(1024);

            while (true) {
                try {
                    int read = fci.read(buffer);
                    if (read == -1) {
                        break;
                    }
                    buffer.flip();

                    fco.write(buffer);

                    buffer.clear();
                } catch (IOException ex) {
                    System.err.println("ERROR: unable to read block");
                }
            }
        } catch (FileNotFoundException ex) {
            System.err.println("ERROR: file not found");
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
                System.err.println("ERROR: unable to close file channel");
            }
        }
    }

Is Java NIO the wrong approach for the solution I'm after? Java NIO是我追求的解决方案的错误方法吗?

Thanks in advance 提前致谢

NIO is not a problem, though it won't help much more than using IO. NIO并不是问题,尽管它不会比使用IO有用得多。

What I would do is: 我要做的是:

  • Use a bigger buffer eg 32 KB. 使用更大的缓冲区,例如32 KB。
  • Use a direct buffer (its faster than using a heap one in this case) 使用直接缓冲区(在这种情况下,它比使用堆缓冲区要快)
  • Use a Socket connection to simulate a network/download connection. 使用套接字连接来模拟网络/下载连接。

You can do all this (except the direct buffer) using plain IO of course. 当然,您可以使用普通IO来完成所有这些操作(直接缓冲区除外)。

If you don't need the Sockets and just want to transfer data I would use transferTo/transferFrom to perform the copy. 如果您不需要套接字,而只想传输数据,则可以使用transferTo / transferFrom来执行复制。

In summary: Using NIO is not a bad or good idea. 总结:使用NIO并不是一个坏主意。

Your copy loop is incorrect. 您的复制循环不正确。 It can lose data if write() doesn't write the entire buffer. 如果write()没有写入整个缓冲区,则可能会丢失数据。 It should read as follows: 它的内容如下:

while ((read = in.read(buffer)) >= 0 || buffer.position() > 0)
{
  buffer.flip();
  out.write(buffer);
  buffer.compact();
}

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

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