多次调用方法

时间:2020-04-11 15:32:10

标签: java

我正在尝试生成一个假的data.txt文件进行测试。我需要创建至少2000个数据 我需要打电话给content()2000多个时间。我该怎么做

  public static void main(String[] args) {

    String newline = System.getProperty("line.separator");
        try{
            // Create new file
            String content = content()+content();

           String path="/Users/raianazaman/Desktop/DataBase/Sample.txt";
           File file = new File(path);

           if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);

            bw.write(content);// Write in file
            bw.close();// Close connection
        }

1 个答案:

答案 0 :(得分:0)

使用for循环。

  public static void main(String[] args) {

    String newline = System.getProperty("line.separator");
    try {

      String path = "/Users/raianazaman/Desktop/DataBase/Sample.txt";
      File file = new File(path);

      if (!file.exists()) {
        file.createNewFile();
      }

      FileWriter fw = new FileWriter(file.getAbsoluteFile());
      BufferedWriter bw = new BufferedWriter(fw);

      for (int i = 0; i < 2000; i++) {
        bw.write(content());// Write in file
      }
      bw.close();// Close connection
    } catch (Exception e) {
      e.printStackTrace();
    }
  }