JAVA文件操作类和文件夹的操作 .docx
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《JAVA文件操作类和文件夹的操作 .docx》由会员分享,可在线阅读,更多相关《JAVA文件操作类和文件夹的操作 .docx(22页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精品名师归纳总结JAVA 文件操作类和文件夹的操作packagecom.gamvan.tools; importjava.io.BufferedReader; importjava.io.File;importjava.io.FileInputStream; importjava.io.FileOutputStream; importjava.io.FileWriter;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader; importjava.io.PrintWriter;im
2、portjava.util.StringTokenizer; publicclass FileOperate privateString message; publicFileOperate /* 读取文本文件内容* paramfilePathAndName带有完整确定路径的文件名* paramencoding文本文件打开的编码方式* return返回文本文件的内容*/publicString readTxtString filePathAndName,String encodingthrowsI OExceptionencoding = encoding.trim;StringBuffer
3、str =new StringBuffer; String st = ;可编辑资料 - - - 欢迎下载精品名师归纳总结try FileInputStream fs =new FileInputStreamfilePathAndName; InputStreamReader isr;if encoding.equalsisr =new InputStreamReaderfs;else isr =new InputStreamReaderfs,encoding;BufferedReader br =new BufferedReaderisr; try String data = ;while d
4、ata = br.readLine.=null str.appenddata+ ;catch Exception e str.appende.toString;st = str.toString;catch IOException es st = ;returnst;/* 新建目录* paramfolderPath目录* return返回目录创建后的路径*/publicString createFolderString folderPath 可编辑资料 - - - 欢迎下载精品名师归纳总结String txt = folderPath; try java.io.File myFilePath
5、=new java.io.Filetxt; txt = folderPath;if .myFilePath.exists myFilePath.mkdir;catchException e message = 创建目录操作出错 ;returntxt;/* 多级目录创建* paramfolderPath预备要在本级目录下创建新目录的目录路径例如 c:myf* parampaths无限级目录参数,各级目录以单数线区分例如 a|b|c* return返回创建文件后的路径例如 c:myfac*/publicString createFoldersString folderPath, String pa
6、ths String txts = folderPath;try String txt;txts = folderPath;StringTokenizer st =newStringTokenizerpaths,|; for int i=0; st.hasMoreTokens; i+txt = st.nextToken.trim;if txts.lastIndexOf/.=-1 txts = createFoldertxts+txt;可编辑资料 - - - 欢迎下载精品名师归纳总结else txts = createFoldertxts+txt+/;catch Exception emessa
7、ge = 创建目录操作出错! ;returntxts;/* 新建文件* paramfilePathAndName文本文件完整确定路径及文件名* paramfileContent文本文件内容* return*/publicvoidcreateFileString filePathAndName, String fileContent try String filePath = filePathAndName; filePath = filePath.toString;File myFilePath =newFilefilePath;if .myFilePath.exists myFilePath
8、.createNewFile;FileWriter resultFile =new FileWritermyFilePath; PrintWriter myFile =newPrintWriterresultFile; String strContent = fileContent;myFile.printlnstrContent;可编辑资料 - - - 欢迎下载精品名师归纳总结myFile.close; resultFile.close;catchException e message = 创建文件操作出错 ;/* 有编码方式的文件创建* paramfilePathAndName文本文件完整
9、确定路径及文件名* paramfileContent文本文件内容* paramencoding编码方式 例如 GBK或者 UTF-8* return*/publicvoidcreateFileString filePathAndName, String fileContent, Strin g encoding try String filePath = filePathAndName; filePath = filePath.toString;File myFilePath =newFilefilePath;if .myFilePath.exists myFilePath.createNew
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA文件操作类和文件夹的操作 JAVA 文件 操作 文件夹
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内