1、JavaIO操作读写追加删除移动复制修改一、多种方式读文件内容。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。Ran
2、domAccessFile;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.i
3、n=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=newFile
4、InputStream(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.
5、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;
6、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(”以字符为单位读取文件内容
7、,一次读多个字节:);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。length1!=/r)87.System。out.pr
8、int(tempchars);88.else89.for(inti=0;icharread;i+)90.if(tempcharsi=/r)91.continue;92.else93.System。out.print(tempcharsi);94.95.96.97.98.99.catch(Exceptione1)100.e1。printStackTrace();101.finally102.if(reader!=null)103.try104.reader.close();105.catch(IOExceptione1)106.107.108.109.110./111.以行为单位读取文件,常用于
9、读面向行的格式化文件112.paramfileName文件名113.*/114.publicstaticvoidreadFileByLines(StringfileName)115.Filefile=newFile(fileName);116.BufferedReaderreader=null;117.try118.System.out.println(以行为单位读取文件内容,一次读一整行:);119.reader=newBufferedReader(newFileReader(file);120.StringtempString=null;121.intline=1;122./一次读入一行,
10、直到读入null为文件结束123.while(tempString=reader.readLine()!=null)124./显示行号125.System。out。println(line”+line+:”+tempString);126.line+;127.128.reader.close();129.catch(IOExceptione)130.e。printStackTrace();131.finally132.if(reader!=null)133.try134.reader.close();135.catch(IOExceptione1)136.137.138.139.140./*1
11、41.随机读取文件内容142.*paramfileName文件名143.*/144.publicstaticvoidreadFileByRandomAccess(StringfileName)145.RandomAccessFilerandomFile=null;146.try147.System.out。println(随机读取一段文件内容:”);148./打开一个随机访问文件流,按只读方式149.randomFile=newRandomAccessFile(fileName,”r”);150./文件长度,字节数151.longfileLength=randomFile.length();1
12、52./读文件的起始位置153.intbeginIndex=(fileLength4)?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
13、.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.avail
14、able();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.readFileByRa
15、ndomAccess(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.*
16、/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.r
17、andomFile.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
18、);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.appendMe
19、thodA(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。i
20、o.;262.263./264.FileOperate。java265.文件的各种操作266.author杨彩http:/。cn/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();2
21、85.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,StringfileC
22、ontent)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.Str
23、ingstrContent=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.printStackTr
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1