I wrote a code that gets a JSON text from a website and formats it so it is easier to read. My problem with the code is:
public static void gsonFile(){
try {
re = new BufferedReader(new FileReader(dateiname));
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
String uglyJSONString ="";
uglyJSONString = re.readLine();
JsonElement je = jp.parse(uglyJSONString);
String prettyJsonString = gson.toJson(je);
System.out.println(prettyJsonString);
wr = new BufferedWriter(new FileWriter(dateiname));
wr.write(prettyJsonString);
wr.close();
re.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It correctly prints it into the console : http://imgur.com/B8MTlYW.png
But in my txt file it looks like this: http://imgur.com/N8iN7dv.png
What can I do so it correctly prints it into the file? (separated by new lines)
Gson uses \n as line delimiter (as can be seen in the newline method here).
Since Notepad does not understand \n you can either open your result file with another file editor (Wordpad, Notepad++, Atom, Sublime Text, etc.) or replace the \n by \r\n before writing it:
prettyJsonString = prettyJsonString.replace("\n", "\r\n");
FileReader and FileWriter are old utility classes that use the platform encoding. This gives non-portable files. And for JSON one ordinarily uses UTF-8.
Path datei = Paths.get(dateiname);
re = Files.newBufferedReader(datei, StandardCharsets.UTF_8);
Or
List<String> lines = Files.readAllLines(datei, StandardCharsets.UTF_8);
// Without line endings as usual.
Or
String text = new String(Files.readAllBytes(datei), StandardCharsets.UTF_8);
And later:
Files.write(text.getBytes(StandardCharsets.UTF_8));
It is problem with your text editor. Not with text. It incorrectly process new line character.
I suppose it expect CR LF(Windows way) symbols and Gson generate only LF symbol (Unix way).
After a quick search this topic might come handy.
Strings written to file do not preserve line breaks
Also, opening in another editor like the others said will help too
Related
I have a standard textfile written with Windows editor (or Word) und saved in ANSI-format in the android device. If I open and read this file and display it on my Android device all characters are displayed correctly except the German Umlaute äÄöÖüÜß. Instead of these characters a white question mark is shown within a black diamond. (I display them in a homescreen widget using remoteViews.setTextViewText(...)
I googled for hours and found lots of hints on using UTF-8 encoding etc. But when I save the file in UTF-8 or with any format but ANSI, I will get an exception and can't read the file at all. Using an android editor shows the encoding of the file is correct both in ANSI and in UTF-8.
My program is too long to copy it here so I extracted the hopefully relevant part and put it below. Please help!
public class Test {
static void readFile() {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "birthday.txt");
if (file.exists())
{
try {
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
//BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(directory+"birthday.txt"), "windows-1252"));
String lineOfText;
while ((lineOfText = br.readLine()) != null) {
//Output lineOfText via remoteViews.setTextViewText(WidgetOutput.getRef(linecounter).getIdWhat(), lineOfText);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
The commented BufferedReader line looks like it should work. However, windows-1252 is the canonical name for classes in java.nio. For classes in java.io (like InputStreamReader) the canonical name is Cp1252. See Supported Encodings
You may also wish to try ISO-8859-1 (nio) or ISO8859_1 (io).
How to convert txt to html with all words of file.txt ?
public class Main {
private static String name = "writer.html";
private static String Text = "C://Users//Vladimir//IdeaProjects//Algorithms//src//pack/textfile.txt";
public static String readtxt(String filename) throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(filename));
String s;
StringBuilder sb = new StringBuilder();
while((s = reader.readLine()) != null) {
sb.append(s + "\n");
}
reader.close();
return sb.toString();
}
public static Object writer(String fileName,String text){
Text = text;
try {
PrintWriter out = new PrintWriter(new File(fileName));
try {
out.print(Text);
} finally {
out.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
}
writer.html (output - C://Users//Vladimir//IdeaProjects//Algorithms//src//pack/textfile.txt )
First of all change this,
From -
sb.append(s + "\n");
To -
sb.append(s + "<br/>");
Also remove this,
Text = text; // not needed
change over here like,
out.print(text);
I think it should be work properly as per your requirement.
It seems that you are missing quite a lot in your code, and that you are on a learning path. If your class is not complete I'd suggest you take a look first how to correctly read from one file and write into another, like this:
File I/O: Reading from one file and writing to another (Java)
or just educate your self on File I/O in Java
In case you have that already covered, and you are wondering how to transfer from simple text file to html, I'd suggest next to look at HTML format as you should create valid html file (where the content of your text file will be copied into <body> element ) - http://www.w3schools.com/html/default.asp
Once you inject that into your target file you can start adding line by line from your txt file. For the sake of simplicity lets assume all your text will be inside single paragraph html element then you will separate each line using <br>
tag (as vishal mentioned).
For 'advanced' transformation you should escape your strings so that all words are correctly diplayed in the browser using something like Commons StringEscapeUtils - or check this thread:How to escape HTML special characters in Java?
Good luck
I don't understand this strange behaviour of regex match in Java. I'm working in Eclipse...
I have a .txt file encoded in UTF-8, where are blocks of text lines divided by these identifiers:
[a]
.
.
.
[b]
.
.
[c]
and so on...
My program is reading this file with this BufferedReader:
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream("file.txt", Charset.forName("UTF-8"))); }
catch (FileNotFoundException e) { System.out.println("File not found!"); e.printStackTrace(); System.exit(0); }
This should find the first identifier:
String line = "";
while (!line.matches("^\\[.\\]$")) { line = reader.readLine(); }
But it is instantly skipped!
But when I try to test the regex "manually", it works:
String line = "[a]";
if (line.matches("^\\[.\\]$")) { System.out.println("Regex matches"); }
It is possible that it is some trivial problem, but I get totally stuck at this point!
Thanks in advance for reply!
EDIT:
Well, I changed the encoding of text file to "ANSI" and it just started to work fine - OH MY GOD - why?! So there must be problem with the reader - I will try to find it out as soon as possible and edit my question.
So when the encoding of text file is "UTF-8", the regex doesn't match the first line of text file, where is "[a]" and matches the next identifier few lines below. What is wrong?
EDIT 2:
LOL I can't trust the Windows Notepad anymore - I had saved that file in it...and a few moments ago I saved that file using PSPad editor and now it works fine!
First of all FileInputStream does not have a constructor with parameters file and character set (JavaDoc JDK). Maybe you got a typo in your code?
new FileInputStream("file.txt", Charset.forName("UTF-8"))
So is this a different implementation of FileInputStream?
Second I tried this little test with a bracket modification:
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(
new FileInputStream("target/classes/test.txt"),
Charset.forName("UTF-8")));
String line = "";
while (!line.matches("^\\[.\\]$")) { line = reader.readLine(); }
System.out.println(line);
And it just worked and printed:
[a]
I am trying to edit a file with java.
I would like to escape every Quotation " in my file with \"
I tried it like this (regards to the other solution on stackoverflow, which code I could copy):
public void replaceInFile(File file) throws IOException {
File tempFile = new File("twittergeoUpdate.csv");
FileWriter fw = new FileWriter(tempFile);
Reader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while (br.ready()) {
fw.write(br.readLine().replaceAll("\"", "\\\"") + "\n");
}
fw.close();
br.close();
fr.close();
}
I was too fast... It doesn't work for me. The Quotation just stay untouched in my file. Any ideas ?
\\\" only escapes "(double quote), you have to escape the back-slashes aswell, thus you need 5 backslashes. \\\\\"
s.replaceAll("\"", "\\\\\"")
You should use StringEscapeUtils#escapeJava() from Apache commons-lang package.
Like this:
org.apache.commons.lang.StringEscapeUtils.escapeJava(<yourStringLiteralHere>)
From javadoc:
StringEscapeUtils#escapeJava() escapes the characters in a String using Java String rules.
Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)
So a tab becomes the characters '\' and 't'.
The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote must be escaped.
Example:
input string: He didn't say, "Stop!"
output string: He didn't say, \"Stop!\"
I coded:
public void replaceInFile(File file) throws IOException {
File tempFile = new File("twittergeoUpdate.csv");
FileWriter fw = new FileWriter(tempFile);
Reader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while (br.ready()) {
fw.write(br.readLine().replaceAll("\"", "\\\\\"") + "\n");
}
fw.close();
br.close();
fr.close();
}//replaceInFile
The correct replacement string is \\\" (5 backslash, not 3)
The basic problem is that you cannot write to a file you are reading from and not expect it to change. In your case, the first thing FileWriter does is truncate the file. I have seen examples where the reader still manages to read something but it is corrupted.
You have to write to a temporary file, close both files and when finished replace (using delete and rename) your original file with the temporary one.
Currently I am trying something very simple. I am looking through an XML document for a certain phrase upon which I try to replace it. The problem I am having is that when I read the lines I store each line into a StringBuffer. When I write the it to a document everything is written on a single line.
Here my code:
File xmlFile = new File("abc.xml")
BufferedReader br = new BufferedReader(new FileReade(xmlFile));
String line = null;
while((line = br.readLine())!= null)
{
if(line.indexOf("abc") != -1)
{
line = line.replaceAll("abc","xyz");
}
sb.append(line);
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter(xmlFile));
bw.write(sb.toString());
bw.close();
I am assuming I need a new line character when I prefer sb.append but unfortunately I don't know which character to use as "\n" does not work.
Thanks in advance!
P.S. I figured there must be a way to use Xalan to format the XML file after I write to it or something. Not sure how to do that though.
The readline reads everything between the newline characters so when you write back out, obviously the newline characters are missing. These characters depend on the OS: windows uses two characters to do a newline, unix uses one for example. To be OS agnostic, retrieve the system property "line.separator":
String newline = System.getProperty("line.separator");
and append it to your stringbuffer:
sb.append(line).append(newline);
Modified as suggested by Brel, your text-substituting approach should work, and it will work well enough for simple applications.
If things start to get a little hairier, and you end up wanting to select elements based on their position in the XML structure, and if you need to be sure to change element text but not tag text (think <abc>abc</abc>), then you'll want to call in in the cavalry and process the XML with an XML parser.
Essentially you read in a Document using a DocuemntBuilder, you hop around the document's nodes doing whatever you need to, and then ask the Document to write itself back to file. Or do you ask the parser? Anyway, most XML parsers have a handful of options that let you format the XML output: You can specify indentation (or not) and maybe newlines for every opening tag, that kinda thing, to make your XML look pretty.
Sb would be the StringBuffer object, which has not been instantiated in this example. This can added before the while loop:
StringBuffer sb = new StringBuffer();
Scanner scan = new Scanner(System.in);
String filePath = scan.next();
String oldString = "old_string";
String newString = "new_string";
String oldContent = "";
BufferedReader br = null;
FileWriter writer = null;
File xmlFile = new File(filePath);
try {
br = new BufferedReader(new FileReader(xmlFile));
String line = br.readLine();
while (line != null) {
oldContent = oldContent + line + System.lineSeparator();
line = br.readLine();
}
String newContent = oldContent.replaceAll(oldString, newString);
writer = new FileWriter(xmlFile);
writer.write(newContent);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
scan.close();
br.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}