Java 更新程序问题

标签 java file sockets object updates

我有一个程序,可以使用服务器通过套接字发送的信息来更新计算机上的文件。我的工作方式是这样的,但我想让它更直观、更简单、更可靠等。这是之前的代码:

int filesize = 6022386; // filesize temporary hardcoded

    int bytesRead;
    int current = 0;

    /**
     *  receive file
     */
    try {
        byte[] byteArray = new byte[filesize];
        java.io.InputStream inStream = socket.getInputStream();
        bytesRead = inStream.read(byteArray, 0, byteArray.length);
        FileOutputStream fileOutStream = new FileOutputStream(
                "C:\\Program Files\\AVTECH\\NPS\\Files\\bin\\NPS Game.txt");
        BufferedOutputStream buffOutStream = new BufferedOutputStream(
                fileOutStream);
        current = bytesRead;

        do {
            bytesRead = inStream.read(byteArray, current,
                    (byteArray.length - current));
            if (bytesRead >= 0)
                current += bytesRead;
        } while (bytesRead > -1);

        buffOutStream.write(byteArray, 0, current);
        buffOutStream.flush();
        buffOutStream.close();
        inStream.close();
        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
        socket.close();
    }

如您所见,在 do, while 循环中,它使用输入流来获取数据。现在我已经更新了我的程序,我让流发送一个名为 UpdateObject 的对象,该对象保存 byte[] 数组以及文件目录。这是代码:

    int filesize = 6022386; // filesize temporary hardcoded

    int bytesRead;
    int current = 0;
    try {
        byte[] byteArray = o.getFile();
         java.io.InputStream inStream = socket.getInputStream();
         bytesRead = inStream.read(byteArray, 0, byteArray.length);

        FileOutputStream fileOutStream = new FileOutputStream(o.getPath());
        BufferedOutputStream buffOutStream = new BufferedOutputStream(
                fileOutStream);
        current = bytesRead;

        do {
            bytesRead = inStream.read(byteArray, current,
                    (byteArray.length - current));
            if (bytesRead >= 0)
                current += bytesRead;
        } while (bytesRead > -1);

        buffOutStream.write(byteArray, 0, current);
        buffOutStream.flush();
        buffOutStream.close();
        inStream.close();
        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

现在我的问题是:我如何更改它,而不是使用instream,而是使用通过套接字发送的UpdateObject中的byte[]对象?我已经做了一些谷歌搜索,但我觉得我不知道要问的正确问题。任何帮助都会很棒!提前致谢!!!

最佳答案

将 try catch block 内的大部分代码替换为:

FileOutputStream fileOutStream = new FileOutputStream(
    UpdateObject.getDirectory()+"\\NPS Game.txt");
fileOutStream.write(UpdateObject.getBytes()); //this is the byte[] array
fileOutStream.close();

希望这有帮助。

关于Java 更新程序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15282348/

相关文章:

java - 我需要一种更好的方法来减少代码行,其中有很多 if 语句和条件调用方法

c - 验证来自内核的文件数字签名

file - 如何将文件读入 double 向量?

c# - TcpClient write 方法是否保证数据被传送到服务器?

java - 这段代码有什么问题吗?

Java REST 服务在 POST 上生成 405

java - LibGdx FPS 太多了。如何限制?

c++ - std::ofstream - setting seekp to wrong position

c# - 发送消息速度套接字

sockets - 关闭套接字时是否需要从epoll/kqueue中注销fd?