将编码设置为FileWriter的UTF-8

New*_*his 15 java encoding utf-8

下面是我的代码,它打算带两个.ckl文件,比较两个,添加新项目并创建一个新的合并文件.程序在Netbeans中运行时正确执行,但是,当执行.jar时,程序似乎没有以UTF-8编码文件.我对编程很陌生,想知道我可能需要在哪里或如何强制执行这种编码?

**我已经删除了Swing代码和其他行,因此只显示了我的方法,即执行所有比较和合并的方法.

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();
    }

}                                        
Run Code Online (Sandbox Code Playgroud)

VGR*_*VGR 31

Java拥有丰富,信息量很大的文档.保持书签状态.每当遇到困难时,请先参考它.你会发现它经常有用.

在这种情况下,FileWriter文档说:

此类的构造函数假定默认字符编码和默认字节缓冲区大小是可接受的.要自己指定这些值,请在FileOutputStream上构造OutputStreamWriter.

如果您想确保您的文件将被写为UTF-8,请替换为:

FileWriter fstream = null;
BufferedWriter out = null;
try {
    fstream = new FileWriter(mergedFile, false);
Run Code Online (Sandbox Code Playgroud)

有了这个:

Writer fstream = null;
BufferedWriter out = null;
try {
    fstream = new OutputStreamWriter(new FileOutputStream(mergedFile), StandardCharsets.UTF_8);
Run Code Online (Sandbox Code Playgroud)


小智 9

对于那些使用FileWriter以便附加到现有文件的人,以下内容将起作用

try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8)) {

    //code

}
Run Code Online (Sandbox Code Playgroud)

  • 应该避免这种构造,而应使用`try(FileOutputStream fos = ...; OutputStreamWriter osw = ...(fos); BufferWriter bw = new BufferedWriter(osw)){`。否则,如果后续执行的构造函数中发生异常,则中间对象将不会在其上调用“ close()”。例如,如果“ BufferedWriter()”具有异常(在这种情况下极不可能发生),则“ OutputStreamWriter”和“ FileOutputStream”实例将不会调用“ close()”。请参阅[Java 8 JLS](https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.20.3.1)。 (2认同)

Blu*_*n93 8

您可以使用命令运行它java -Dfile.encoding=UTF-8 -jar yourjar.jar

按照了解更多信息。