我有很多个byte[],现在要保存起来,请问应该怎么保存起来比较合适,要求后期要能拿出来使用!

解决方案 »

  1.   

    写文件吧public class testFile { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    byte [] dd=new byte[5];
    DataOutputStream BytWriter=null;
    try {
    BytWriter = new DataOutputStream(new FileOutputStream("byteFile.txt"));
    BytWriter.write(dd);

    } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    try {
    BytWriter.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    }}读取使用DataIutputStream 也很方便
      

  2.   

    保存:
                    /*以下是需要保存的数据 */
    byte[] ba1 = {1,2};
    byte[] ba2 = {3,4};
    byte[] ba3 = {5,6};
    /*以上是需要保存的数据 */

    /* 放到一个容器中,以便保存 */
    byte[][] container = {ba1,ba2,ba3};
    FileOutputStream fOutputStream = new FileOutputStream("D:/testfile");
    ObjectOutputStream outputStream = new ObjectOutputStream(fOutputStream);
    outputStream.writeObject(container);
    outputStream.close();
    fOutputStream.close();
    获取: FileInputStream fileInputStream = new FileInputStream("D:/testfile");
    ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
    byte[][] container = (byte[][])inputStream.readObject();
    inputStream.close();
    fileInputStream.close();
    for(byte[] ba : container){
    for(byte b : ba){
    System.out.print(b+"\t");
    }
    System.out.println();
    }
      

  3.   

    可以再建一个byte[]去存储其他的byte[],就是用二维数组