String 试图将随机字符串放入文件中

String 试图将随机字符串放入文件中,string,file,random,repeat,String,File,Random,Repeat,我正在尝试将随机字符串插入到.txt文件中。我的代码如下: import java.util.*; import java.io.*; class fileProcessing{ public static void main (String[] args) throws Exception{ letter(); } public static void letter() throws Exception{ int count = 0;

我正在尝试将随机字符串插入到.txt文件中。我的代码如下:

import java.util.*;
import java.io.*;

class fileProcessing{
   public static void main (String[] args) throws Exception{
      letter();
   }

   public static void letter() throws Exception{
      int count = 0;
         PrintStream out = new PrintStream(new File("nums.txt"));
            while (count < 7 ){
              Random rand = new Random();
                  int randomNum = 97 + rand.nextInt((122 - 97) + 1);
                     char a = (char)randomNum;
                        out.print(a);
                        count++;
      }
   }
}
import java.util.*;
导入java.io.*;
类文件处理{
公共静态void main(字符串[]args)引发异常{
字母();
}
public static void letter()引发异常{
整数计数=0;
PrintStream out=新的PrintStream(新文件(“nums.txt”);
而(计数<7){
Random rand=新的Random();
int randomNum=97+rand.nextInt((122-97)+1);
字符a=(字符)随机数;
打印(a);
计数++;
}
}
}

我尝试在一个.txt文件中放入一行7个随机字母,大约400次。我的代码只允许我输入一行7个字母。我不知道怎么把其余的399条线路接进来。任何帮助都将不胜感激。谢谢

使用包含已编写循环的
嵌套循环
。这样做的目的是,在生成一个单词后,进行新的迭代,在文件中写入另一个单词

import java.util.*
import java.io.*;

class FileProcessing
{
    public static void main(String[] args) throws IOException
    {
        letters();
    }

    public static void letters() throws IOException
    {
        int count;
        PrintStream out = new PrintStream(new File("nums.txt"));

        /*Outer loop. When the loop on the inside finishes generating 
         *a word, this loop will iterate again.
         */
        for(int i=0; i<400; ++i)
        {
             count=0;
             /*your current while loop*/
             while (count < 7)
             {
                 Random rand = new Random();
                 int randomNum = 97 + rand.nextInt((122-97)+1);
                 char a = (char) randomNum;
                 out.print(a);
                 count++;
             }
             //print new line so all words are in a separate line
             out.println();
        }
        //close PrintStream
        out.close();
    }
}
import java.util*
导入java.io.*;
类文件处理
{
公共静态void main(字符串[]args)引发IOException
{
字母();
}
public static void letters()引发IOException
{
整数计数;
PrintStream out=新的PrintStream(新文件(“nums.txt”);
/*外部循环。当内部循环完成生成时
*一句话,这个循环将再次迭代。
*/

对于(int i=0;i使用嵌套循环您是否尝试过另一个包含while循环的400循环?除了out.print(“\n”)。它需要是println。不过感谢您的回答。