我在这里有一个代码,我将在我的 Java 服务器中使用它作为日志记录。这似乎在单行输入中运行良好。但是当我输入多行时。例如:
Hey there!
Hey More!
当我查看 PrintWriter 创建的文本文件时,它告诉我:
Hey there!Hey More!
我想在我的文本文件上看到的是:
Hey there!
Hey More!
这是我的代码:我不知道我将如何附加它。我正在考虑 \n 但这不起作用。或者我的代码中是否缺少某些内容。请检查,你会对我有很大的帮助!谢谢!
try
{
clientSocket = serverSocket.accept(); // accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
StringBuilder str =new StringBuilder();
StringBuilder abc =new StringBuilder();
while ((message = bufferedReader.readLine()) != null)
{
abc.append("Message from " + message + " at " + dateFormat.format(cal.getTime())+"/n");
System.out.println("Message from " + message + " at " + dateFormat.format(cal.getTime()));
str.append(message+"\n");
}
PrintWriter out= new PrintWriter(new BufferedWriter(new FileWriter("C:/god.txt", true)));
out.println(abc);
out.close ();
JOptionPane.showMessageDialog(null, str);
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex)
{
System.out.println("Problem in message reading");
}