In: Computer Science
Write a public Java method called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor.
The string should contain the following words:
{“the”, “quick”, “brown”, “fox”}
import java.io.FileWriter; // FileWriter class
import java.io.IOException;
public class Write {
public void WriteToFile()
{
String S[];
S= {“the”, “quick”, “brown”, “fox”};
try {
FileWriter fw = new
FileWriter("words.dat");
for(int i=0; i<4; i++)
{
fw.write(S[i]);
fw.write("\n");
}
fw.close();
System.out.println("Successfully
written in file.");
} catch (IOException e) {
System.out.println("Error");
e.printStackTrace();
}
}
public static void main()
{
Write obj= new Write();
obj.WriteToFile();
}
}