here's a little code. This class runs on two computers, one side sends a file (send()) and the other one recieves it (read()). I know send() works because when i run school solution (its an assignment) it can download a file from me, but for some reason when i try to download the file is created (by the constructor) but read doesn't write anything into the file.
这是一个小代码。该类在两台计算机上运行,一方发送文件(send()),另一方接收文件(read())。我知道send()有效,因为当我运行学校解决方案(它的一个任务)它可以从我下载文件,但由于某种原因,当我尝试下载文件时(由构造函数)创建但读取不写任何东西进入文件。
public class SendFile extends BasicMessage implements Message{
private File _file;
public SendFile(CommandEnum caption){
super(caption);
}
public SendFile(String file){
super(CommandEnum.FILE);
_file = new File(FMDataManager.instance().getSharedDirectory(),file);
}
public void send (DataOutputStream out) throws IOException{
out.writeUTF(_caption.toString());
out.writeLong(_file.length());
FileInputStream fis = new FileInputStream(_file);
BufferedInputStream bis = new BufferedInputStream(fis);
for (int i=0; i<_file.length(); i++)
out.write(bis.read());
out.writeUTF(CommandEnum.END.toString());
}
public void read(DataInputStream in) throws IOException{
FileOutputStream fos = new FileOutputStream(_file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
in.readUTF();
long size = in.readLong();
for (int i=0; i<size; i++)
bos.write(in.read());
System.out.println(in.readUTF());
}
}
any ideas? thanks
有任何想法吗?谢谢
1 个解决方案
#1
0
You must close your streams to ensure it is correct. In your particular case, file content is probably still inside the BufferedOutputStream.
您必须关闭流以确保它是正确的。在您的特定情况下,文件内容可能仍在BufferedOutputStream中。
#1
0
You must close your streams to ensure it is correct. In your particular case, file content is probably still inside the BufferedOutputStream.
您必须关闭流以确保它是正确的。在您的特定情况下,文件内容可能仍在BufferedOutputStream中。