小弟在初学阶段,为什么这段代码写到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();
}
}
在读/写操作时应该加上编码吧?如"gb2312","utf-8"等
不知这能不能帮到你
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(); } } }