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

下载本文档

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

文档简介

1、一、多种方式读文件内容。 1、按字节读取文件内容 2、按字符读取文件内容 3、按行读取文件内容 4、随机读取文件内容 Java代码  1. import   2. import   3. import   4. import   5. import   6. import   7. import   8. import

2、   9. import    10. public class ReadFromFile   11. /* 12.    * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 13.    * param fileName 文件的名 14.    */  15. public&

3、#160;static void readFileByBytes(String fileName)  16.    File file = new File(fileName);  17.    InputStream in = null;  18.    try   19.    

4、60;("以字节为单位读取文件内容,一次读一个字节:");  20.     / 一次读一个字节  21.     in = new FileInputStream(file);  22.     int tempbyte;  23.     while(tempbyte=in.read(

5、) != -1)  24.      (tempbyte);  25.       26.     in.close();  27.     catch (IOException e)   28.     e.printStackTrace

6、();  29.     return;  30.      31.    try   32.     ("以字节为单位读取文件内容,一次读多个字节:");  33.     /一次读多个字节  34.     byte 

7、;tempbytes = new byte100;  35.     int byteread = 0;  36.     in = new FileInputStream(fileName);  37.     ReadFromFile.showAvailableBytes(in);  38.  

8、   /读入多个字节到字节数组中,byteread为一次读入的字节数  39.     while (byteread = in.read(tempbytes) != -1)  40.      (tempbytes, 0, byteread);  41.       42.  

9、   catch (Exception e1)   43.     e1.printStackTrace();  44.     finally   45.     if (in != null)  46.      try  &

10、#160;47.       in.close();  48.       catch (IOException e1)   49.        50.       51.      52.   53. /*

11、 54.    * 以字符为单位读取文件,常用于读文本,数字等类型的文件 55.    * param fileName 文件名 56.    */  57. public static void readFileByChars(String fileName)  58.    File file =

12、0;new File(fileName);  59.    Reader reader = null;  60.    try   61.     ("以字符为单位读取文件内容,一次读一个字节:");  62.     / 一次读一个字符  63.   

13、60; reader = new InputStreamReader(new FileInputStream(file);  64.     int tempchar;  65.     while (tempchar = reader.read() != -1)  66.      /对于wind

14、ows下,/r/n这两个字符在一起时,表示一个换行。  67.      /但如果这两个字符分开显示时,会换两次行。  68.      /因此,屏蔽掉/r,或者屏蔽/n。否则,将会多出很多空行。  69.      if (char)tempchar) != '/r')  70.    &#

15、160;  (char)tempchar);  71.        72.       73.     reader.close();  74.     catch (Exception e)   75.     e.printStack

16、Trace();  76.      77.    try   78.     ("以字符为单位读取文件内容,一次读多个字节:");  79.     /一次读多个字符  80.     char tempchars = new char30;

17、60; 81.     int charread = 0;  82.     reader = new InputStreamReader(new FileInputStream(fileName);  83.     /读入多个字符到字符数组中,charread为一次读取字符数  84.     

18、while (charread = reader.read(tempchars)!=-1)  85.      /同样屏蔽掉/r不显示  86.      if (charread = tempchars.length)&&(tempcharstempchars.length-1 != '/r')  87.  

19、60;    (tempchars);  88.      else  89.       for (int i=0; i<charread; i+)  90.        if(tempcharsi = '/r') 

20、0;91.         continue;  92.        else  93.         (tempcharsi);  94.          95.     

21、    96.        97.       98.      99.     catch (Exception e1)   100.     e1.printStackTrace();  101.   &

22、#160;finally   102.     if (reader != null)  103.      try   104.       reader.close();  105.       catch (IOException e

23、1)   106.        107.       108.      109.   110. /* 111.    * 以行为单位读取文件,常用于读面向行的格式化文件 112.    * param fileName 文件名 113.

24、    */  114. public static void readFileByLines(String fileName)  115.    File file = new File(fileName);  116.    BufferedReader reader = null;  117.  

