java FileReader/FileWriter读写字母和数字没问题,但读写汉字就乱码。记录下,后面找到解决方法再补上。

public static void main(String[] args) {

FileReader fr = null;

FileWriter fw = null;

try {

int a = 0;

fr = new FileReader("c:/a.txt");//读取目标

fw = new FileWriter("c:/b.txt");//写入目标

while((a=fr.read())!=-1){

System.out.print((char)a+" ");//控制台显示所读取的文件

fw.write((char)a);//每读取一次就写入一次

}

fw.flush();//一次刷新将所读取到的文件直接刷新到本地目标文件

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

finally{

try {

fw.close();//最后记得关闭资源

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

相关文章:

  • 2022-12-23
  • 2021-10-03
  • 2022-01-29
  • 2022-12-23
  • 2021-11-23
  • 2021-09-20
  • 2022-01-16
猜你喜欢
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
相关资源
相似解决方案