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