25、60; try   118.     ("以行为单位读取文件内容,一次读一整行:");  119.     reader = new BufferedReader(new FileReader(file);  120.     String tempString = null;  121.

26、     int line = 1;  122.     /一次读入一行,直到读入null为文件结束  123.     while (tempString = reader.readLine() != null)  124.      /显示行号  125. 

27、0;    ("line " + line + ": " + tempString);  126.      line+;  127.       128.     reader.close();  129.  

28、0;  catch (IOException e)   130.     e.printStackTrace();  131.     finally   132.     if (reader != null)  133.      try 

29、60; 134.       reader.close();  135.       catch (IOException e1)   136.        137.       138.      139. 

30、0; 140. /* 141.    * 随机读取文件内容 142.    * param fileName 文件名 143.    */  144. public static void readFileByRandomAccess(String fileName)  145.    RandomAccessFile

31、 randomFile = null;  146.    try   147.     ("随机读取一段文件内容:");  148.     / 打开一个随机访问文件流,按只读方式  149.     randomFile = new RandomAccessFile(

32、fileName, "r");  150.     / 文件长度,字节数  151.     long fileLength = randomFile.length();  152.     / 读文件的起始位置  153.     int beginIndex

33、0;= (fileLength > 4) ? 4 : 0;  154.     /将读文件的开始位置移到beginIndex位置。  155.     randomFile.seek(beginIndex);  156.     byte bytes = new byte10;  1

34、57.     int byteread = 0;  158.     /一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。  159.     /将一次读取的字节数赋给byteread  160.     while (byteread = randomFile.read(bytes) !=&#

35、160;-1)  161.      (bytes, 0, byteread);  162.       163.     catch (IOException e)  164.     e.printStackTrace();  165.     

36、finally   166.     if (randomFile != null)  167.      try   168.       randomFile.close();  169.       catch (IOException

37、0;e1)   170.        171.       172.      173.   174. /* 175.    * 显示输入流中还剩的字节数 176.    * param in 177.    */&#

38、160; 178. private static void showAvailableBytes(InputStream in)  179.    try   180.     ("当前字节输入流中的字节数为:" + in.available();  181.     catch (IOException e

39、)   182.     e.printStackTrace();  183.      184.   185.   186. public static void main(String args)   187.    String fileName = "C:/temp/ne

40、wTemp.txt"  188.    ReadFromFile.readFileByBytes(fileName);  189.    ReadFromFile.readFileByChars(fileName);  190.    ReadFromFile.readFileByLines(fileName);  191.    ReadFromFile.readFileByRando

41、mAccess(fileName);  192.   193.   194.   195.    196.   197. 二、将内容追加到文件尾部  198.   199. import   200. import   201. import   202.   203. /* 204. * 将内容追

42、加到文件尾部 205. */  206. public class AppendToFile   207.   208. /* 209.    * A方法追加文件:使用RandomAccessFile 210.    * param fileName 文件名 211.    * param content 追

43、加的内容 212.    */  213. public static void appendMethodA(String fileName, String content)  214.    try   215.     / 打开一个随机访问文件流,按读写方式  216.     Ran

44、domAccessFile randomFile = new RandomAccessFile(fileName, "rw");  217.     / 文件长度,字节数  218.     long fileLength = randomFile.length();  219.     /将写文件指针移到文

45、件尾。  220.     randomFile.seek(fileLength);  221.     randomFile.writeBytes(content);  222.     randomFile.close();  223.     catch (IOException e)  224.  &

46、#160;  e.printStackTrace();  225.      226.   227. /* 228.    * B方法追加文件:使用FileWriter 229.    * param fileName 230.    * param content 231.   

47、0;*/  232. public static void appendMethodB(String fileName, String content)  233.    try   234.     /打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件  235.     FileWriter writer&

48、#160;= new FileWriter(fileName, true);  236.     writer.write(content);  237.     writer.close();  238.     catch (IOException e)   239.     e.printSt

49、ackTrace();  240.      241.   242.   243. public static void main(String args)   244.    String fileName = "C:/temp/newTemp.txt"  245.    String&

50、#160;content = "new append!"  246.    /按方法A追加文件  247.    AppendToFile.appendMethodA(fileName, content);  248.    AppendToFile.appendMethodA(fileName, "append end. /n");&#

51、160; 249.    /显示文件内容  250.    ReadFromFile.readFileByLines(fileName);  251.    /按方法B追加文件  252.    AppendToFile.appendMethodB(fileName, content);  253.    AppendToFile.appendMe

52、thodB(fileName, "append end. /n");  254.    /显示文件内容  255.    ReadFromFile.readFileByLines(fileName);  256.   257.   258.   259. 三文件的各种操作类  260.   261. import ja

53、va.io.*;  262.   263. /* 264. * FileOperate.java 265. * 文件的各种操作 266. * author 杨彩 267. * 文件操作 1.0 268. */  269.   270. public class FileOperate  271.   272.   273. pu

54、blic FileOperate()  274.   275.   276. /* 277. * 新建目录 278. */  279. public void newFolder(String folderPath)  280.   281. try  282.   283. String filePath = folderPath;&#

55、160; 284. filePath = filePath.toString();  285. File myFilePath = new File(filePath);  286. if(!myFilePath.exists()  287.   288. myFilePath.mkdir();  289.   290. ("新建目录操作 成功执行");  

56、291.   292. catch(Exception e)  293.   294. ("新建目录操作出错");  295. e.printStackTrace();  296.   297.   298. /* 299. * 新建文件 300. */  301. public void newFile(String filePathAndNam

57、e, String fileContent)  302.   303. try  304.   305. String filePath = filePathAndName;  306. filePath = filePath.toString();  307. File myFilePath = new File(filePath);  308.

58、 if (!myFilePath.exists()  309.   310. myFilePath.createNewFile();  311.   312. FileWriter resultFile = new FileWriter(myFilePath);  313. PrintWriter myFile = new PrintWriter(resultFile);  314. S

59、tring strContent = fileContent;  315. myFile.println(strContent);  316. resultFile.close();  317. ("新建文件操作 成功执行");  318.   319. catch (Exception e)  320.   321. ("新建目录操作出错"); 

60、 322. e.printStackTrace();  323.   324.   325. /* 326. * 删除文件 327. */  328. public void delFile(String filePathAndName)  329.   330. try  331.   332. String filePath = fi

61、lePathAndName;  333. filePath = filePath.toString();  334. File myDelFile = new File(filePath);  335. myDelFile.delete();  336. ("删除文件操作 成功执行");  337.   338. catch (Exception e) &#

62、160;339.   340. ("删除文件操作出错");  341. e.printStackTrace();  342.   343.   344. /* 345. * 删除文件夹 346. */  347. public void delFolder(String folderPath)  348.   349. try  350.

63、   351. delAllFile(folderPath); /删除完里面所有内容  352. String filePath = folderPath;  353. filePath = filePath.toString();  354. File myFilePath = new File(filePath);  355. if(myFilePath.delete()  

64、;/删除空文件夹  356. ("删除文件夹" + folderPath + "操作 成功执行");  357.  else   358. ("删除文件夹" + folderPath + "操作 执行失败");  359.   360.   361. catch (Ex

65、ception e)  362.   363. ("删除文件夹操作出错");  364. e.printStackTrace();  365.   366.   367. /* 368. * 删除文件夹里面的所有文件 369. * param path String 文件夹路径 如 c:/fqf 370. */  371. pu

66、blic 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

67、;  381.   382. String tempList = file.list();  383. File temp = null;  384. for (int i = 0; i < tempList.length; i+)  385.   386. if(path.endsWith(File.separator) 

68、; 387.   388. temp = new File(path + tempListi);  389.   390. else  391.   392. temp = new File(path + File.separator + tempListi);  393.   394. if (temp.isFile

69、()  395.   396. temp.delete();  397.   398. if (temp.isDirectory()  399.   400. /delAllFile(path+"/"+ tempListi);/先删除文件夹里面的文件  401. delFolder(path+ File.separatorChar + tempListi);/再删除空文件夹 &

70、#160;402.   403.   404. ("删除文件操作 成功执行");  405.   406. /* 407. * 复制单个文件 408. * param oldPath String 原文件路径 如:c:/fqf.txt 409. * param newPath String 复制后路径 如:f:/fqf.txt 410.

71、*/  411. public void copyFile(String oldPath, String newPath)  412.   413. try  414.   415. int bytesum = 0;  416. int byteread = 0;  417. File oldfile = new

72、60;File(oldPath);  418. if (oldfile.exists()  419.   420. /文件存在时  421. InputStream inStream = new FileInputStream(oldPath); /读入原文件  422. FileOutputStream fs = new FileOutputStream(newPath);  

73、;423. byte buffer = new byte1444;  424. while ( (byteread = inStream.read(buffer) != -1)  425.   426. bytesum += byteread; /字节数 文件大小  427. (bytesum);  428. fs.write(buffer, 0,

74、 byteread);  429.   430. inStream.close();  431.   432. ("删除文件夹操作 成功执行");  433.   434. catch (Exception e)  435.   436. ("复制单个文件操作出错");  437. e.printStackTrace(); &#

75、160;438.   439.   440. /* 441. * 复制整个文件夹内容 442. * param oldPath String 原文件路径 如:c:/fqf 443. * param newPath String 复制后路径 如:f:/fqf/ff 444. */  445. public void copyFolder(String oldP

76、ath, 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; 

77、 453. for (int i = 0; i < file.length; i+)  454.   455. if(oldPath.endsWith(File.separator)  456.   457. temp=new File(oldPath+filei);  458.   459. else  460.   461. t

78、emp=new File(oldPath+File.separator+filei);  462.   463. if(temp.isFile()  464.   465. FileInputStream input = new FileInputStream(temp);  466. FileOutputStream output = new FileOutputStream(newPath +

79、 "/" +  467. (temp.getName().toString();  468. byte b = new byte1024 * 5;  469. int len;  470. while ( (len = input.read(b) != -1)  471.   472. output.writ

80、e(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+&quo

81、t;/"+filei);  482.   483.   484. ("复制文件夹操作 成功执行");  485.   486. catch (Exception e)  487.   488. ("复制整个文件夹内容操作出错");  489. e.printStackTrace();  490.   491. 

82、0; 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. &#

83、160; 499. copyFile(oldPath, newPath);  500. delFile(oldPath);  501.   502. /* 503. * 移动文件到指定目录 504. * param oldPath String 如:c:/fqf.txt 505. * param newPath String 如:d:/fqf.txt 506. */  

84、;507. public void moveFolder(String oldPath, String newPath)  508.   509. copyFolder(oldPath, newPath);  510. delFolder(oldPath);  511.   512. public static void main(String args)  513. 

85、0; 514. String aa,bb;  515. boolean exitnow=false;  516. ("使用此功能请按1 功能一:新建目录");  517. ("使用此功能请按2 功能二:新建文件");  518. ("使用此功能请按3 功能三:删除文件");  519. ("使用此功能请按4 功能四:删除文件夹"); &#

86、160;520. ("使用此功能请按5 功能五:删除文件夹里面的所有文件");  521. ("使用此功能请按6 功能六:复制文件");  522. ("使用此功能请按7 功能七:复制文件夹的所有内容");  523. ("使用此功能请按8 功能八:移动文件到指定目录");  524. ("使用此功能请按9 功能九:移动文件夹到指定目录");  5

87、25. ("使用此功能请按10 退出程序");  526. while(!exitnow)  527.   528. 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. c

温馨提示

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

评论

0/150

提交评论