我正在尝试将XML文件从桌面服务器传输到Android客户端,但我在Android设备上只获得了整个文件的1024个字节。我的代码是:
byte[] mybytearray = new byte[(int) filePianificazione.length()];
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePianificazione));
bufferedInputStream.read(mybytearray, 0, mybytearray.length);
bufferedInputStream.close();
out.write(mybytearray, 0, mybytearray.length);
out.flush();
byte[] mybytearray = new byte[1024];
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = in.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
答案 0 :(得分:1)
首先声明byte[] mybytearray = new byte[1024];
然后你正在做一个
int bytesRead = in.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
在您的读取代码(Android客户端)中,您只读取1024个字节,因为这是输入缓冲区的长度,而您只读取一次。你需要有一个while
循环,它将继续从输入流中读取,然后将其写出来,直到你达到EOF。
类似的东西:
while(in.available() > 0)
{
int bytesRead = in.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
}
答案 1 :(得分:0)
在Java中复制流的规范方法如下:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}