WANGYANG000001
WANGYANG000001
采纳率50%
2015-12-25 23:26 浏览 1.5k
已采纳

把数据+类型写入字节数组里这个程序总错,求大神指出错的地方,或者给出正确的代码,感激不尽

50

package 处理流;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Demo02 {
public static void main(String[] args) throws IOException{
read(write());
}
//把数据+类型写到字节数组里
public static byte[] write()throws IOException
{
byte[] b2=null;
ByteArrayOutputStream dos=new ByteArrayOutputStream();
DataOutputStream out=new DataOutputStream(
new BufferedOutputStream(dos));
double point=3.14159;
int num=2628;
String str="我是汪洋";
String str02="刘林是我同学";
out.writeDouble(point);
out.writeInt(num);
out.writeUTF(str);
out.writeUTF(str02);
dos.flush();
b2=dos.toByteArray();

    return b2;
}
//从字节数组中获得数据+类型
public static void read(byte[] b2)throws IOException
{
    DataInputStream into=new DataInputStream(
            new BufferedInputStream(
                    new ByteArrayInputStream(b2)));
    System.out.println("point="+into.readDouble());
    System.out.println("num="+into.readInt());
    System.out.println("str="+into.readUTF());
    System.out.println("str02="+into.readUTF());
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答 默认 最新

  • 已采纳
    danielinbiti danielinbiti 2015-12-26 02:31
    
    out.writeUTF(str02);
    out.flush();//加上这句,把流输出到dos
    dos.flush();
    
    点赞 2 评论 复制链接分享
  • devmiao devmiao 2015-12-26 02:38

    用ultraedit看下。文件写入正确了么

    点赞 2 评论 复制链接分享
  • WANGYANG000001 WANGYANG000001 2015-12-25 23:27

    粘贴的时候重复了一片,大神们不要介意

    点赞 评论 复制链接分享
  • lbl231249 LBL121520 2015-12-26 02:13

    请粘贴异常信息,你这样总不能让我拿去运行看看吧,浪费时间

    点赞 评论 复制链接分享
  • Mr_dsw Andoter 2015-12-26 04:16

    将这句修改了就好了:
    DataOutputStream out=new DataOutputStream(dos);

        然后就正常了,完整代码:
    
                public static void main(String []args){
            try {
                read(write());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
    
        //把数据+类型写到字节数组里
        public static byte[] write()throws IOException
        {
            byte[] b2=null; 
            ByteArrayOutputStream dos=new ByteArrayOutputStream();
            DataOutputStream out=new DataOutputStream(dos);
            double point=3.14159;
            int num=2628;
            String str="我是汪洋";
            String str02="刘林是我同学";
            out.writeDouble(point);
            out.writeInt(num);
            out.writeUTF(str);
            out.writeUTF(str02);
            dos.flush();
            b2=dos.toByteArray();
            return b2;
        }
        //从字节数组中获得数据+类型
        public static void read(byte[] b2)throws IOException
        {
            DataInputStream into=new DataInputStream(
                    new BufferedInputStream(
                            new ByteArrayInputStream(b2)));
            System.out.println("point="+into.readDouble());
            System.out.println("num="+into.readInt());
            System.out.println("str="+into.readUTF());
            System.out.println("str02="+into.readUTF());
        }
    
    点赞 评论 复制链接分享

相关推荐