首页 新闻 会员 周边

一段关于Java文件流操作的代码问题

0
悬赏园豆:10 [已关闭问题] 关闭于 2010-05-19 14:19

小弟在初学阶段,为什么这段代码写到txt文件中之后是乱码呢。我看参考答案上面也是用的writeInt来写的。向各位高手请教解决之道。还有就是小弟积分少,没多少积分拿来悬赏。谢谢各位大侠了。
import java.io.*;
/**
 *
 * @author Administrator
 */
public class no10_6 {
 public static void main(String[] args) throws IOException
    {
        int a[]=new int [50];
        File file1=new File("d:
\\a.txt");
        FileOutputStream fout=new FileOutputStream(file1);
         DataOutputStream dout=new DataOutputStream(fout);
         
        for(i=0;i<50;i++)
        {
            a[i]=(int)(Math.random()*1000);
        try
        {

           dout.writeInt(a[i]);

        }
        catch(FileNotFoundException e)
        {
            System.out.println(e);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
           
            dout.close();
    }
}

青楼歌姬的主页 青楼歌姬 | 初学一级 | 园豆:122
提问于:2010-05-18 09:33
< >
分享
所有回答(2)
0

在读/写操作时应该加上编码吧?如"gb2312","utf-8"等

Astar | 园豆:40805 (高人七级) | 2010-05-18 09:50
怎么写啊?能给个例子么?
支持(0) 反对(0) 青楼歌姬 | 园豆:122 (初学一级) | 2010-05-18 12:43
0

不知这能不能帮到你

public class TestFile {

    public static void main(String[] args) {
        int number;
        String path = "d:\\a.txt";
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(path);
            for (int i = 0; i < 50; i++) {
                number = (int) (Math.random() * 1000);
                writer.print(number+" ");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally{
            writer.close();
        }
    }
}
SangBillLee | 园豆:212 (菜鸟二级) | 2012-05-29 18:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册