首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

字符流的有关问题

2011-12-29 
字符流的问题public class ShuHeanl implements ActionListener {Frame1 fFile ff new File(E:\\java\

字符流的问题
public class ShuHeanl implements ActionListener {
  Frame1 f;
  File ff = new File("E:\\java\\e.txt");
  public void actionPerformed(ActionEvent e) {
  String str=new String();
  if (e.getSource() == f.jButton1) {
  try {
  FileReader rf = new FileReader(ff);
  BufferedReader br = new BufferedReader(rf);
  while((str=br.readLine())!=null){
  f.jTextArea1.append(str+'\n');
  System.out.print(str);
  }
  br.close();
  rf.close();
  } catch (IOException ex) {}
  }
  }


  我用的是字符流 应该是不会出现截断流的,点击jButton1时把E:\\java\\e.txt的文件内容传到jTextArea1中 但问题是每个英文字母前有一个筐
 效果 :?覻?覻 文件为中文时出现在jTextArea1中的是一些不认识的字


[解决办法]
读取文件时也要根据文件编码进行读取的。

Java code
    import java.io.*;    import java.nio.charset.Charset;    String charset = "UTF-8";    File file = new File(filePath);    Reader reader = new InputStreamReader(new BufferedInputStream(new FileInputStream(file)), Charset.forName(charset));    .... 

热点排行