This is the method Icreated for my personal use:
/** This method creates a file and writes the lines of fileContent into it
* @param file file path (relative or absolute)
* @param fileContent Array containing the lines to be written into the file
* @param doOutput Do some output in the command window
*/
public static void saveToFile (String file, String[] fileContent, boolean doOutput) {
saveToFile (new File (file), fileContent, doOutput);
}
/** This method creates a file and writes the lines of fileContent into it
* @param file output file
* @param fileContent Array containing the lines to be written into the file
* @param doOutput Do some output in the command window
*/
public static void saveToFile (File file, String[] fileContent, boolean doOutput) {
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, false)));
if (doOutput)
System.out.print ("Writing ");
for (int i = 0; i < fileContent.length; i++) {
out.println(fileContent[i]);
if (doOutput)
System.out.print (".");
}
if (doOutput)
System.out.println (" done");
out.close();
} catch (IOException e) {
System.out.println("I/O Error: " + e.getMessage());
}
}
The important lines are:
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, false)));
out.println(fileContent[i]);
out.close();
PrintWriter and File are in the java.io package.