收藏 分享(赏)

Java_IO操作_(读写、追加、删除、移动、复制、修改).doc

上传人:hskm5268 文档编号:6620964 上传时间:2019-04-18 格式:DOC 页数:15 大小:52.93KB
下载 相关 举报
Java_IO操作_(读写、追加、删除、移动、复制、修改).doc_第1页
第1页 / 共15页
Java_IO操作_(读写、追加、删除、移动、复制、修改).doc_第2页
第2页 / 共15页
Java_IO操作_(读写、追加、删除、移动、复制、修改).doc_第3页
第3页 / 共15页
Java_IO操作_(读写、追加、删除、移动、复制、修改).doc_第4页
第4页 / 共15页
Java_IO操作_(读写、追加、删除、移动、复制、修改).doc_第5页
第5页 / 共15页
点击查看更多>>
资源描述

1、一、多种方式读文件内容。 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 j

2、ava.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.

3、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(“以字节为单位读

4、取文件内容,一次读多个字节:“); 33. /一次读多个字节 34. byte tempbytes = new byte100; 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); 4

5、1. 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

6、 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. /但如果这两个字符分开显示时,会换两

7、次行。 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 char30; 81. int c

8、harread = 0; 82. reader = new InputStreamReader(new FileInputStream(fileName); 83. /读入多个字符到字符数组中,charread 为一次读取字符数 84. while (charread = reader.read(tempchars)!=-1) 85. /同样屏蔽掉/r 不显示 86. if (charread = tempchars.length) 88. else 89. for (int i=0; i 4) ? 4 : 0; 154. /将读文件的开始位置移到 beginIndex 位置。 155. ra

9、ndomFile.seek(beginIndex); 156. byte bytes = new byte10; 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(

10、); 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.avail

11、able(); 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); 1

12、91. 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 方法追加文件

13、:使用 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 = r

14、andomFile.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 s

15、tatic 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 stat

16、ic 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

17、. /按方法 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.

18、* author 杨彩 http:/ 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 m

