日期:2014-05-20  浏览次数:20917 次

将byte[]写入类对象中,不能完全写入
见了一个需要传输的类,如下
Java code
public class FileInfoRequest implements Serializable{

     private String sessionID;
     private byte[] fileContent;  
      
     public String getsessionID() {  
     return sessionID;  
     }  
      
     public void setsessionID(String sessionid) {  
     this.sessionID = sessionid;  
     }  
      
     
      
     public byte[] getFileContent() {  
     return fileContent;  
     }  
      
     public void setFileContent(byte[] fileContent) {  
     this.fileContent = fileContent;  
     }  
     
     

}


下面是写入类对象的代码:
Java code
sentFile=new File(filePath);
            FileInputStream fis=new FileInputStream(sentFile);
            
            byte[] b=new byte[64];
            try {
                fis=new FileInputStream(sentFile);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            request.setsessionID(sessionid);
            while(fis.read(b,0,b.length)!=-1){
                request.setFileContent(b);
            }
                        system.out.println(request.getfileContent().length);



打印出来的filecontent 的长度总是为64, 请问各位高手, 怎样才能把文件全部写入类对象中?

------解决方案--------------------
Java code


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


public class Test {
    public static void main(String[] args) {
    FileInfoRequest fileInfoRequest = new FileInfoRequest();
    fileInfoRequest.setsessionID("testSessionId");
    fileInfoRequest.setFileContent("testFileContent".getBytes());//创建测试对象,并初始化

    writeObjectToFile(fileInfoRequest, "c:/test.txt");
    fileInfoRequest = readObjectFromFile("c:/test.txt");
    System.out.println(fileInfoRequest.getsessionID() + "  "
        + new String(fileInfoRequest.getFileContent()));//读出对象,并输出
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    private static <T> void writeObjectToFile(T t, String fileName) {// 将一个对象 写入文件,对象是可序列化的
    if (t == null)
        return;// 对象不存在 默认不写
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(new FileOutputStream(fileName));
        oos.writeObject(t);
        oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束);
    } catch (Exception e) {
    } finally {
        try {
        if (oos != null)
            oos.close();
        } catch (Exception e) {
        }
    }
    }

    private static <T> T readObjectFromFile(String fileName) {// 从文件中读出一个对象
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(new FileInputStream(fileName));
        T t = (T) ois.readObject();
        return t;

    } catch (Exception e) {
    } finally {
        try {
        if (ois != null)
            ois.close();
        } catch (Exception e) {
        }
    }
    return null;
    }   
}

class FileInfoRequest implements Serializable {

    private String sessionID;
    private byte[] fileContent;

    public String getsessionID() {
    return sessionID;
    }

    public void setsessionID(String sessionid) {
    this.sessionID = sessionid;
    }

    public byte[] getFileContent() {
    return fileContent;
    }

    public void setFileContent(byte[] fileContent) {
    this.fileContent = fileContent;
    }

}