由于最近在做一个关于从手机本地读取格式化的txt文件中的内容,并且把内容放在listview中显示。这样问题来了,就是如何能够遍历已经获取到特定的map中就是一个问题,在网上找了一些资料,找到了一个很全面的java遍历文件的文章,在这里转载引用。
文章参考:http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html
(一)按字节读取文件内容:
public classReadFromFile {/*** 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。*/
public static voidreadFileByBytes(String fileName) {
File file= newFile(fileName);
InputStream in= null;try{
System.out.println("以字节为单位读取文件内容,一次读一个字节:");//一次读一个字节
in = newFileInputStream(file);inttempbyte;while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
}catch(IOException e) {
e.printStackTrace();return;
}try{
System.out.println("以字节为单位读取文件内容,一次读多个字节:");//一次读多个字节
byte[] tempbytes = new byte[100];int byteread = 0;
in= newFileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);//读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write