0

I am trying to write a series of line to a text file using Java.

Code:

File file = new File("file.txt");
FileWriter writer = new FileWriter(file,true);
for (first for loop) {
            for (second for loop) {
                if (condition TRUE){
                    writer.write(element1 + element2.toString()+"\n");
                }
                else {
                writer.write("-"+ element1 + element2.toString()+"\n");
                }
            }
        }

Error:

Only an empty file is getting created. Please, any suggestions would be helpful

data_person
  • 3,024
  • 7
  • 28
  • 52
  • 1
    Can you give us your actual code? Using `FileWriter` is fine, you do need to close it though. – jiveturkey May 11 '17 at 21:18
  • @jnbbender thanks for your time the given answer solved it. – data_person May 11 '17 at 21:23
  • Possible duplicate of [How do I create a file and write to it in Java?](http://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it-in-java) – Thierry May 11 '17 at 22:12

3 Answers3

0

Replace this:

FileWriter writer = new FileWriter(file,true);

with:

BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));

and in the end:

writer.close();
z m
  • 1,215
  • 2
  • 15
  • 19
0

There is nothing wrong with the snippet you provided. The problem could be that, the conditions in your for loops and if statements are never met. Thus your code never gets to

writer.write(element1 + element2.toString()+"\n");.

So to be able to help on that unless you provide the actual code.

Also you must ensure that file.txt exist. It would be better if you use an absolute path like C:\\Users\\YOUNG MILLIE\\Documents\\file.txt in windows environment.

Because i tried with this example and it works fine.

        File file = new File("C:\\Users\\YOUNG MILLIE\\Documents\\file.txt");
        FileWriter writer = new FileWriter(file,true);
        for (int i = 0; i <= 100;i++) {
            for (int k = 0; k <= 50;k++) {
                if (i <=50 ){
                    writer.write(k +"\n");
                }
                else {
                    writer.write("-"+ k +"\n");
                }
            }
        }
        writer.close();
Young Emil
  • 1,892
  • 1
  • 19
  • 30
-1

Here is how the code should look like and you can substitute your logic wherever applicable.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteAFile {

    public static void main(String [] args) {
        String element1="First Element";
        Integer element2=10;
        FileWriter writer = null;

        try {
            writer = new FileWriter(new File("file.txt"), true);

            for (int i=0; i<5; i++) {
                for (int j=0; j<5; j++) {
                    if (true){ //Write the condition here
                        writer.write(element1 + element2.toString()+"\n");
                    }
                    else {
                        writer.write("-"+ element1 + element2.toString()+"\n");
                    }
                }
            }
            writer.close();
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
rslj
  • 179
  • 2
  • 10