解决方案 »

  1.   

    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;
        }
         
    }
    这样你的问题就解决了。。
      

  2.   


    还是乱码诶。。我运行都没问题的,看我的输出文件,下图:
    用println输出用print输出
      

  3.   


    你把打印的outfile.println(RandomChar.createLower()+"  ");里面的空格删掉一个看看是不是乱码。偶数个的空格是正常的,奇数个的空格就是乱码