求助!自定义RandomChar 类产生随机字母写入文件产生乱码。 PrintWriter 乱码java 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 FileWriter 用于写入字符流。要写入原始字节流,使用 FileOutputStream。参考代码import java.io.*;import java.util.*;public class TestRandom { public static void main(String[] args){ try { //FileWriter file = new FileWriter("myfile.txt",false); FileOutputStream file = new FileOutputStream("myfile.txt",false); PrintWriter outfile = new PrintWriter(file); for(int i = 0; i< 1001; i++){ outfile.println(RandomChar.createLower()+" "); } outfile.flush(); outfile.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } }}class RandomChar { //a-z 97-122 //A-Z 65-90 public static char createUpper(){ Random uppercaserad = new Random(); char uppercase = (char) (65 + uppercaserad.nextInt(26)); return uppercase; } public static char createLower(){ Random lowercaseintrad = new Random(); char lowercase = (char) (97 + lowercaseintrad.nextInt(26)); return lowercase; } }这样你的问题就解决了。。 还是乱码诶。。我运行都没问题的,看我的输出文件,下图:用println输出用print输出 你把打印的outfile.println(RandomChar.createLower()+" ");里面的空格删掉一个看看是不是乱码。偶数个的空格是正常的,奇数个的空格就是乱码 myeclipse连接oracle处错误 用JAVA语言编写一个程序求一个一元二次方程的根(用弦截法) 很不明白????????????? 我们用cvs进行版本控制,我现在想将源代码中一目录的名称从大写改为小写,应该怎么操作呀? 关于正则表达式的问题。 如何将char 转换为 byte 请问在JList上的内容有了变动之后,如何更新显示? Linux下如何运行eclipse? j2se 1.4.1 Beta NOW AVAILABLE! DXF 如何用JAVA去写 编译器明明可以确定为错误,为什么只报出警告或任何警告错误都不报出? 是不是老年代的的gc都是fullgc
import java.util.*;
public class TestRandom {
public static void main(String[] args){
try {
//FileWriter file = new FileWriter("myfile.txt",false);
FileOutputStream file = new FileOutputStream("myfile.txt",false);
PrintWriter outfile = new PrintWriter(file);
for(int i = 0; i< 1001; i++){
outfile.println(RandomChar.createLower()+" ");
}
outfile.flush();
outfile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
class RandomChar {
//a-z 97-122
//A-Z 65-90
public static char createUpper(){
Random uppercaserad = new Random();
char uppercase = (char) (65 + uppercaserad.nextInt(26));
return uppercase;
}
public static char createLower(){
Random lowercaseintrad = new Random();
char lowercase = (char) (97 + lowercaseintrad.nextInt(26));
return lowercase;
}
}
这样你的问题就解决了。。
还是乱码诶。。我运行都没问题的,看我的输出文件,下图:
用println输出用print输出
你把打印的outfile.println(RandomChar.createLower()+" ");里面的空格删掉一个看看是不是乱码。偶数个的空格是正常的,奇数个的空格就是乱码