java.关于写入文件,文字编码问题!
importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.FileReader;im...
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class Test {
public static void main(String[] args) throws Exception {
String id = "0001";
String text = "hhh";
String line = null;
BufferedReader br = new BufferedReader(new FileReader("c:/content1.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("c:/content2.txt"));
while((line = br.readLine()) != null) {
if(line.contains(id))
line = id + " " + text;
bw.write(line);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
}
}
写入的字体都是 ????? 这样的
估计是因为 写入格式 不是 utf-8
怎么设置 写入格式为 utf-8 展开
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class Test {
public static void main(String[] args) throws Exception {
String id = "0001";
String text = "hhh";
String line = null;
BufferedReader br = new BufferedReader(new FileReader("c:/content1.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("c:/content2.txt"));
while((line = br.readLine()) != null) {
if(line.contains(id))
line = id + " " + text;
bw.write(line);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
}
}
写入的字体都是 ????? 这样的
估计是因为 写入格式 不是 utf-8
怎么设置 写入格式为 utf-8 展开
展开全部
package p1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Test
{
public static void main ( String[] args ) throws Exception
{
String id = "0001";
String text = "hhh";
String line = null;
// 自行调整charset即可
BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream ("c:/content1.txt"), "GBK"));
BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream ("c:/content1.txt"), "UTF-8"));
while (( line = br.readLine () ) != null)
{
if (line.contains (id))
line = id + " " + text;
bw.write (line);
bw.newLine ();
}
bw.flush ();
bw.close ();
br.close ();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询