Java I/O How to - Write an array of strings to a file








Question

We would like to know how to write an array of strings to a file.

Answer

/*  w  w w. j ava 2 s  .c om*/
import java.io.FileWriter;

public class Main {
  public static void main(String[] argv) throws Exception {
    FileWriter fw = new FileWriter("file.dat");
    String strs[] = { "com", "exe", "doc" };

    for (int i = 0; i < strs.length; i++) {
      fw.write(strs[i] + "\n");
    }
    fw.close();
  }
}