读取普通文件

2017-11-08  本文已影响0人  聪哥堂

1、读取普通的文件

public static void main(String[] args) throws IOException {

                    File src = new File("D:/KFO/parent/1122.txt");

                    InputStream is = null;

                    try {

                            //选择流

                                is = new FileInputStream(src);

                                //读取,定义一个缓冲数组

                                byte[] car = new byte[1024];

                                //定义实际读取的长度

                                int len = 0;

                                while((len = is.read(car)) != -1){

                                //再将字节数组转化成为字符串

                                String info = new String(car,0,len);

                                System.out.println(info);

                    }

        } catch (FileNotFoundException e) {

                e.printStackTrace();

}finally{

                    if(is != null){

                is.close();

            }

        }

}

上一篇 下一篇

猜你喜欢

热点阅读