How do you print symbols in Java to a file when you have only the symbol description?
I received a string from DB2 which contains symbols.
Two samples:
1) <0800>
2) 51V 3801Z
Such a string goes to two different places. One is a JSP rendering it as HTML. That is perfect; I get <0800> and 51V 3801Z, respectively. The other place is a CSV file created with java.io.FileWriter, and it does not convert to "<", ">", and " ". Instead, it is printed exactly as it came from DB2:
<0800>
and 51V 3801Z.
Is there anything the "new" nio library could help me? I have tried apache.commons.lang3.StringScapeUtils.escapeHTML4 without success.
I suggest looking into Apache's StringEscapeUtils, namely the unescapeHtml4() method.
Example:
String input = "<0800>";
String output = StringEscapeUtils.unescapeHtml4(input);
Ensure you are using the unescapeHtml4 method, and not the regular escapeHtml4 method!
I'm having trouble replacing return characters in a JTextArea in windows 7.
I have an input textArea that for data storage purposes I want to replace the "\r\n" with a unique string like "#!". Problem is, I can't seem to get it to replace it.
EX of issue:
JTextArea exampleText = new JTextArea("Enter Text",10,3);
String oneLineOfText = exampleText.getText().replace("\r\n","#!");
System.out.println(oneLineOfText);
Input:
Text
Text everywhere
Output:
Text
Text everywhere
Desired Output:
Text#!Text everywhere
I feel like I must be doing something really silly. This works perfectly fine in ubuntu when I use "\n" instead of "\r\n".
As I understand it, \r\n is a Windows line terminator.
Instead of looking for just a single line terminator, you could look for multiples and replace them.
For this you could use a regular expressiong and String#replaceAll, for example...
//String text = "This is\r\na test\r\nfor some text";
String text = "This is\na test\r\nfor some text";
System.out.println(text);
text = text.replaceAll("\r\n|\n", "#!");
System.out.println(text);
Which outputs...
This is
a test
for some text
This is#!a test#!for some text
You should also note, that not all text editors/text files have the \r\n line terminator, but Java does a pretty job of dealing with this...
Don't hard-code the newline character, use the system newline character:
System.getProperty("line.separator");
How do I get a platform-dependent new line character?
open a text file, and save the text of JTextArea in it. Now read the text from the saved text file charater by character by checking the following line,
string variable s="";
if(ch=='\n')
add the #! to the string variable;
finally place this string variable to the output section.
I have a text file with 1000 lines in the following format:
19 x 75 Bullnose Architrave/Skirting £1.02
I am writing a method that reads the file line by line in - This works OK.
I then want to split each string using the "£" as a deliminater & write it out to
an ArrayList<String> in the following format:
19 x 75 Bullnose Architrave/Skirting, Metre, 1.02
This is how I have approached it (productList is the ArrayList, declared/instantiated outside the try block):
try{
br = new BufferedReader(new FileReader(aFile));
String inputLine = br.readLine();
String delim = "£";
while (inputLine != null){
String[]halved = inputLine.split(delim, 2);
String lineOut = halved[0] + ", Metre, " + halved[1];//Array out of bounds
productList.add(lineOut);
inputLine = br.readLine();
}
}
The String is not splitting and I keep getting an ArrayIndexOutOfBoundsException. I'm not very familiar with regex. I've also tried using the old StringTokenizer but get the same result.
Is there an issue with £ as a delim or is it something else? I did wonder if it is something to do with the second token not being read as a String?
Any ideas would be helpful.
Here are some of the possible causes:
The encoding of the file doesn't match the encoding that you are using to read it, and the "pound" character in the file is getting "mangled" into something else.
The file and your source code are using different pound-like characters. For instance, Unicode has two code points that look like a "pound sign" - the Pound Sterling character (00A3) and the Lira character (2084) ... then there is the Roman semuncia character (10192).
You are trying to compile a UTF-8 encoded source file without tell the compiler that it is UTF-8 encoded.
Judging from your comments, this is an encoding mismatch problem; i.e. the "default" encoding being used by Java doesn't match the actual encoding of the file. There are two ways to address this:
Change the encoding of the file to match Java's default encoding. You seem to have tried that and failed. (And it wouldn't be the way I'd do this ...)
Change the program to open the file with a specific (non default) encoding; e.g. change
new FileReader(aFile)
to
new FileReader(aFile, encoding)
where encoding is the name of the file's actual character encoding. The names of the encodings understood by Java are listed here, but my guess is that it is "ISO-8859-1" (aka Latin-1).
This is probably a case of encoding mismatch. To check for this,
Print delim.length and make sure it is 1.
Print inputLine.length and make sure it is the right value (42).
If one of them is not the expected value then you have to make sure you are using UTF-8 everywhere.
You say delim.length is 1, so this is good. On the other hand if inputLine.length is 34, this is very wrong. For "19 x 75 Bullnose Architrave/Skirting £1.02" you should get 42 if all was as expected. If your file was UTF-8 encoded but read as ISO-8859-1 or similar you would have gotten 43.
Now I am a little at a loss. To debug this you could print individually each character of the string and check what is wrong with them.
for (int i = 0; i < inputLine.length; i++)
System.err.println("debug: " + i + ": " + inputLine.charAt(i) + " (" + inputLine.codePointAt(i) + ")");
Many thanks for all your replies.
Specifying the encoding within the read & saving the original text file as UTF -8 has worked.
However, the experience has taught me that delimiting text using "£" or indeed other characters that may have multiple representations in different encodings is a poor strategy.
I have decided to take a different approach:
1) Find the last space in the input string & replace it with "xxx" or similar.
2) Split this using the delimiter "xxx." which should split the strings & rip out the "£".
3) Carry on..
So, I've got the following code to write to a file:
Formatter output = ....... // Creating the formatter works, writes to appropriate file.
output.format("%d\n", records.length);
for(GradeRecord gR:records)
{
output.format(gR.toString() + "\n");
}
Only problem is, the output doesn't have newline characters.
Doesn't work if I replace "\n" with "\r", either.
...I don't know why this doesn't work. Output is created and writes correctly. (I see the file created and everything is written in it, except for newline characters.)
you can use the format "%n" to output the platform specific newline using a formatter.
You want to use the correct line break string regardless of what platform it's being run on. You can do this dynamically using
String newline = System.getProperty("line.separator");
So you can later do
output.format(gR.toString() + newline);
You can try using \\n instead of \n
I'm trying to send an email in Java but when I read the body of the email in Outlook, it's gotten rid of all my linebreaks. I'm putting \n at the ends of the lines but is there something special I need to do other than that? The receivers are always going to be using Outlook.
I found a page on microsoft.com that says there's a 'Remove line breaks' "feature" in Outlook so does this mean there's no solution to get around that other than un-checking that setting?
Thanks
I've just been fighting with this today. Let's call the behavior of removing the extra line breaks "continuation." A little experimenting finds the following behavior:
Every message starts with continuation off.
Lines less than 40 characters long do not trigger continuation, but if continuation is on, they will have their line breaks removed.
Lines 40 characters or longer turn continuation on. It remains on until an event occurs to turn it off.
Lines that end with a period, question mark, exclamation point or colon turn continuation off. (Outlook assumes it's the end of a sentence?)
Lines that turn continuation off will start with a line break, but will turn continuation back on if they are longer than 40 characters.
Lines that start or end with a tab turn continuation off.
Lines that start with 2 or more spaces turn continuation off.
Lines that end with 3 or more spaces turn continuation off.
Please note that I tried all of this with Outlook 2007. YMMV.
So if possible, end all bullet items with a sentence-terminating punctuation mark, a tab, or even three spaces.
You can force a line break in outlook when attaching one (or two?) tab characters (\t) just before the line break (CRLF).
Example:
This is my heading in the mail\t\n
Just here Outlook is forced to begin a new line.
It seems to work on Outlook 2010. Please test if this works on other versions.
See also Outlook autocleaning my line breaks and screwing up my email format
You need to use \r\n as a solution.
Microsoft Outlook 2002 and above removes "extra line breaks" from text messages by default (kb308319). That is, Outlook seems to simply ignore line feed and/or carriage return sequences in text messages, running all of the lines together.
This can cause problems if you're trying to write code that will automatically generate an email message to be read by someone using Outlook.
For example, suppose you want to supply separate pieces of information each on separate lines for clarity, like this:
Transaction needs attention!
PostedDate: 1/30/2009
Amount: $12,222.06
TransID: 8gk288g229g2kg89
PostalCode: 91543
Your Outlook recipient will see the information all smashed together, as follows:
Transaction needs attention! PostedDate: 1/30/2009 Amount: $12,222.06 TransID: 8gk288g229g2kg89 ZipCode: 91543
There doesn't seem to be an easy solution. Alternatives are:
You can supply two sets of line breaks between each line. That does stop Outlook from combining the lines onto one line, but it then displays an extra blank line between each line (creating the opposite problem). By "supply two sets of line breaks" I mean you should use "\r\n\r\n" or "\r\r" or "\n\n" but not "\r\n" or "\n\r".
You can supply two spaces at the beginning of every line in the body of your email message. That avoids introducing an extra blank line between each line. But this works best if each line in your message is fairly short, because the user may be previewing the text in a very narrow Outlook window that wraps the end of each line around to the first position on the next line, where it won't line up with your two-space-indented lines. This strategy has been used for some newsletters.
You can give up on using a plain text format, and use an html format.
I had the same issue, and found a solution. Try this: %0D%0A to add a line break.
I have used html line break instead of "\n" . It worked fine.
Adding "\t\r\n" ( \t for TAB) instead of "\r\n" worked for me on Outlook 2010 . Note : adding 3 spaces at end of each line also do same thing but that looks like a programming hack!
You need to send HTML emails. With <br />s in the email, you will always have your line breaks.
The trick is to use the encodeURIComponent() functionality from js:
var formattedBody = "FirstLine \n Second Line \n Third Line";
var mailToLink = "mailto:x#y.com?body=" + encodeURIComponent(formattedBody);
RESULT:
FirstLine
SecondLine
ThirdLine
I had been struggling with all of the above solutions and nothing helped here, because I used a String variable (plain text from a JTextPane) in combination with "text/html" formatting in my e-mail library.
So, the solution to this problem is to use "text/plain", instead of "text/html" and no need to replace return characters at all:
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/plain");
For Outlook 2010 and later versions, use \t\n rather than using \r\n.
If you can add in a '.' (dot) character at the end of each line, this seems to prevent Outlook ruining text formatting.
Try \r\c instead of \n.
EDIT: I think #Robert Wilkinson had it right. \r\n. Memory just isn't what it used to be.
The \n largely works for us, but Outlook does sometimes take it upon itself to remove the line breaks as you say.
I also had this issue with plain/text mail type. Earlier, I used "\n\n" but there was two line breaks. Then, I used "\t\n" and it worked. I was using StringBuffer in java to append content.
The content got printed in next line in Outlook 2010 mail.
Put the text in <pre> Tags and outlook will format and display the text correctly.
i defined it in CSS inline in HTML Body like:
CSS:
pre {
font-family: Verdana, Geneva, sans-serif;
}
i defined the font-family to have to font set.
HTML:
<td width="70%"><pre>Entry Date/Time: 2013-09-19 17:06:25
Entered By: Chris
worklog mania
____________________________________________________________________________________________________
Entry Date/Time: 2013-09-19 17:05:42
Entered By: Chris
this is a new Worklog Entry</pre></td>
Because it is a query, only percent escaped characters work, means %0A gives you a line break. For example,
<a href="mailto:someone#gmail.com?Subject=TEST&body=Hi there,%0A%0AHow are you?%0A%0AThanks">email to me</a>
I also had this issue with plain/text mail type.Form Feed \f worked for me.
Sometimes you have to enter \r\n twice to force outlook to do the break.
This will add one empty line but all the lines will have break.
\r\n will not work until you set body type as text.
message.setBody(MessageBody.getMessageBodyFromText(msg));
BodyType type = BodyType.Text;
message.getBody().setBodyType(type);
I was facing the same issue and here is the code that resolved it:
\t\n - for new line in Email service JavaMailSender
String mailMessage = JSONObject.toJSONString("Your message").replace(",", "\t\n").trim();
RESOLVED IN MY APPLICATION
In my application, I was trying to send an email whose message body was typed by the user in text area. When mail was send, outlook automatically removed line break entered by user.
e.g if user entered
Yadav
Mahesh
outlook displayed it as
YadavMahesh
Resolution: I changed the line break character "\r\n" with "\par " ( remember to hit space at the end of RTF code "\par" )and line breaks are restrored.
Cheers,
Mahesh
Try this:
message.setContent(new String(body.getBytes(), "iso-8859-1"),
"text/html; charset=\"iso-8859-1\"");
Regards,
Mohammad Rasool Javeed
I have a good solution that I tried it, it is just add the Char(13) at end of line like the following example:
Dim S As String
S = "Some Text" & Chr(13)
S = S + "Some Text" & Chr(13)
if the message is text/plain using, \r\n should work;
if the message type is text\html, use < p/>
if work need to be done with formatted text with out html encoding.
it can be easy achieved with following scenario that creates div element on the fly and using <pre></pre> html element to keep formatting.
var email_body = htmlEncode($("#Body").val());
function htmlEncode(value) {
return "<pre>" + $('<div/>').text(value).html() + "</pre>";
}
Not sure if it was mentioned above but Outlook has a checkbox setting called "Remove extra line breaks in plain text messages" and is checked by default. It is located in a different spot for different versions of Outlook but for 2010 go to the "File" tab. Select "Options => Mail" Scroll down to "Message format" Uncheck the checkbox.