简体   繁体   中英

JMS TextMessage not recognizing CR LF

It appears to me like JMS TextMessage containing Java.lang.String isn't recognizing \\r\\n as line-break but instead treating the CR LF as part of input on a Windows machine.

@Override
    public void onMessage(Message message) {
        try {
      String text = ((TextMessage)message).getText();
      String line=null;
      BufferedReader br = new BufferedReader(new StringReader(text));      
      for(line = br.readLine(); line != null; line = br.readLine()) {
           System.out.println(line);
      }
      catch (JMSException e) {
             System.err.println( "Error processing message: " + e.getMessage() );
             e.printStackTrace();
        }

Can anyone provide any input and /or recommednations around the same.

It's not completely clear from your question, but it sounds like the string is read correctly on input , but it is not being formatted correctly by the println output .

Control characters like line-feeds and and carriage returns are just like any other character in a string. What makes them different is how they are interpreted by the output device, lie a terminal program (eg linux terminal, putty, etc.) or the windows command prompt.

If you are printing this string to a destination that does not interpret these characters correctly, you may not see proper formatting even if the string data is correct. For example some IDE's output windows do not correctly format certain control characters, so you'll see different formatting in your IDE than you would see in an actual terminal.

One possible reason could be that it is not sent properly from the source system. I would look there to see what can be done next. Most languages offer cross-platform EOL constant's which might come in handy. For instance, Ruby has four:

irb(main):002:0> require 'English'; test = "One"+ $INPUT_RECORD_SEPARATOR
=> "One\n"
irb(main):003:0> test1 = "One" + $/
=> "One\n"
irb(main):004:0> test2 = "One"+$-0
=> "One\n"
irb(main):005:0> require 'English';test3="One"+$RS
=> "One\n"
irb(main):006:0>

You can try this:

 String text = ((TextMessage)message).getText();
 if(text!=null)
     text=text.replaceAll("\r\n","");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM