DataOutputStream类实现了DataOutput接口,
而DataOutput类有个方法:writeChars(用于Unicode格式的String)
应该可以往文件中写入中文!

解决方案 »

  1.   

    DataOutputStream不能直接往文件里写,一般是结合,FileOutputStream写到文件里。
      

  2.   

    DataOutputStream myDataStream;
    FileOutputStream myFileStream; 
    BufferedOutputStream myBufferStream; //get a file handle 
    mhyFileStream = new FileOutputStream("/usr/db/stock.dbf"); 
    //chain a buffered output stream (for efficiency); 
    myBufferStream = new BufferedOutputStream(myFileStream); 
    //chain a data output file myDataStream = new DataOutputStream(myBufferStream); //Now we can use both input streams to access our file 
    //(iiIf we want to ...) 
    myBufferStream.write(b); 
    myDataStream.writeInt(i); //close the data file explicitly 
    //Always colse the "topmost" file stream 
    myDataStream.close(); 
    myBuffersStream.close(); 
    myFileStream.close(); 
      

  3.   

    用BufferedWriter可以写入,为什么非要用DataOutputStream???
      

  4.   

    FileOutputStream f = new FileOutputStream("C:/abc.txt",true);
    DataOutputStream filed = new DataOutputStream(f);
    filed.writeUTF("这是我的第一句话!");
    filed.close();这段代码就能执行。文件abc里就会有中文的“这是我的第一句话!”,不过前面会有两个你看不懂的垃圾字符(那两个好像是UTF-8字符),不用管它。
      

  5.   

    哦,我找到说明了:
    文件中前两个字符显示为空格或垃圾字符。子是因为writeUTF()再UTF字符的前面加上两个字节,表示数据占用的字节数。
      

  6.   

    static String toChinese(String strvalue) {
        try {
          if (strvalue == null) {
            return null;
          }
          else {
            strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
            return strvalue;
          }
        }
        catch (Exception e) {
          return null;
        }
      }
    有人发的贴子中的这段代码可以读中文,但读不了 noratong(诺拉) 提供代码写的中文,
    难道这也得配套才行吗??
      

  7.   

    InputStreamReader和OutputStreamWriter在构造的时候有个Charaset参数
    设定对了就可以。如设定为UTF-8
      

  8.   

    老大这是读文件的:
    FileInputStream file = new FileInputStream("C:/abc.txt");
    DataInputStream in = new DataInputStream(file);
    String strtxt = "";
    while (strtxt != null)
    {
       strtxt = in.readUTF();
       System.out.println(strtxt);
    }
    in.close();
      

  9.   

    不行啊
    老大,你自己试试看。。
    readUTF();异常
      

  10.   

    DataInputStream in = new DataInputStream(System.in);
    try ()
    {
      byte[] bt = new byte[1024];
      in.read(bt);  .......................
    }
      

  11.   

    对了,老大读文件的代码我没又给你写在try、catch块中,你自己不能写一下吗???你出现那个错误的原因就是没把读文件的代码也写进try、catch块中,写进去就行了!
      

  12.   

    我明白了,不过不能在Visual J++ 下运行,java.io.UTFDataFormatException
    JBuilder 下可以通过