Link to home
Create AccountLog in
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.
ASKER CERTIFIED SOLUTION
Avatar of Ajay-Singh
Ajay-Singh

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of thomas908
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
"\n" should work, alternatively, you can use this

out.write(code+" : "+invalidChars[i]);
out.write(System.getProperty("line.separator"));