try {
//以GBK读入
File fileName = new File("E:\\工作内容\\纯真ip数据\\ip\\纯真ip.txt");
FileInputStream fr = new FileInputStream(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(fr, "GBK"));
//以UTF-8写入
File wfileName = new File("E:\\工作内容\\纯真ip数据\\ip\\数据中心\\数据中心.txt");
FileOutputStream fw = new FileOutputStream (wfileName);
OutputStreamWriter pw = new OutputStreamWriter (fw, "UTF-8");
String line = "";
while ((line = br.readLine()) != null) {
if (line.contains("数据中心")) {
System.out.println("写入一条 : " + line);
pw.write(line+"\r\n");
pw.flush();
}
}
br.close();
fr.close();
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
java 文件编码GBK转UTF-8
最新推荐文章于 2025-09-10 15:48:18 发布
本文介绍了一种从GBK编码读取文件并将其内容转换为UTF-8编码的Java程序实现方法。该程序通过读取指定路径下的文本文件,筛选包含特定关键字“数据中心”的行,并将这些行写入新的UTF-8编码文件中。
4927

被折叠的 条评论
为什么被折叠?



