简体   繁体   English

Java更新程序问题

[英]Java Updating Program questions

I have a program that updates files on the computer using information sent by a server, using sockets. 我有一个程序,该程序使用服务器通过套接字发送的信息来更新计算机上的文件。 The way I had it worked, but i wanted to make it more intuitive, simpler, more reliable, etc. here is the previous code: 它的工作方式,但是我想使其更直观,更简单,更可靠,等等。这是以前的代码:

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();
    }

as you can see, in the do, while loop, it is using the input stream to get the data. 如您所见,在do, while循环中,它使用输入流获取数据。 now that i've updated my program, i have the stream sending an object called UpdateObject , which holds the byte[] array along with the file directory. 现在,我已经更新了程序,我让流发送了一个名为UpdateObject的对象,该对象包含byte[]数组以及文件目录。 here is that code: 这是代码:

    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();
    }

now my question is this: how do i change it so instead of using the instream , to use just a byte[] object in the UpdateObject sent over the socket? 现在我的问题是:我该如何更改它,而不是使用instream ,而只使用通过套接字发送的UpdateObject的byte []对象? i've done some google searching, but i dont feel like i know the right question to ask. 我已经做了一些谷歌搜索,但我不觉得我知道问正确的问题。 any help would be great! 任何帮助将是巨大的! thanks in advance!!! 提前致谢!!!

By replacing most of your code inside the try catch block with: 通过将try catch块中的大多数代码替换为:

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

Hope this helps. 希望这可以帮助。

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

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