日期:2014-05-20 浏览次数:21160 次
public static void main(String args[])
    {
        byte buffer[] = new byte[2056];
        try
        {
            FileInputStream fileint = new FileInputStream("dchs.txt");
            int bytes = fileint.read(buffer,0,2056);
            String str = new String(buffer,0,0,bytes);
            System.out.print(str);
        }//try
        catch(Exception e)
        {
            String err = e.toString();
            System.out.println(err);
        }//cathch
    }//public static void main(String args[])
public class ReadFileDemo {
    public static void main(String args[])
    {
        byte buffer[] = new byte[2056];
        try
        {
            FileInputStream fileint = new FileInputStream("易中天拼三国.txt");
            int i=0;
            while(fileint.read(buffer)!=-1){
                String str=new String(buffer);
                System.out.println(str);
            }
        }//try
        catch(Exception e)
        {
            e.printStackTrace();
            String err = e.toString();
            System.out.println(err);
        }//cathch
    }
}
------解决方案--------------------
这个好说 用FileReader。 FileReader 专门用来读取一些纯文本的。 FileInputstream 最底层的一个字节一个字节的读,适合读媒体文件比如图片视频
------解决方案--------------------
非要用FileInputStream读 可以用转换流 InputStreamReader 封起来 在用个BufferedReader封起来就好多了
------解决方案--------------------
用FileReader。 FileReader 专门用来读取一些纯文本的。 FileInputstream 最底层的一个字节一个字节的读,适合读媒体文件比如图片视频
------解决方案--------------------
我用下面的方法试了下能读中文。
File file=new File("C:\\dchs.txt");
        char[] ch=new char[(int)file.length()];
        int len=0;
        FileReader fr;
        try {
            fr = new FileReader(file);
            len=fr.read(ch);
            System.out.println(new String(ch,0,len));
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }
------解决方案--------------------