当前位置:编程学习 > JAVA >>

java 使用随机流向文件写入数字乱码

import java.io.*;
public class RandomStream {
public static void main(String args[]){
File file = new File("E:/暴风影音","4.txt");
int a[] = {1,2,3,4,5,6,7,8,9,10};
try{
RandomAccessFile inAndOut = new RandomAccessFile(file,"rw");
for(int i = 0;i<a.length;i++){
inAndOut.writeInt(a[i]);
}



}
catch(IOException o) {
o.getCause();
}
}
}




网上说的什么字符编码不一致就完事了,众说纷纭,也没给出具体方案 究竟怎么改,如果懂得就帮我改改吧  分全给你,谢谢,我想知道我在这个程序加个什么语句才能不乱码 ,如果你没写代码 就别恢复了 谢谢







java 乱码 编码 --------------------编程问答-------------------- 把写的换下成这个。inAndOut.writeBytes(new String(i+""));

 writeInt和writeLong都转换成write(int i),说明上却是写入byte,估计问题就在这了。
    /**
     * Writes the specified byte to this file. The write starts at 
     * the current file pointer.
     *
     * @param      b   the <code>byte</code> to be written.
     * @exception  IOException  if an I/O error occurs.
     */
    public native void write(int b) throws IOException;
--------------------编程问答--------------------

import java.io.*;

public class RandomStream {
public static void main(String args[]) {
File file = new File("/Users/user/csdn_test", "4.txt");
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
try {
RandomAccessFile inAndOut = new RandomAccessFile(file, "rw");
for (int i = 0; i < a.length; i++) {
inAndOut.writeUTF(String.valueOf(a[i]));
}
inAndOut.close();
} catch (IOException o) {
o.getCause();

}
}

补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,