相关文章推荐
年轻有为的滑板  ·  关于解决 错误: 找不到或无法加载主类 ...·  1 年前    · 
乐观的蚂蚁  ·  kaggle实战—泰坦尼克(五、模型搭建-模 ...·  2 年前    · 
淡定的茶壶  ·  ubuntu 18.04 ...·  2 年前    · 
飘逸的企鹅  ·  Process finished with ...·  2 年前    · 
Code  ›  java - 为 FileWriter 设置编码为 UTF-8 -
fstream
https://segmentfault.com/q/1010000042886708
道上混的西红柿
2 年前
segmentfault segmentfault
注册登录
问答 博客 标签 活动
发现
✓ 使用“Bing”搜本站 使用“Google”搜本站 使用“百度”搜本站 站内搜索
注册登录
  1. 首页
  2. 问答
  3. Stack Overflow 翻译
  4. 问答详情

为 FileWriter 设置编码为 UTF-8

社区维基
1
发布于
2022-11-24
新手上路,请多包涵

下面是我的代码,它打算获取两个 .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();

原文由 NewToThis 发布,翻译遵循 CC BY-SA 4.0 许可协议

Stack Overflow 翻译 java encoding utf-8
阅读 683
2 个回答
得票 最新
社区维基
1
发布于
2022-11-24
✓ 已被采纳

Java 拥有内容 丰富、信息量大的文档 。保留它的书签。遇到困难时先参考一下。您会发现它经常有用。

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

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

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

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

有了这个:

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

原文由 VGR 发布,翻译遵循 CC BY-SA 3.0 许可协议

社区维基
1
发布于
2022-11-24

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

try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8)) {
    //code
 
推荐文章
年轻有为的滑板  ·  关于解决 错误: 找不到或无法加载主类 原因: java.lang.ClassNotFoundException 的方法_找不到或无法加载主类 org.dromara.dromaraapplicati
1 年前
乐观的蚂蚁  ·  kaggle实战—泰坦尼克(五、模型搭建-模型评估)_kaggle 模型评价-CSDN博客
2 年前
淡定的茶壶  ·  ubuntu 18.04 安装python包_ubuntu1804 安装python_JY HUA的博客-CSDN博客
2 年前
飘逸的企鹅  ·  Process finished with exit code -1073741819 (0xC0000005) C++ clion - Stack Overflow
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号