1 importjava.io.BufferedReader;2 importjava.io.File;3 importjava.io.FileInputStream;4 importjava.io.FileReader;5 importjava.io.IOException;6 importjava.io.InputStream;7 importjava.io.InputStreamReader;8 importjava.io.RandomAccessFile;9 importjava.io.Reader;10
11 public classReadFromFileUtil {12
13 /**
14 *@paramargs15 */
16 public static voidmain(String[] args) {17 String fileName = "D://output.txt";18 readFileByBytes(fileName);19 readFileByChars(fileName);20 readFileByLines(fileName);21 readFileByRandomAccess(fileName);22 }23
24 /**
25 * 随机读取文件内容26 */
27 public static voidreadFileByRandomAccess(String fileName) {28 RandomAccessFile randomFile = null;29 try{30 System.out.println("随机读取一段文件内容:");31 //打开一个随机访问文件流,按只读方式
32 randomFile = new RandomAccessFile(fileName, "r");33 //文件长度,字节数
34 long fileLength =randomFile.length();35 //读文件的起始位置
36 int beginIndex = (fileLength > 4) ? 0 : 0;37 //将读文件的开始位置移到beginIndex位置。
38 randomFile.seek(beginIndex);39 byte[] bytes = new byte[10];40 int byteread = 0;41 //一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。42 //将一次读取的字节数赋给byteread
43 while ((byteread = randomFile.read(bytes)) != -1) {44 System.out.write(bytes, 0, byteread);45 }46 } catch(IOException e) {47 e.printStackTrace();48 } finally{49 if (randomFile != null) {50 try{51 randomFile.close();52 } catch(IOException e1) {53 }54 }55 }56 }57 /**
58 * 以行为单位读取文件,常用于读面向行的格式化文件59 */
60 public static voidreadFileByLines(String fileName) {61 File file = newFile(fileName);62 BufferedReader reader = null;63 try{64 System.out.println("以行为单位读取文件内容,一次读一整行:");65 reader = new BufferedReader(newFileReader(file));66 String tempString = null;67 int line = 1;68 //一次读入一行,直到读入null为文件结束
69 while ((tempString = reader.readLine()) != null) {70 //显示行号
71 System.out.println("line " + line + ": " +tempString);72 line++;73 }74 reader.close();75 } catch(IOException e) {76 e.printStackTrace();77 } finally{78 if (reader != null) {79 try{80 reader.close();81 } catch(IOException e1) {82 }83 }84 }85 }86
87 /**
88 * 以字符为单位读取文件,常用于读文本,数字等类型的文件89 */
90 public static voidreadFileByChars(String fileName) {91 File file = newFile(fileName);92 Reader reader = null;93 try{94 System.out.println("以字符为单位读取文件内容,一次读一个字节:");95 //一次读一个字符
96 reader = new InputStreamReader(newFileInputStream(file));97 inttempchar;98 while ((tempchar = reader.read()) != -1) {99 //对于windows下,\r\n这两个字符在一起时,表示一个换行。100 //但如果这两个字符分开显示时,会换两次行。101 //因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
102 if (((char) tempchar) != '\r') {103 System.out.print((char) tempchar);104 }105 }106 reader.close();107 } catch(Exception e) {108 e.printStackTrace();109 }110 try{111 System.out.println("\n以字符为单位读取文件内容,一次读多个字节:");112 //一次读多个字符
113 char[] tempchars = new char[30];114 int charread = 0;115 reader = new InputStreamReader(newFileInputStream(fileName));116 //读入多个字符到字符数组中,charread为一次读取字符数
117 while ((charread = reader.read(tempchars)) != -1) {118 //同样屏蔽掉\r不显示
119 if ((charread ==tempchars.length)120 && (tempchars[tempchars.length - 1] != '\r')) {121 System.out.print(tempchars);122 } else{123 for (int i = 0; i < charread; i++) {124 if (tempchars[i] == '\r') {125 continue;126 } else{127 System.out.print(tempchars[i]);128 }129 }130 }131 }132
133 } catch(Exception e1) {134 e1.printStackTrace();135 } finally{136 if (reader != null) {137 try{138 reader.close();139 } catch(IOException e1) {140 }141 }142 }143 }144 /**
145 * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。146 */
147 public static voidreadFileByBytes(String fileName) {148 File file = newFile(fileName);149 InputStream in = null;150 try{151 System.out.println("以字节为单位读取文件内容,一次读一个字节:");152 //一次读一个字节
153 in = newFileInputStream(file);154 inttempbyte;155 while ((tempbyte = in.read())!=-1) {156 System.out.println(tempbyte);157 }158 } catch(Exception e) {159 e.printStackTrace();160 }161
162 try{163 System.out.println("以字节为单位读取文件内容,一次读多个字节:");164 //一次读多个字节
165 byte[] tempbytes = new byte[100];166 int byteread = 0;167 in = newFileInputStream(fileName);168 ReadFromFile.showAvailableBytes(in);169 //读入多个字节到字节数组中,byteread为一次读入的字节数
170 while ((byteread = in.read(tempbytes)) != -1) {171 System.out.write(tempbytes, 0, byteread);//好方法,第一个参数是数组,第二个参数是开始位置,第三个参数是长度
172 }173 } catch(Exception e1) {174 e1.printStackTrace();175 } finally{176 if (in != null) {177 try{178 in.close();179 } catch(IOException e1) {180 }181 }182 }183 }184
185 /**
186 * 显示输入流中还剩的字节数187 */
188 private static voidshowAvailableBytes(InputStream in) {189 try{190 System.out.println("当前字节输入流中的字节数为:" +in.available());191 } catch(IOException e) {192 e.printStackTrace();193 }194 }195
196 }