19、yFilePath = 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(Stri

20、ng 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(myFi

21、lePath); 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(); 32

22、3. 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. ca

23、tch (Exception e) 339. 340. System.out.println(“删除文件操作出错“); 341. e.printStackTrace(); 342. 343. 344. /* 345. * 删除文件夹 346. */ 347. public void delFolder(String folderPath) 348. 349. try 350. 351. delAllFile(folderPath); /删除完里面所有内容 352. String filePath = folderPath; 353. filePath = filePath.toString()

24、; 354. File myFilePath = new File(filePath); 355. if(myFilePath.delete() /删除空文件夹 356. System.out.println(“删除文件夹“ + folderPath + “操作 成功执行“); 357. else 358. System.out.println(“删除文件夹“ + folderPath + “操作 执行失败“); 359. 360. 361. catch (Exception e) 362. 363. System.out.println(“删除文件夹操作出错“); 364. e.printS

25、tackTrace(); 365. 366. 367. /* 368. * 删除文件夹里面的所有文件 369. * param path String 文件夹路径 如 c:/fqf 370. */ 371. public void delAllFile(String path) 372. 373. File file = new File(path); 374. if(!file.exists() 375. 376. return; 377. 378. if(!file.isDirectory() 379. 380. return; 381. 382. String tempList = fi

26、le.list(); 383. File temp = null; 384. for (int i = 0; i tempList.length; i+) 385. 386. if(path.endsWith(File.separator) 387. 388. temp = new File(path + tempListi); 389. 390. else 391. 392. temp = new File(path + File.separator + tempListi); 393. 394. if (temp.isFile() 395. 396. temp.delete(); 397.

27、 398. if (temp.isDirectory() 399. 400. /delAllFile(path+“/“+ tempListi);/先删除文件夹里面的文件 401. delFolder(path+ File.separatorChar + tempListi);/再删除空文件夹 402. 403. 404. System.out.println(“删除文件操作 成功执行“); 405. 406. /* 407. * 复制单个文件 408. * param oldPath String 原文件路径 如:c:/fqf.txt 409. * param newPath String 复

28、制后路径 如:f:/fqf.txt 410. */ 411. public void copyFile(String oldPath, String newPath) 412. 413. try 414. 415. int bytesum = 0; 416. int byteread = 0; 417. File oldfile = new File(oldPath); 418. if (oldfile.exists() 419. 420. /文件存在时 421. InputStream inStream = new FileInputStream(oldPath); /读入原文件 422.

29、FileOutputStream fs = new FileOutputStream(newPath); 423. byte buffer = new byte1444; 424. while ( (byteread = inStream.read(buffer) != -1) 425. 426. bytesum += byteread; /字节数 文件大小 427. System.out.println(bytesum); 428. fs.write(buffer, 0, byteread); 429. 430. inStream.close(); 431. 432. System.out.

30、println(“删除文件夹操作 成功执行“); 433. 434. catch (Exception e) 435. 436. System.out.println(“复制单个文件操作出错“); 437. e.printStackTrace(); 438. 439. 440. /* 441. * 复制整个文件夹内容 442. * param oldPath String 原文件路径 如:c:/fqf 443. * param newPath String 复制后路径 如:f:/fqf/ff 444. */ 445. public void copyFolder(String oldPath,

31、 String newPath) 446. 447. try 448. 449. (new File(newPath).mkdirs(); /如果文件夹不存在 则建立新文件夹 450. File a=new File(oldPath); 451. String file=a.list(); 452. File temp=null; 453. for (int i = 0; i file.length; i+) 454. 455. if(oldPath.endsWith(File.separator) 456. 457. temp=new File(oldPath+filei); 458. 45

32、9. else 460. 461. temp=new File(oldPath+File.separator+filei); 462. 463. if(temp.isFile() 464. 465. FileInputStream input = new FileInputStream(temp); 466. FileOutputStream output = new FileOutputStream(newPath + “/“ + 467. (temp.getName().toString(); 468. byte b = new byte1024 * 5; 469. int len; 47

33、0. while ( (len = input.read(b) != -1) 471. 472. output.write(b, 0, len); 473. 474. output.flush(); 475. output.close(); 476. input.close(); 477. 478. if(temp.isDirectory() 479. 480. /如果是子文件夹 481. copyFolder(oldPath+“/“+filei,newPath+“/“+filei); 482. 483. 484. System.out.println(“复制文件夹操作 成功执行“); 485

34、. 486. catch (Exception e) 487. 488. System.out.println(“复制整个文件夹内容操作出错“); 489. e.printStackTrace(); 490. 491. 492. /* 493. * 移动文件到指定目录 494. * param oldPath String 如:c:/fqf.txt 495. * param newPath String 如:d:/fqf.txt 496. */ 497. public void moveFile(String oldPath, String newPath) 498. 499. copyFil

35、e(oldPath, newPath); 500. delFile(oldPath); 501. 502. /* 503. * 移动文件到指定目录 504. * param oldPath String 如:c:/fqf.txt 505. * param newPath String 如:d:/fqf.txt 506. */ 507. public void moveFolder(String oldPath, String newPath) 508. 509. copyFolder(oldPath, newPath); 510. delFolder(oldPath); 511. 512. p

36、ublic static void main(String args) 513. 514. String aa,bb; 515. boolean exitnow=false; 516. System.out.println(“使用此功能请按1 功能一:新建目录“); 517. System.out.println(“使用此功能请按2 功能二:新建文件“); 518. System.out.println(“使用此功能请按3 功能三:删除文件“); 519. System.out.println(“使用此功能请按4 功能四:删除文件夹“); 520. System.out.println(“使用

37、此功能请按5 功能五:删除文件夹里面的所有文件“); 521. System.out.println(“使用此功能请按6 功能六:复制文件“); 522. System.out.println(“使用此功能请按7 功能七:复制文件夹的所有内容“); 523. System.out.println(“使用此功能请按8 功能八:移动文件到指定目录“); 524. System.out.println(“使用此功能请按9 功能九:移动文件夹到指定目录“); 525. System.out.println(“使用此功能请按10 退出程序“ ); 526. while(!exitnow) 527. 52

38、8. FileOperate fo=new FileOperate(); 529. try 530. 531. BufferedReader Bin=new BufferedReader(new InputStreamReader(System.in); 532. String a=Bin.readLine(); 533. int b=Integer.parseInt(a); 534. switch(b) 535. 536. case 1:System.out.println(“你选择了功能一 请输入目录名“); 537. aa=Bin.readLine(); 538. fo.newFolde

39、r(aa); 539. break; 540. case 2:System.out.println(“你选择了功能二 请输入文件名“); 541. aa=Bin.readLine(); 542. System.out.println(“请输入在“+aa+“ 中的内容“); 543. bb=Bin.readLine(); 544. fo.newFile(aa,bb); 545. break; 546. case 3:System.out.println(“你选择了功能三 请输入文件名“); 547. aa=Bin.readLine(); 548. fo.delFile(aa); 549. bre

40、ak; 550. case 4:System.out.println(“你选择了功能四 请输入文件名“); 551. aa=Bin.readLine(); 552. fo.delFolder(aa); 553. break; 554. case 5:System.out.println(“你选择了功能五 请输入文件名“); 555. aa=Bin.readLine(); 556. fo.delAllFile(aa); 557. break; 558. case 6:System.out.println(“你选择了功能六 请输入文件名“); 559. aa=Bin.readLine(); 560

41、. System.out.println(“请输入目标文件名“); 561. bb=Bin.readLine(); 562. fo.copyFile(aa,bb); 563. break; 564. case 7:System.out.println(“你选择了功能七 请输入源文件名“); 565. aa=Bin.readLine(); 566. System.out.println(“请输入目标文件名“); 567. bb=Bin.readLine(); 568. fo.copyFolder(aa,bb); 569. break; 570. case 8:System.out.println

42、(“你选择了功能八 请输入源文件名“); 571. aa=Bin.readLine(); 572. System.out.println(“请输入目标文件名“); 573. bb=Bin.readLine(); 574. fo.moveFile(aa,bb); 575. break; 576. case 9:System.out.println(“你选择了功能九 请输入源文件名“); 577. aa=Bin.readLine(); 578. System.out.println(“请输入目标文件名“); 579. bb=Bin.readLine(); 580. fo.moveFolder(aa

43、,bb); 581. break; 582. case 10:exitnow=true; 583. System.out.println(“程序结束,请退出“); 584. break; 585. default:System.out.println(“输入错误.请输入 1-10 之间的数“); 586. 587. System.out.println(“请重新选择功能“); 588. 589. catch(Exception e) 590. 591. System.out.println(“输入错误字符或程序出错“); 592. 593. 594. 595. 596. try597. Buf

44、feredInputStream bis = 598. new BufferedInputStream(new FileInputStream(file);599. byte buff = new byte(int)file.length();600. bis.read(buff);601. FileOutputStream fos = new FileOutputStream(file);602. String lines = (new String(buff).split(“n“);603. for(String line : lines)604. fos.write(line.replace(sourceValue, formValue)+“n“).getBytes();605. 606. fos.flush();607. fos.close();608. bis.close();609. catch(FileNotFoundException ex)610. ex.printStackTrace();611. catch(IOException ioe)612. ioe.printStackTrace();613.

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

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报