Set encoding as UTF-8 for FileWriter

Below is my code, it should take two .ckl files, compare them, add new elements and create a new merged file. The program runs correctly when run on Netbeans, however when executing .jar the program does not seem to encode the file in UTF-8. I am new to programming and would like to know where and how I might need to provide this encoding?

** I removed the Swing code and other lines so that only my method is shown, a method that does all the comparison and merging.

public void mergeFiles(File[] files, File mergedFile) { ArrayList<String> list = new ArrayList<String>(); FileWriter fstream = null; BufferedWriter out = null; try { fstream = new FileWriter(mergedFile, false); out = new BufferedWriter(fstream); } catch (IOException e1) { e1.printStackTrace(); } // Going in a different direction. We are using a couple booleans to tell us when we want to copy or not. So at the beginning since we start // with our source file we set copy to true, we want to copy everything and insert vuln names into our list as we go. After that first file // we set the boolean to false so that we dont start copying anything from the second file until it is a vuln. We set to true when we see vuln // and set it to false if we already have that in our list. // We have a tmpCopy to store away the value of copy when we see a vuln, and reset it to that value when we see an </VULN> Boolean copy = true; Boolean tmpCopy = true; for (File f : files) { textArea1.append("merging files into: " + mergedFilePathway + "\n"); FileInputStream fis; try { fis = new FileInputStream(f); // BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(mergedFile), "UTF-8")); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String aLine; while ((aLine = in.readLine()) != null) { // Skip the close checklist and we can write it in at the end if (aLine.trim().equals("</iSTIG>")) { continue; } if (aLine.trim().equals("</STIGS>")) { continue; } if (aLine.trim().equals("</CHECKLIST>")) { continue; } if (aLine.trim().equals("<VULN>")) { // Store our current value of copy tmpCopy = copy; copy = true; String aLine2 = in.readLine(); String aLine3 = in.readLine(); String nameLine = in.readLine(); if (list.contains(nameLine.trim())) { textArea1.append("Skipping: " + nameLine + "\n"); copy = false; while (!(aLine.trim().equals("</VULN>"))) { aLine = in.readLine(); } continue; // this would skip the writing out to file part } else { list.add(nameLine.trim()); textArea1.append("::: List is now :::"); textArea1.append(list.toString() + "\n"); } if (copy) { out.write(aLine); out.newLine(); out.write(aLine2); out.newLine(); out.write(aLine3); out.newLine(); out.write(nameLine); out.newLine(); } } else if (copy) { out.write(aLine); out.newLine(); } // after we have written to file, if the line was a close vuln, switch copy back to original value if (aLine.trim().equals("</VULN>")) { copy = tmpCopy; } } in.close(); } catch (IOException e) { e.printStackTrace(); } copy = false; } // Now lets add the close checklist tag we omitted before try { out.write("</iSTIG>"); out.write("</STIGS>"); out.write("</CHECKLIST>"); } catch (IOException e) { e.printStackTrace(); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } } 
+5
source share
4 answers

Java has extensive, highly informative documentation . Bookmark it. Contact him first when you have difficulty. You will find it often useful.

In this case, the documentation for FileWriter reads:

The constructors of this class assume that the default character encoding and byte byte size are acceptable by default. To determine these values โ€‹โ€‹yourself, create an OutputStreamWriter in FileOutputStream.

If you want to be sure that your file will be written as UTF-8, replace this:

 FileWriter fstream = null; BufferedWriter out = null; try { fstream = new FileWriter(mergedFile, false); 

with this:

 Writer fstream = null; BufferedWriter out = null; try { fstream = new OutputStreamWriter(new FileOutputStream(mergedFile), StandardCharsets.UTF_8); 
+17
source

For those who use FileWriter to add to an existing file, it will work

 try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8)) { //code } 
+2
source

You can simply start it with the command java -Dfile.encoding=UTF-8 -jar yourjar.jar .

Follow this for more information.

0
source

Here is a good example on how to create a BufferWriter with an OutputStream that sets the UTF encoding.

0
source

Source: https://habr.com/ru/post/1242001/


All Articles