90. if(tempchars[i] == ’/r’){
91. continue;
92. }else{
93. System。
out.print(tempchars[i]);
94. }
95. }
96. }
97. }
98.
99. } catch (Exception e1) {
100. e1。
printStackTrace();
101. }finally {
102. if (reader !
= null){
103. try {
104. reader.close();
105. } catch (IOException e1) {
106. }
107. }
108. }
109.}
110./**
111. * 以行为单位读取文件,常用于读面向行的格式化文件
112. * @param fileName 文件名
113. */
114.public static void readFileByLines(String fileName){
115. File file = new File(fileName);
116. BufferedReader reader = null;
117. try {
118. System.out.println("以行为单位读取文件内容,一次读一整行:
");
119. reader = new BufferedReader(new FileReader(file));
120. String tempString = null;
121. int line = 1;
122. //一次读入一行,直到读入null为文件结束
123. while ((tempString = reader.readLine()) !
= null){
124. //显示行号
125. System。
out。
println("line ” + line + ":
” + tempString);
126. line++;
127. }
128. reader.close();
129. } catch (IOException e) {
130. e。
printStackTrace();
131. } finally {
132. if (reader !
= null){
133. try {
134. reader.close();
135. } catch (IOException e1) {
136. }
137. }
138. }
139.}
140./**
141. * 随机读取文件内容
142. * @param fileName 文件名
143. */
144.public static void readFileByRandomAccess(String fileName){
145. RandomAccessFile randomFile = null;
146. try {
147. System.out。
println("随机读取一段文件内容:
”);
148. // 打开一个随机访问文件流,按只读方式
149. randomFile = new RandomAccessFile(fileName, ”r”);
150. // 文件长度,字节数
151. long fileLength = randomFile.length();
152. // 读文件的起始位置
153. int beginIndex = (fileLength 〉 4) ?
4 :
0;
154. //将读文件的开始位置移到beginIndex位置.
155. randomFile.seek(beginIndex);
156. byte[] bytes = new byte[10];
157. int byteread = 0;
158. //一次读10个字节,如果文件内容不足10个字节,则读剩下的字节.
159. //将一次读取的字节数赋给byteread
160. while ((byteread = randomFile。
read(bytes)) !
= -1){
161. System.out。
write(bytes, 0, byteread);
162. }
163. } catch (IOException e){
164. e。
printStackTrace();
165. } finally {
166. if (randomFile !
= null){
167. try {
168. randomFile.close();
169. } catch (IOException e1) {
170. }
171. }
172. }
173.}
174./**
175. * 显示输入流中还剩的字节数
176. * @param in
177. */
178.private static void showAvailableBytes(InputStream in){
179. try {
180. System.out。
println(”当前字节输入流中的字节数为:
" + in.available());
181. } catch (IOException e) {
182. e。
printStackTrace();
183. }
184.}
185.
186.public static void main(String[] args) {
187. String fileName = "C:
/temp/newTemp.txt”;
188. ReadFromFile。
readFileByBytes(fileName);
189. ReadFromFile.readFileByChars(fileName);
190. ReadFromFile。
readFileByLines(fileName);
191. ReadFromFile.readFileByRandomAccess(fileName);
192.}
193.}
194.
195.
196.
197.二、将内容追加到文件尾部
198.
199.import java.io.FileWriter;
200.import java。
io.IOException;
201.import java。
io.RandomAccessFile;
202.
203./**
204.* 将内容追加到文件尾部
205.*/
206.public class AppendToFile {
207.
208./**
209. * A方法追加文件:
使用RandomAccessFile
210. * @param fileName 文件名
211. * @param content 追加的内容
212. */
213.public static void appendMethodA(String fileName, String content){
214. try {
215. // 打开一个随机访问文件流,按读写方式
216. RandomAccessFile randomFile = new RandomAccessFile(fileName, ”rw”);
217. // 文件长度,字节数
218. long fileLength = randomFile.length();
219. //将写文件指针移到文件尾。
220. randomFile。
seek(fileLength);
221. randomFile.writeBytes(content);
222. randomFile.close();
223. } catch (IOException e){
224. e。
printStackTrace();
225. }
226.}
227./**
228. * B方法追加文件:
使用FileWriter
229. * @param fileName
230. * @param content
231. */
232.public static void appendMethodB(String fileName, String content){
233. try {
234. //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
235. FileWriter writer = new FileWriter(fileName, true);
236. writer.write(content);
237. writer.close();
238. } catch (IOException e) {
239. e.printStackTrace();
240. }
241.}
242.
243.public static void main(String[] args) {
244. String fileName = "C:
/temp/newTemp。
txt”;
245. String content = ”new append!
”;
246. //按方法A追加文件
247. AppendToFile。
appendMethodA(fileName, content);
248. AppendToFile.appendMethodA(fileName, "append end. /n”);
249. //显示文件内容
250. ReadFromFile.readFileByLines(fileName);
251. //按方法B追加文件
252. AppendToFile。
appendMethodB(fileName, content);
253. AppendToFile.appendMethodB(fileName, "append end. /n”);
254. //显示文件内容
255. ReadFromFile。
readFileByLines(fileName);
256.}
257.}
258.
259.三文件的各种操作类
260.
261.import java。
io.*;
262.
263./**
264.* FileOperate。
java
265.* 文件的各种操作
266.* @author 杨彩 http:
//。
cn/m/yangcai
267.* 文件操作 1.0
268.*/
269.
270.public class FileOperate
271.{
272.
273.public FileOperate()
274.{
275.}
276./**
277.* 新建目录
278.*/
279.public void newFolder(String folderPath)
280.{
281.try
282.{
283.String filePath = folderPath;
284.filePath = filePath。
toString();
285.File myFilePath = new File(filePath);
286.if(!
myFilePath。
exists())
287.{
288.myFilePath。
mkdir();
289.}
290.System。
out.println("新建目录操作 成功执行");
291.}
292.catch(Exception e)
293.{
294.System.out.println("新建目录操作出错”);
295.e。
printStackTrace();
296.}
297.}
298./**
299.* 新建文件
300.*/
301.public void newFile(String filePathAndName, String fileContent)
302.{
303.try
304.{
305.String filePath = filePathAndName;
306.filePath = filePath.toString();
307.File myFilePath = new File(filePath);
308.if (!
myFilePath。
exists())
309.{
310.myFilePath.createNewFile();
311.}
312.FileWriter resultFile = new FileWriter(myFilePath);
313.PrintWriter myFile = new PrintWriter(resultFile);
314.String strContent = fileContent;
315.myFile.println(strContent);
316.resultFile.close();
317.System。
out.println("新建文件操作 成功执行");
318.}
319.catch (Exception e)
320.{
321.System.out。
println(”新建目录操作出错”);
322.e。
printStackTrace();
323.}
324.}
325./**
326.* 删除文件
327.*/
328.public void delFile(String filePathAndName)
329.{
330.try
331.{
332.String filePath = filePathAndName;
333.filePath = filePath。
toString();
334.File myDelFile = new File(filePath);
335.myDelFile.delete();
336.System。
out.println(”删除文件操作 成功执行");
337.}
338.catch (Exception e)
339.{
340.System。
out.println("删除文件操作出错”);
341.e.printStackTr