JavaIO操作读写追加删除移动复制修改.docx

上传人:b****6 文档编号:6080523 上传时间:2023-01-03 格式:DOCX 页数:22 大小:27.26KB
下载 相关 举报
JavaIO操作读写追加删除移动复制修改.docx_第1页
第1页 / 共22页
JavaIO操作读写追加删除移动复制修改.docx_第2页
第2页 / 共22页
JavaIO操作读写追加删除移动复制修改.docx_第3页
第3页 / 共22页
JavaIO操作读写追加删除移动复制修改.docx_第4页
第4页 / 共22页
JavaIO操作读写追加删除移动复制修改.docx_第5页
第5页 / 共22页
点击查看更多>>
下载资源
资源描述

JavaIO操作读写追加删除移动复制修改.docx

《JavaIO操作读写追加删除移动复制修改.docx》由会员分享,可在线阅读,更多相关《JavaIO操作读写追加删除移动复制修改.docx(22页珍藏版)》请在冰豆网上搜索。

JavaIO操作读写追加删除移动复制修改.docx

JavaIO操作读写追加删除移动复制修改

一、多种方式读文件内容。

 

1、按字节读取文件内容 

2、按字符读取文件内容 

3、按行读取文件内容 

4、随机读取文件内容 

Java代码  

1.import java。

io。

BufferedReader;  

2.import java.io.File;  

3.import java。

io。

FileInputStream;  

4.import java。

io.FileReader;  

5.import java。

io。

IOException;  

6.import java.io。

InputStream;  

7.import java.io。

InputStreamReader;  

8.import java.io。

RandomAccessFile;  

9.import java。

io。

Reader;   

10.public class ReadFromFile {  

11./** 

12.   * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。

 

13.   * @param fileName 文件的名 

14.   */  

15.public static void readFileByBytes(String fileName){  

16.   File file = new File(fileName);  

17.   InputStream in = null;  

18.   try {  

19.    System。

out.println(”以字节为单位读取文件内容,一次读一个字节:

”);  

20.    // 一次读一个字节  

21.    in = new FileInputStream(file);  

22.    int tempbyte;  

23.    while((tempbyte=in。

read()) !

= -1){  

24.     System。

out。

write(tempbyte);  

25.    }  

26.    in。

close();  

27.   } catch (IOException e) {  

28.    e.printStackTrace();  

29.    return;  

30.   }  

31.   try {  

32.    System。

out.println("以字节为单位读取文件内容,一次读多个字节:

");  

33.    //一次读多个字节  

34.    byte[] tempbytes = new byte[100];  

35.    int byteread = 0;  

36.    in = new FileInputStream(fileName);  

37.    ReadFromFile.showAvailableBytes(in);  

38.    //读入多个字节到字节数组中,byteread为一次读入的字节数  

39.    while ((byteread = in.read(tempbytes)) !

= —1){  

40.     System.out。

write(tempbytes, 0, byteread);  

41.    }  

42.   } catch (Exception e1) {  

43.    e1.printStackTrace();  

44.   } finally {  

45.    if (in !

= null){  

46.     try {  

47.      in。

close();  

48.     } catch (IOException e1) {  

49.     }  

50.    }  

51.   }  

52.}  

53./** 

54.   * 以字符为单位读取文件,常用于读文本,数字等类型的文件 

55.   * @param fileName 文件名 

56.   */  

57.public static void readFileByChars(String fileName){  

58.   File file = new File(fileName);  

59.   Reader reader = null;  

60.   try {  

61.    System。

out.println(”以字符为单位读取文件内容,一次读一个字节:

”);  

62.    // 一次读一个字符  

63.    reader = new InputStreamReader(new FileInputStream(file));  

64.    int tempchar;  

65.    while ((tempchar = reader.read()) !

= —1){  

66.     //对于windows下,/r/n这两个字符在一起时,表示一个换行。

  

67.     //但如果这两个字符分开显示时,会换两次行。

  

68.     //因此,屏蔽掉/r,或者屏蔽/n。

否则,将会多出很多空行。

  

69.     if (((char)tempchar) !

= ’/r'){  

70.      System.out。

print((char)tempchar);  

71.     }  

72.    }  

73.    reader.close();  

74.   } catch (Exception e) {  

75.    e.printStackTrace();  

76.   }  

77.   try {  

78.    System。

out。

println(”以字符为单位读取文件内容,一次读多个字节:

");  

79.    //一次读多个字符  

80.    char[] tempchars = new char[30];  

81.    int charread = 0;  

82.    reader = new InputStreamReader(new FileInputStream(fileName));  

83.    //读入多个字符到字符数组中,charread为一次读取字符数  

84.    while ((charread = reader.read(tempchars))!

=—1){  

85.     //同样屏蔽掉/r不显示  

86.     if ((charread == tempchars.length)&&(tempchars[tempchars。

length—1] !

= '/r’)){  

87.      System。

out.print(tempchars);  

88.     }else{  

89.      for (int i=0; i

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1