您的位置:首页 > 其它

从txt中读出文本内容(博文专用)

2014-02-24 18:39 183 查看
方法1:定义txt内容同等大小的数组,一次性读出文本的所有内容。

File file = new File("D:\\xx.txt");

Long fileLengthLong = file.length();

byte[] fileContent = new byte[fileLengthLong.intValue()];

try {

        FileInputStream inputStream = new FileInputStream(file);

        inputStream.read(fileContent);

 inputStream.close();

} catch (Exception e) {

 // TODO: handle exception

}

String string = new String(fileContent);

方法2:按行循环读取文本内容.

BufferedReader in = new BufferedReader(new FileReader("D:\\xx.txt"));

String line = null;

while((line = in.readLine())!=null)

{

  System.out.println(line);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  文件读写 读txt