如果这个文本文件是汉字的,代码如下: 
import java.io.*; 
public class FileIn { 
        
    /** 
    * Creates a new instance of <code>FileIn </code>. 
    */ 
    public FileIn() { 
    } 
    
    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
        // TODO code application logic here 
        try 
        {FileInputStream rf=new FileInputStream("d:\\ls.txt"); 
        int b; 
        while((b=rf.read())!=-1) 
        System.out.print((char)b); 
        rf.close(); 
        } 
        catch(IOException ie) 
        {System.out.println(ie); 
        } 
    } 

此文本文件是D盘ls文件夹下的ls.txt,结果运行时不能显示汉字,请问如何显示汉字。

解决方案 »

  1.   

    public class FileInputStreamextends InputStreamFileInputStream 从文件系统中的某个文件中获得输入字节。哪些文件可用取决于主机环境。 FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。
    启发性的代码 import java.io.*; 
    public class testFile {     /** 
        * Creates a new instance of <code>FileIn </code>. 
        */ 
        public testFile() { 
        } 
        
        /** 
        * @param args the command line arguments 
        */ 
        public static void main(String[] args) { 
            // TODO code application logic here 
            try 
            {
             //FileInputStream rf=new FileInputStream("d:\\ls.txt");  
             //这个是字节流用,一般不用来读文本文件
            
             FileReader fr = new FileReader("d:\\ls.txt");
             BufferedReader br = new BufferedReader(fr); 
            
             String temp;
             while((temp=br.readLine())!=null){ 
             System.out.println(temp); 
        
             } 
             br.close();
            }catch(IOException ie) 
             {
             System.out.println(ie); 
             }finally{
            
             } 
        }
    }
      

  2.   

    用字符流就是了,这时候不用什么时候用啊,比如BufferedReader直接readLine返回一个String
    字节流的话就练你的字符编码了,记住一点就不会有乱码,就是写的时候和读的时候用一种字符编码
      

  3.   

    直接给代码你看吧import java.awt.*;
    import java.awt.event.*;
    import java.io.FileReader;import javax.swing.*;public class openfile extends JFrame{
    JButton b1=new JButton("选择文件");
    JTextField t1=new JTextField(20);
    JTextArea result=new JTextArea();
    JButton b2=new JButton("打开");
    FileReader myfile;
    Font zi=new Font("宋体",Font.TYPE1_FONT,15);
    public openfile() {

     setTitle("文件打开演示");
     setLayout(null);
     setSize(700, 600);
     setResizable(false);
     this.setBackground(Color.DARK_GRAY);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
     Dimension frm = this.getSize();
     setLocation( (scr.width - frm.width) / 2,
             (scr.height - frm.height) / 2 );

     b1.setBounds(50, 30, 100, 25);
     t1.setBounds(180,30 , 180, 25);
     JScrollPane s1 = new JScrollPane(result);
     result.setBackground(Color.lightGray);
     result.setFont(zi);
     s1.setBounds(20, 100, 650, 450);
     b2.setBounds(400, 30, 80, 25);

        add(b1);
    add(t1);
    add(s1);
    add(b2);
     b1.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 FileDialog my=new FileDialog(openfile.this);
                 my.setVisible(true);
                 t1.setText(my.getDirectory()+my.getFile());
                }
            }); 
     b2.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
             try{
                
             myfile=new FileReader(t1.getText());
             int show=myfile.read();
             while(show!=-1){
             result.append((char)show+"");
             show=myfile.read();
             }
            
            
            
             }
             catch(Exception ee){} 
          
                }
            }); 
     
     
     
     
     
     
     
     
     setVisible(true);
     
     
     
    }
    public static void main(String[] args){
    new openfile();

    }