I am unable to write a file .In Java Eclipse. This is what I have: public static
ID: 656624 • Letter: I
Question
I am unable to write a file .In Java Eclipse. This is what I have:
public static void WriteToFile() {
{
try {
String content = "asdf";
//this is the file location on your computer
File file = new File("filename.txt");
//array list
ArrayList<String> id = new ArrayList();
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
//this is the output of arraylist to file: filename
FileWriter fw = new FileWriter(file.getAbsoluteFile());
//buffer out the text file
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
for (int i = 0; i<id.size(); i++) {
//output to the file
bw.write(id.get(i).toString()+ " ");
}
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
what AM I doing wrong? my file is blank
Explanation / Answer
public static void WriteToFile()
{
try
{
String content = "asdf";
//this is the file location on your computer
File file = new File("filename.txt");
//array list
ArrayList<String> id = new ArrayList();
// if file doesnt exists, then create it
if (!file.exists()
{
file.createNewFile();
}
}
//this is the output of arraylist to file: filename
FileWriter fw = new FileWriter(file.getAbsoluteFile());
//buffer out the text file
BufferedWriter bw = new BufferedWriter(fw);
{
bw.write(content);
{
for (int i = 0; i<id.size(); i++)
{
//output to the file
bw.write(id.get(i).toString()+ " ");
{
bw.close();
System.out.println("Done");
catch (IOException e)
{
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.