加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java file 文件操作 operate file of java

发布时间:2020-12-14 06:27:17 所属栏目:Java 来源:网络整理
导读:java文件操作 com.b510; java.io.File; java.io.FileInputStream; java.io.FileOutputStream; java.io.FileWriter; java.io.InputStream; java.io.PrintWriter; OperateFiles { main(String[] args) { OperateFiles operateFiles = OperateFiles(); operateF

java文件操作

com.b510; java.io.File; java.io.FileInputStream; java.io.FileOutputStream; java.io.FileWriter; java.io.InputStream; java.io.PrintWriter; OperateFiles { main(String[] args) { OperateFiles operateFiles = OperateFiles(); operateFiles.newFolder("c:/hongten"); operateFiles.newFile("c:/hongten/Hello.txt","hello,i'm Hongten.你好"); operateFiles.deleteFile("c:/hongten/Hello.txt"); operateFiles.deleteFolder("c:/hongten"); operateFiles.copyFolder("c:/hongten","e:/hongten"); String expandedName=operateFiles.getExpandedName("c:/hongten/Hello.txt"); System.out.println(expandedName); System.out.println(operateFiles.getFilePath("c:/hongten/Hello.txt")); } String getExpandedName(String filePath){ filePath.substring(filePath.lastIndexOf(".")+1); } String getFilePath(String file){ file.substring(0,file.lastIndexOf("/")); } newFolder(String folderPath) { { File myFolderPath = File(folderPath.toString()); (!myFolderPath.exists()) { myFolderPath.mkdir(); } } (Exception e) { System.out.println("新建目录操作出错"); e.printStackTrace(); } } newFile(String filePath) { { File myFilePathFile = File(filePath.toString()); (!myFilePathFile.exists()) { myFilePathFile.createNewFile(); } } (Exception e) { System.out.println("新文件创建失败"); e.printStackTrace(); } } newFile(String filePath,String fileContent) { { newFile(filePath); FileWriter resultFile = FileWriter(filePath); PrintWriter myFile = PrintWriter(resultFile); myFile.println(fileContent); resultFile.close(); } (Exception e) { System.out.println("新建文件操作出错"); e.printStackTrace(); } } deleteFile(String filePath) { { File preDelFile = File(filePath); (preDelFile.exists()) { preDelFile.delete(); } { System.out.println(filePath + "不存在!"); } } (Exception e) { System.out.println("删除文件操作出错"); e.printStackTrace(); } } deleteFolder(String folderPath) { { delAllFiles(folderPath); File preDelFoder = File(folderPath); (preDelFoder.exists()) { preDelFoder.delete(); } { System.out.println(folderPath + "不存在!"); } } (Exception e) { System.out.println("删除文件操作出错"); e.printStackTrace(); } } delAllFiles(String folderPath) { File file = File(folderPath); (!file.exists()) { ; } (!file.isDirectory()) { ; } String[] tempList = file.list(); File temp = ; ( i = 0; i < tempList.length; i++) { (folderPath.endsWith(File.separator)) { temp = File(folderPath + tempList[i]); } { temp = File(folderPath + File.separator + tempList[i]); } (temp.isFile()) { temp.delete(); } (temp.isDirectory()) { delAllFiles(folderPath + "/" + tempList[i]); deleteFolder(folderPath + "/" + tempList[i]); } } } copyFile(String oldPath,String newPath) { { bytesum = 0; byteread = 0; File oldfile = File(oldPath); (oldfile.exists()) { InputStream inStream = FileInputStream(oldPath); FileOutputStream fs = FileOutputStream(newPath); [] buffer = [1444]; ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; fs.write(buffer,byteread); } inStream.close(); } } (Exception e) { System.out.println("复制单个文件操作出错"); e.printStackTrace(); } } copyFolder(String oldPath,String newPath) { { ( File(newPath)).mkdirs(); File a = File(oldPath); String[] file = a.list(); File temp = ; ( i = 0; i < file.length; i++) { (oldPath.endsWith(File.separator)) { temp = File(oldPath + file[i]); } { temp = File(oldPath + File.separator + file[i]); } (temp.isFile()) { FileInputStream input = FileInputStream(temp); FileOutputStream output = FileOutputStream(newPath + "/" + (temp.getName()).toString()); [] b = [1024 * 5]; len; ((len = input.read(b)) != -1) { output.write(b,len); } output.flush(); output.close(); input.close(); } (temp.isDirectory()) { copyFolder(oldPath + "/" + file[i],newPath + "/" + file[i]); } } } (Exception e) { System.out.println("复制整个文件夹内容操作出错"); e.printStackTrace(); } } moveFile(String oldPath,String newPath) { copyFile(oldPath,newPath); deleteFile(oldPath); } moveFolder(String oldPath,String newPath) { copyFolder(oldPath,newPath); deleteFolder(oldPath); } String getPath() { String sysPath = .getClass().getResource("/").getPath(); sysPath = sysPath.substring(1,sysPath.length() - 16); sysPath; } }

现在有时间把这些东西整理出来,给大家分享一下……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读