I have a requirement to send the sms and email on the trigger of some event.
It's working fine when I am testing with english. But when I change the text to Japanese, it's producing some sort of junk message. I am using java as my programming language.
I have tried some solutions like changing the charset preference and adding -Dfile.encoding=UTF8 in run configurations, but doesn't seem to work.
It's not working in a particular case.
When I hard-code the string in my java class as Japanese string then it's working fine. But when I try to read from property file it's producing some junk characters.
Finally I have solved this. As I have stated in my question itself, that the problem is mainly with reading from .property file. When I tried to hardcode the string it's working perfectly.
As #Henry suggested in his comments, all the .property files contain only ISO8859-1 characters. So, I followed the process to convert string from ISO8859-1 format to UTF.
It can be achieved by simply using this one line of code.
String utf8String = new String(Charsets.ISO_8859_1.encode("your string").array()).
Although, It solved my purpose, But I thought it's not a clean way to solve this.
For my scenario I had to add a new configuration, so instead of going through above way. I kept the string in separate text file and read from the file.
String filePath = "filename.txt"
try {
BufferedReader br = new BufferedReader(new FileReader(filePath));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
return sb.toString();
} catch (FileNotFoundException e) {
log.error("file not found ", e);
} catch (IOException e) {
log.error("Error occurred ", e);
}
It solved the issue without using any hack to convert from one CharSet to another.
Related
I am trying to create an authentication system of sorts that uses a file called Users.dat to store user data. Currently, I am developing a method to remove users by rewriting the Users.dat file, omitting the user specified. The code below works in a basic environment with an all-encompassing directory containing the .java files and the Users.dat file in the same spot. The old Users.dat file is deleted and Users.dat.tmp is renamed to User.dat. (No problems here, everything works as intended).
public static boolean RemoveUser(String userName) {
// TODO remove username from Users.dat
try {
File originalFile = new File("Users.dat");
System.out.println(originalFile.getAbsolutePath());
BufferedReader read = new BufferedReader(new FileReader("Users.dat"));
String line = null;
while ((line = read.readLine()) != null) {
if (line.indexOf(userName) != -1) {
break;
}
}
String[] userInfo = line.split(", ");
if (!userName.equals(userInfo[2])) {
System.out.println("Username not found. No users removed.");
read.close();
return false;
}
File tempFile = new File(originalFile.getAbsolutePath() + ".tmp");
PrintWriter print = new PrintWriter(new FileWriter(tempFile));
String lineToRemove = line;
BufferedReader read2 = new BufferedReader(new FileReader("Users.dat"));
while ((line = read2.readLine()) != null) {
if (!line.trim().equals(lineToRemove)) {
print.println(line);
print.flush();
}
}
print.close();
read.close();
read2.close();
System.out.println(originalFile.getAbsolutePath());
originalFile.delete(); //This line is not executing correctly
tempFile.renameTo(originalFile); //Nor is this line
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return true;
}
Users.dat file format:
Joe, Last, jlast, 58c536ed8facc2c2a293a18a48e3e120, true
Sam, sone, samsone, 2c2a293a18a48e3e12058c536ed8facc, false
Jane, Best, jbest, 293a18a48e3e12052058c536ed8facc2c, false
Andrew, Estes, Aestes, 63a490d69aa544fd1272a976014ad570, true
Test, User, tuser, 63a490d69aa544fd1272a976014ad570, true
I have two System.out.println(originalFile.getAbsolutePath()) statements, one at the beginning, one at the end to make sure the path isn't getting screwed up in the process of everything somehow.
Like I said, the code works, however, when I try to implement it in my project, it creates the Users.dat.tmp and it writes the correct data to it, but it does not delete the old Users.dat file, nor does it rename the Users.dat.tmp file to replace Users.dat. I'm certain the directory is correct, as I am literally displaying it as the code executes. I can't figure out any other reason why originalFile.delete() and tempFile.renameTo(originalFile) aren't functioning properly.
EDIT:
Using java.nio.file, I was able to produce an error message. it reads:
java.nio.file.FileSystemException: C:\Path\Users.dat: The process cannot access the file because it is being used by another process.
I don't have the file open when this error message is shown, and I don't get this error using java.nio in my testing environment mentioned at the beginning. I'm not sure what other process the message is referring to.
EDIT 2:
I tried running the code on other machines, one a Mac, the other a Windows laptop, and the code functioned on the Mac just fine, but I was still seeing the same issue on the Windows laptop.
I had the similar issue. My problem was not closing all the streams I read and written to the file. Thanks for your Edit #1, that was helpful
When you wrap
BufferedReader read = new BufferedReader(new FileReader("Users.dat"));
don't you need to close the inner readers too?
If not for the author, but for those who stambled upon this question (like me), hope this suggestion will be useful
I had an earlier function that I was calling in main that was accessing Users.dat, but I never closed the BufferredReader in that function.
I have a requirement where a huge HTML file must be read and displayed in the front-end of my application. The HTML file size is around 25MB.
Tried several options like:
Option 1:
try (Scanner scnr = new Scanner(file);) {
while (scnr.hasNextLine()) {
String line= scnr.nextLine();
}
}
Option 2:
FileUtils.readFileToString(file, "UTF-8");
Option 3:
IOUtils.toString(new FileInputStream(new File(file)), "UTF-8")
All the above 3 options are failing to read the file. I see no error. The processing just stops and the webpage throws an "error" popup with no info.
Problem seems to be that the entire HTML file content is read as a single line of string.
Is there a way in which I can read this file?
I went through several other questions here to see if there is a possible solution, but nothing seems to be working for this case.
#user811433, I did some testing with Apache Commons IO reading a log file with size around 800MB and no error occurred in the execution.
This method opens an InputStream for the file. When you have finished
with the iterator you should close the stream to free internal
resources. This can be done by calling the LineIterator.close() or
LineIterator.closeQuietly(LineIterator) method.
In case you process line by line like a Stream, The recommended usage pattern is something like this:
File file = new File("C:\\Users\\lucas\\Desktop\\file-with-800MB.log");
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try {
while (it.hasNext()) {
String line = it.nextLine();
// do something with line, here just sysout...
System.out.println( line );
}
} finally {
LineIterator.closeQuietly(it);
}
Some extra references, here and here
try {
File f=new File("test.html");
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String content=null;
while((content=reader.readLine())!=null)
{
System.out.println(content);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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 (e.g. 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","");
I've got a loop that reads through a text file and outputs it, now I'm trying to get it to loop through, and write what's printed out into a text file as I want it to display as HTML. This is what I've got so far for this method:
public void hChoice()
{
File fbScores = new File ("P:/SD/Assignment1/fbScores.txt");
String line = "";
try {
Scanner scanScores = new Scanner(fbScores);
while(scanScores.hasNext())
{
line = scanScores.nextLine();
stringArr = line.split(":");
if(stringArr.length == 4)
{
System.out.println("<h1>" + stringArr[0]+" [" +stringArr[2]+"] |" + stringArr[1]+" ["+ stringArr[3]+" ]<br></h1> ");
PrintWriter out = new PrintWriter("P:/SD/Assignment1/HTMLscores.txt");
out.close();
}
}
}
catch (FileNotFoundException e)
{
System.out.println("problem " +e.getMessage());
}
}
I've added the HTML tags in the print out and it prints it out fine, but I've tried several different methods to get it to print to a text file but none have worked. Pretty new to Java so any help would be much appreciated. Thankyou. :)
You've gotten your syntax and code wrong for writing to files.
Please Google and check the right syntax for writing to files using java. Plenty of resources available. You'll learn better if you try it yourself.
FYR, here is one: http://www.tutorialspoint.com/java/java_files_io.htm
I have the following lines of code that reads a TXT file (most of the time generated in Windows so it's ISO format) and it has the character Ñ. When I try to upload the file using Java on my workstation which is a Windows Machine, it is displayed properly and saved in the database properly. However, when I run the application on my app server which is Unix, the character Ñ is not displayed properly and turns into a symbol.
Here is the java code:
BufferedReader br = new BufferedReader(new InputStreamReader(fu.getInputStream()));
String input;
try {
while((input = br.readLine()) != null) {
if(!input.equals("")) {
items.add(input);
}
}
} finally {
br.close();
}
Pass the InputStreamReader's constructor the charset parameter.
http://docs.oracle.com/javase/7/docs/api/java/io/InputStreamReader.html
Use the following syntax to read the file.
List<String> lines = Files.readAllLines(Paths.get("yourfile"),
StandardCharsets.UTF_8);