Jesus
Jesus

Reputation: 8586

why my log doesn't show newlines in notepad only in writer (Java)

I'm developing an app in Java which captures string and generates a log text

public static void createTXTLog(String pathLog, String textLog){
    try{
        BufferedWriter writer = new BufferedWriter(new FileWriter(pathLog));
        writer.write(textLog);
        writer.close();
    }
    catch(Exception e){
        System.out.println("An error occured while generating log txt: \n" + e.toString());
    }
}

when I open my log generated by this method it shows in notepad something like this:

09-12-2013 17:48:18 :[INF] Creando la conexion a la BD... 09-12-2013 17:48:18 :[eW003][ERR][inicioProceso] Error en la creacion de la conexion a la BD: java.sql.SQLException: Listener refused the connection with the following error:ORA-12505, TNS:listener does not currently know of SID given in connect descriptor The Connection descriptor used by the client was:180.176.40.134:1522:DBDESAS

But if I open this on Writer it shows properly:

09-12-2013 17:48:18 :[INF] Creando la conexion a la BD... 

09-12-2013 17:48:18 :[eW003][ERR][inicioProceso] Error en la creacion de la conexion a la BD: 
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
180.176.40.134:1522:DBDESAS

Any idea how to fix it in notepad??? thanks in advance

Upvotes: 0

Views: 113

Answers (2)

C.Champagne
C.Champagne

Reputation: 5489

This is certainly because you use "\n" instead of "\r\n" which the line separator in Windows.

Upvotes: 1

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79875

Your problem is that you are only returning a new-line, not a carriage-return and new-line; which is what you need for a Windows text file. You could change this to the following.

System.out.println("An error occured while generating log txt:" + System.lineSeparator() + e);

Or if you're running on something other than Windows but will be looking at your file in Notepad in Windows, you could do this.

System.out.println("An error occured while generating log txt: \r\n" + e);

I took the liberty of removing the redundant toString() call.

Upvotes: 4

Related Questions