Java_IO操作_(读写、追加、删除、移动、复制、修改).doc_第1页
Java_IO操作_(读写、追加、删除、移动、复制、修改).doc_第2页
Java_IO操作_(读写、追加、删除、移动、复制、修改).doc_第3页
Java_IO操作_(读写、追加、删除、移动、复制、修改).doc_第4页
Java_IO操作_(读写、追加、删除、移动、复制、修改).doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一、多种方式读文件内容。1、按字节读取文件内容2、按字符读取文件内容3、按行读取文件内容4、随机读取文件内容Java代码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. publicclassReadFromFile11. /*12. *以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。13. *paramfileName文件的名14. */15. publicstaticvoidreadFileByBytes(StringfileName)16. Filefile=newFile(fileName);17. InputStreamin=null;18. try19. System.out.println(以字节为单位读取文件内容,一次读一个字节:);20. /一次读一个字节21. in=newFileInputStream(file);22. inttempbyte;23. while(tempbyte=in.read()!=-1)24. System.out.write(tempbyte);25. 26. in.close();27. catch(IOExceptione)28. e.printStackTrace();29. return;30. 31. try32. System.out.println(以字节为单位读取文件内容,一次读多个字节:);33. /一次读多个字节34. bytetempbytes=newbyte100;35. intbyteread=0;36. in=newFileInputStream(fileName);37. ReadFromFile.showAvailableBytes(in);38. /读入多个字节到字节数组中,byteread为一次读入的字节数39. while(byteread=in.read(tempbytes)!=-1)40. System.out.write(tempbytes,0,byteread);41. 42. catch(Exceptione1)43. e1.printStackTrace();44. finally45. if(in!=null)46. try47. in.close();48. catch(IOExceptione1)49. 50. 51. 52. 53. /*54. *以字符为单位读取文件,常用于读文本,数字等类型的文件55. *paramfileName文件名56. */57. publicstaticvoidreadFileByChars(StringfileName)58. Filefile=newFile(fileName);59. Readerreader=null;60. try61. System.out.println(以字符为单位读取文件内容,一次读一个字节:);62. /一次读一个字符63. reader=newInputStreamReader(newFileInputStream(file);64. inttempchar;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(Exceptione)75. e.printStackTrace();76. 77. try78. System.out.println(以字符为单位读取文件内容,一次读多个字节:);79. /一次读多个字符80. chartempchars=newchar30;81. intcharread=0;82. reader=newInputStreamReader(newFileInputStream(fileName);83. /读入多个字符到字符数组中,charread为一次读取字符数84. while(charread=reader.read(tempchars)!=-1)85. /同样屏蔽掉/r不显示86. if(charread=tempchars.length)&(tempcharstempchars.length-1!=/r)87. System.out.print(tempchars);88. else89. for(inti=0;i4)?4:0;154. /将读文件的开始位置移到beginIndex位置。155. randomFile.seek(beginIndex);156. bytebytes=newbyte10;157. intbyteread=0;158. /一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。159. /将一次读取的字节数赋给byteread160. while(byteread=randomFile.read(bytes)!=-1)161. System.out.write(bytes,0,byteread);162. 163. catch(IOExceptione)164. e.printStackTrace();165. finally166. if(randomFile!=null)167. try168. randomFile.close();169. catch(IOExceptione1)170. 171. 172. 173. 174. /*175. *显示输入流中还剩的字节数176. *paramin177. */178. privatestaticvoidshowAvailableBytes(InputStreamin)179. try180. System.out.println(当前字节输入流中的字节数为:+in.available();181. catch(IOExceptione)182. e.printStackTrace();183. 184. 185. 186. publicstaticvoidmain(Stringargs)187. StringfileName=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. importjava.io.FileWriter;200. importjava.io.IOException;201. importjava.io.RandomAccessFile;202. 203. /*204. *将内容追加到文件尾部205. */206. publicclassAppendToFile207. 208. /*209. *A方法追加文件:使用RandomAccessFile210. *paramfileName文件名211. *paramcontent追加的内容212. */213. publicstaticvoidappendMethodA(StringfileName,Stringcontent)214. try215. /打开一个随机访问文件流,按读写方式216. RandomAccessFilerandomFile=newRandomAccessFile(fileName,rw);217. /文件长度,字节数218. longfileLength=randomFile.length();219. /将写文件指针移到文件尾。220. randomFile.seek(fileLength);221. randomFile.writeBytes(content);222. randomFile.close();223. catch(IOExceptione)224. e.printStackTrace();225. 226. 227. /*228. *B方法追加文件:使用FileWriter229. *paramfileName230. *paramcontent231. */232. publicstaticvoidappendMethodB(StringfileName,Stringcontent)233. try234. /打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件235. FileWriterwriter=newFileWriter(fileName,true);236. writer.write(content);237. writer.close();238. catch(IOExceptione)239. e.printStackTrace();240. 241. 242. 243. publicstaticvoidmain(Stringargs)244. StringfileName=C:/temp/newTemp.txt;245. Stringcontent=newappend!;246. /按方法A追加文件247. AppendToFile.appendMethodA(fileName,content);248. AppendToFile.appendMethodA(fileName,appendend./n);249. /显示文件内容250. ReadFromFile.readFileByLines(fileName);251. /按方法B追加文件252. AppendToFile.appendMethodB(fileName,content);253. AppendToFile.appendMethodB(fileName,appendend./n);254. /显示文件内容255. ReadFromFile.readFileByLines(fileName);256. 257. 258. 259. 三文件的各种操作类260. 261. importjava.io.*;262. 263. /*264. *FileOperate.java265. *文件的各种操作266. *author杨彩/m/yangcai267. *文件操作1.0268. */269. 270. publicclassFileOperate271. 272. 273. publicFileOperate()274. 275. 276. /*277. *新建目录278. */279. publicvoidnewFolder(StringfolderPath)280. 281. try282. 283. StringfilePath=folderPath;284. filePath=filePath.toString();285. FilemyFilePath=newFile(filePath);286. if(!myFilePath.exists()287. 288. myFilePath.mkdir();289. 290. System.out.println(新建目录操作成功执行);291. 292. catch(Exceptione)293. 294. System.out.println(新建目录操作出错);295. e.printStackTrace();296. 297. 298. /*299. *新建文件300. */301. publicvoidnewFile(StringfilePathAndName,StringfileContent)302. 303. try304. 305. StringfilePath=filePathAndName;306. filePath=filePath.toString();307. FilemyFilePath=newFile(filePath);308. if(!myFilePath.exists()309. 310. myFilePath.createNewFile();311. 312. FileWriterresultFile=newFileWriter(myFilePath);313. PrintWritermyFile=newPrintWriter(resultFile);314. StringstrContent=fileContent;315. myFile.println(strContent);316. resultFile.close();317. System.out.println(新建文件操作成功执行);318. 319. catch(Exceptione)320. 321. System.out.println(新建目录操作出错);322. e.printStackTrace();323. 324. 325. /*326. *删除文件327. */328. publicvoiddelFile(StringfilePathAndName)329. 330. try331. 332. StringfilePath=filePathAndName;333. filePath=filePath.toString();334. FilemyDelFile=newFile(filePath);335. myDelFile.delete();336. System.out.println(删除文件操作成功执行);337. 338. catch(Exceptione)339. 340. System.out.println(删除文件操作出错);341. e.printStackTrace();342. 343. 344. /*345. *删除文件夹346. */347. publicvoiddelFolder(StringfolderPath)348. 349. try350. 351. delAllFile(folderPath);/删除完里面所有内容352. StringfilePath=folderPath;353. filePath=filePath.toString();354. FilemyFilePath=newFile(filePath);355. if(myFilePath.delete()/删除空文件夹356. System.out.println(删除文件夹+folderPath+操作成功执行);357. else358. System.out.println(删除文件夹+folderPath+操作执行失败);359. 360. 361. catch(Exceptione)362. 363. System.out.println(删除文件夹操作出错);364. e.printStackTrace();365. 366. 367. /*368. *删除文件夹里面的所有文件369. *parampathString文件夹路径如c:/fqf370. */371. publicvoiddelAllFile(Stringpath)372. 373. Filefile=newFile(path);374. if(!file.exists()375. 376. return;377. 378. if(!file.isDirectory()379. 380. return;381. 382. StringtempList=file.list();383. Filetemp=null;384. for(inti=0;itempList.length;i+)385. 386. if(path.endsWith(File.separator)387. 388. temp=newFile(path+tempListi);389. 390. else391. 392. temp=newFile(path+File.separator+tempListi);393. 394. if(temp.isFile()395. 396. temp.delete();397. 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. *paramoldPathString原文件路径如:c:/fqf.txt409. *paramnewPathString复制后路径如:f:/fqf.txt410. */411. publicvoidcopyFile(StringoldPath,StringnewPath)412. 413. try414. 415. intbytesum=0;416. intbyteread=0;417. Fileoldfile=newFile(oldPath);418. if(oldfile.exists()419. 420. /文件存在时421. InputStreaminStream=newFileInputStream(oldPath);/读入原文件422. FileOutputStreamfs=newFileOutputStream(newPath);423. bytebuffer=newbyte1444;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.println(删除文件夹操作成功执行);433. 434. catch(Exceptione)435. 436. System.out.println(复制单个文件操作出错);437. e.printStackTrace();438. 439. 440. /*441. *复制整个文件夹内容442. *paramoldPathString原文件路径如:c:/fqf443. *paramnewPathString复制后路径如:f:/fqf/ff444. */445. publicvoidcopyFolder(StringoldPath,StringnewPath)446. 447. try448. 449. (newFile(newPath).mkdirs();/如果文件夹不存在则建立新文件夹450. Filea=newFile(oldPath);451. Stringfile=a.list();452. Filetemp=null;453. for(inti=0;ifile.length;i+)454. 455. if(oldPath.endsWith(File.separator)456. 457. temp=newFile(oldPath+filei);458. 459. else460. 461. temp=newFile(oldPath+File.separator+filei);462. 463. if(temp.isFile()464. 465. FileInputStreaminput=newFileInputStream(temp);466. FileOutputStreamoutput=newFileOutputStream(newPath+/+467. (temp.getName().toString();468. byteb=newbyte1024*5;469. intlen;470. 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. 486. catch(Exceptione)487. 488. System.out.println(复制整个文件夹内容操作出错);489. e.printStackTrace();490. 491. 492. /*493. *移动文件到指定目录494. *paramoldPathString如:c:/fqf.txt495. *paramnewPathString如:d:/fqf.txt496. */497. publicvoidmoveFile(StringoldPath,StringnewPath)498. 499. copyFile(oldPath,newPath);500. delFile(oldPath);501. 502. /*503. *移动文件到指定目录504. *paramoldPathString如:c:/fqf.txt505. *paramnewPathString如:d:/fqf.txt506. */507. publicvoidmoveFolder(StringoldPath,StringnewPath)508. 509. copyFolder(oldPath,newPath);510. delFolder(oldPath);511. 512. publicstaticvoidmain(Stringargs)513. 514. Stringaa,bb;515. booleanexitnow=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(使用此功能请按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. 528. FileOperatefo=newFileOperate();529. try530. 531. BufferedReaderBin=newBufferedReader(newInputStreamReader(System.in);532. Stringa=Bin.readLine();533. intb=Integer.parseInt(a);534. switch(b)535. 536. case1:System.out.println(你选择了功能一请输入目录名);537. aa=Bin.readLine();538. fo.newFolder(aa);539. break;540. case2:System.out.println(你选择了功能二请输入文件名);541. aa=Bin.readLine();542. System.out.println(请输入在+aa+中的内容);543. bb=Bin.readLine();544. fo.newFile(aa,bb);545. break;546. case3:System.out.println(你选择了功能三请输入文件名);547. aa=Bin.readLine();548. fo.delFile(aa);549. break;550. case4:System.out.println(你选择了功能四请输入文件名);551. aa=Bin.readLine();552. fo.delFolder(aa);553. break;554. case5:System.out.println(你选择了功能五请输入文件名);555. aa=Bin.readLine();556. fo.delAllFile(aa);557. break;558. case6:System.out.println(你选择了功能六请输入文件名);559. aa=Bin.readLine();560. System.out.println(请输入目标文件名);561. bb=Bin.readLine();562. fo.copyFile(aa,bb);563. break;564. case7:System.out.print

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论