filewriter 设置编码_将FileWriter的编码设置为UTF-8

下面是我的代码,它打算获取两个.ckl文件,比较两个文件,添加新项目并创建一个新的合并文件。该程序在Netbeans中运行时可以正确执行,但是,当执行.jar时,该程序似乎未使用UTF-8编码文件。我对编程很陌生,想知道在什么地方或如何执行这种编码?

**我删除了Swing代码和其他行,以便仅显示我的方法,该方法完成所有比较和合并。

public void mergeFiles(File[] files, File mergedFile) {

ArrayList list = new ArrayList();

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

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("")) {

continue;

}

if (aLine.trim().equals("")) {

continue;

}

if (aLine.trim().equals("")) {

continue;

}

if (aLine.trim().equals("")) {

// 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(""))) {

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("")) {

copy = tmpCopy;

}

}

in.close();

} catch (IOException e) {

e.printStackTrace();

}

copy = false;

}

// Now lets add the close checklist tag we omitted before

try {

out.write("");

out.write("");

out.write("");

} catch (IOException e) {

e.printStackTrace();

}

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值