Avatar of thomas908
thomas908
 asked on

Writing to a File in Java


I am using the following code to write to a text file. The file is getting created but it is empty.

BufferedWriter out = null;
        try {
            out = new BufferedWriter(new FileWriter("C:\\abc.txt", true));
        }catch(Exception e) {
            e.printStackTrace();
        }
        for(int i=0; i<invalidCodesList.size(); i++) {
            int code = ((Integer)invalidCodesList.get(i)).intValue();
            invalidChars[i] = new Character((char)(code)).toString();
            try {
               
                out.write(code+" : "+invalidChars[i]);
                Logger.getInstance().writeLog(code+" : "+invalidChars[i]);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            Logger.getInstance().writeLog("ASCII code value is ----- "+code+"::::: character is ::  "+invalidChars[i]);
        }

Please tell me why it is not writing to the file.
Java

Avatar of undefined
Last Comment
Ajay-Singh

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ajay-Singh

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
thomas908

ASKER
Thank You
Can you also please tell me how to get a newline in the text file. All the input is comiing in single line.
I have tried "\n" but is is not working
Ajay-Singh

"\n" should work, alternatively, you can use this

out.write(code+" : "+invalidChars[i]);
out.write(System.getProperty("line.separator"));
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck