Java 简单IO文件解决
发布时间:2021-11-22 09:17:35 所属栏目:PHP教程 来源:互联网
导读:Java 简单IO文件处理 package com.java.file; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * ope
Java 简单IO文件处理 package com.java.file; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * operate file * @author Administrator * */ public class OperateFile { /** * create this file * @param filePath * @throws IOException */ public static void createFile(String filePath) throws IOException{ int lastPoint = filePath.lastIndexOf(""); if(lastPoint==-1){ throw new IOException("this filePath is not true !!"); } String folderPath = filePath.substring(0, lastPoint); File folderFile = new File(folderPath); if(folderFile.exists()){ File newFile = new File(filePath); if(!newFile.exists()){ newFile.createNewFile(); }else{ throw new IOException("the file is exists !!"); } }else{ throw new IOException("application cann't find the folder!!"); } System.out.println("execute createFile(filePath) success!!"); } /** * delete this file * @param filePath * @throws IOException */ public static void deleteFile(String filePath) throws IOException{ String folderPath = filePath.substring(0, filePath.lastIndexOf("")); File folderFile = new File(folderPath); if(folderFile.exists()){ File file = new File(filePath); if(file.exists()){ file.delete(); }else{ throw new IOException("the file is not exists !!"); } }else{ throw new IOException("application cann't find the folder!!"); } System.out.println("execute deleteFile(filePath) success!!"); } /** * copy file to target file * @param srcPath * @param targetPath * @throws IOException */ public static void copyFile(String srcPath, String targetPath) throws IOException{ File srcFile = new File(srcPath); if(srcFile.exists()){ //create fileInputDtream read file FileInputStream fileInputStream = new FileInputStream(srcFile); File targetFile = new File(targetPath); if(!targetFile.exists()){ createFile(targetPath); } //create fileOutputStream write file FileOutputStream fileOutputStream = new FileOutputStream(targetFile); int content = fileInputStream.read(); while(content!=-1){ fileOutputStream.write(content); content = fileInputStream.read(); } fileOutputStream.flush(); fileOutputStream.close(); fileInputStream.close(); System.out.println("execute copyFile(srcPath, targetPath) success!!"); }else { throw new IOException("the src file is not exists!!"); } } /** * copy file to target file by buffered * @param srcPath * @param targetPath * @throws IOException */ public static void copyFileByBuffered(String srcPath, String targetPath) throws IOException{ File srcFile = new File(srcPath); if(srcFile.exists()){ //create fileInputDtream read file FileInputStream fileInputStream = new FileInputStream(srcFile); //create bufferedInputStream BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); File targetFile = new File(targetPath); if(!targetFile.exists()){ createFile(targetPath); } //create fileOutputStream write file FileOutputStream fileOutputStream = new FileOutputStream(targetFile); //create bufferedOutputStream BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); int content = bufferedInputStream.read(); while(content!=-1){ bufferedOutputStream.write(content); content = bufferedInputStream.read(); } fileOutputStream.flush(); bufferedOutputStream.close(); bufferedInputStream.close(); fileOutputStream.close(); fileInputStream.close(); System.out.println("execute copyFileByBuffered(srcPath, targetPath) success!!"); }else { throw new IOException("the src file is not exists!!"); } } /** * copy file to target file by buffered ,then delete the file * @param srcPath * @param targetPath * @throws IOException */ public static void cutFileByBuffered(String srcPath, String targetPath) throws IOException{ copyFileByBuffered(srcPath, targetPath); deleteFile(srcPath); System.out.println("execute cutFileByBuffered(srcPath, targetPath) success!!"); } /** * create this folder * @param folderPath * @throws IOException */ public static void createFolder(String folderPath) throws IOException{ File folderFile = new File(folderPath); if(!folderFile.exists()){ //folderFile.mkdir(); //mkdirs 会创建指定路径的多个不存在文件夹,但是mkdir只会创建最底层的文件夹,如果其父目录不存在 则什么都不做 folderFile.mkdirs(); }else{ throw new IOException("this folder is exists!!!"); } System.out.println("execute createFolder(folderPath) success!!"); } /** * copy folder to target folder * @param srcFolderPath * @param targetFolderPath * @throws IOException */ public static void copyFolder(String srcFolderPath, String targetFolderPath) throws IOException{ File srcFolder = new File(srcFolderPath); if(srcFolder.exists()){ createFolder(targetFolderPath); String [] folderStrs = srcFolder.list(); for (int i = 0; i < folderStrs.length; i++) { String subSrcFolderPath = srcFolderPath + File.separator + folderStrs[i]; File subSrcFolder = new File(subSrcFolderPath); String subTargetFilePath = targetFolderPath + File.separator + folderStrs[i]; if(subSrcFolder.isDirectory()){ copyFolder(subSrcFolderPath, subTargetFilePath); }else{ copyFileByBuffered(subSrcFolderPath, subTargetFilePath); } } }else{ throw new IOException("this src folder is not exists!!!"); } System.out.println("execute copyFolder(srcFolderPath, String targetFolderPath) success!!"); } /** * delete this folder * @param folderPath * @throws IOException */ public static void deleteFolder(String folderPath) throws IOException{ File folderFolder = new File(folderPath); if(folderFolder.exists()){ String [] folderStrs = folderFolder.list(); for (int i = 0; i < folderStrs.length; i++) { String subFolderPath = folderPath + File.separator + folderStrs[i]; File subFolder = new File(subFolderPath); if(subFolder.isDirectory()){ deleteFolder(subFolderPath); }else{ subFolder.delete(); } } folderFolder.delete(); }else{ throw new IOException("this src folder is not exists!!!"); } System.out.println("execute deleteFolder(folderPath) success!!"); } public static void main(String[] args) { try { //OperateFile.deleteFile("D:a"); //OperateFile.copyFile("D:a1.txt", "D:b.txt"); //OperateFile.copyFileByBuffered("D:a.txt", "D:b.txt"); //OperateFile.cutFileByBuffered("D:a.txt", "D:b.txt"); //OperateFile.createFolder("D:aaa.txt"); //OperateFile.copyFolder("d:a","d:newa"); OperateFile.deleteFolder("d:newa"); } catch (Exception e) { e.printStackTrace(); } } } ![]() (编辑:应用网_丽江站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |