Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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();

}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote