Java常用文件操作--简单实用
2019-08-05 21:06
148 查看
文章目录
修改系统分割符
主要解决在windows和linux目录下文件分割符不一致的问题。
//系统目录分隔符 private static String separator; static { // 设置系统目录分隔符 if ("/".equals(File.separator)){ separator = "/"; }else if ("\\".equals(File.separator)){ separator = "\\"; }else { separator = File.separator; } } /** * 修改系统分割符 * @param filePath * @return */ public static String modifyDelimiter(String filePath){ String result = ""; if ("/".equals(File.separator)){ result = filePath.replaceAll("/",separator+separator).replaceAll("\\\\",separator+separator); }else if ("\\".equals(File.separator)){ result = filePath.replaceAll("/",separator+separator).replaceAll("\\\\",separator+separator); } System.out.println("文件目录:"+result); return result; }
创建目录
/** * 创建目录 * @param dirNamePath:目录路径名称 * @return */ public static boolean createDir(String dirNamePath) { dirNamePath = modifyDelimiter(dirNamePath); File dir = new File(dirNamePath); if (dir.exists()) {// 判断目录是否存在 System.out.println("创建目录失败,目标目录已存在!"); return false; } if (dir.mkdirs()) {// 创建目标目录 System.out.println("创建目录成功!" + dirNamePath); return true; } else { System.out.println("创建目录失败!"); return false; } }
创建文件
/** * 创建文件 * @param filename * @return */ public static boolean createFile(String dirPath,String filename) throws IOException { File dir = new File(dirPath); if (!dir.isDirectory()){ createDir(dirPath); } String filePath = modifyDelimiter(dirPath+File.separator+filename); File file = new File(filePath); if (file.exists()){ System.out.println("创建文件失败,目标文件已存在!"); return false; } if (file.createNewFile()){ System.out.println("创建文件成功,目标文件:"+file.getAbsolutePath()); }else { System.out.println("创建文件失败"); return false; } return false; }
删除目录
/** * 删除目录 * @param dirNamePath:目录路径名称 * @return */ public static boolean deleteDir(String dirNamePath){ dirNamePath = modifyDelimiter(dirNamePath); File file = new File(dirNamePath); /** * 要利用File类的delete()方法删除目录时,必须保证该目录下没有文件或者子目录,否则删除失败, * 因此在实际应用中,我们要删除目录,必须利用递归删除该目录下的所有子目录和文件,然后再删除该目录。 */ if (!file.isDirectory()) { // 当且仅当此抽象路径名表示的文件存在,且是一个目录时,返回true,直接删除目录即可 file.delete(); } else if (file.isDirectory()) { String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File delfile = new File(dirNamePath + File.separator + filelist[i]); if (!delfile.isDirectory()) { delfile.delete(); System.out.println(delfile.getAbsolutePath() + "删除文件成功"); } else if (delfile.isDirectory()) { deleteFile(dirNamePath + File.separator + filelist[i]); } } System.out.println(file.getAbsolutePath() + "删除成功"); file.delete(); } return true; }
删除文件
/** * 删除文件 * @param dirPath * @param filename * @return */ public static boolean deleteFile(String dirPath,String filename){ String filePath = modifyDelimiter(dirPath+File.separator+filename); File file = new File(filePath); if (file.isFile() && file.exists()) {// 路径为文件且不为空则进行删除 file.delete();// 文件删除 return true; } return false; }
/** * 删除文件 * @param filePath * @return */ public static boolean deleteFile(String filePath){ File file = new File(filePath); if (file.isFile() && file.exists()) {// 路径为文件且不为空则进行删除 file.delete();// 文件删除 return true; } return false; }
复制文件
/** * 复制文件 * @param sourceFile * @param targetFile * @throws IOException */ public static void copyFile(File sourceFile,File targetFile) throws IOException{ // 新建文件输入流并对它进行缓冲 FileInputStream input = new FileInputStream(sourceFile); BufferedInputStream inBuff=new BufferedInputStream(input); // 新建文件输出流并对它进行缓冲 FileOutputStream output = new FileOutputStream(targetFile); BufferedOutputStream outBuff=new BufferedOutputStream(output); // 缓冲数组 byte[] b = new byte[1024 * 5]; int len; while ((len =inBuff.read(b)) != -1) { outBuff.write(b, 0, len); } // 刷新此缓冲的输出流 outBuff.flush(); //关闭流 inBuff.close(); outBuff.close(); output.close(); input.close(); }
复制文件夹
/** * 复制文件夹 * @param sourceDir * @param targetDir * @throws IOException */ public static void copyDirectiory(String sourceDir, String targetDir) throws IOException { // 新建目标目录 (new File(targetDir)).mkdirs(); // 获取源文件夹当前下的文件或目录 File[] file = (new File(sourceDir)).listFiles(); for (int i = 0; i < file.length; i++) { if (file[i].isFile()) { // 源文件 File sourceFile=file[i]; // 目标文件 File targetFile=new File(new File(targetDir).getAbsolutePath() +File.separator+file[i].getName()); copyFile(sourceFile,targetFile); } if (file[i].isDirectory()) { // 准备复制的源文件夹 String dir1=sourceDir + "/" + file[i].getName(); // 准备复制的目标文件夹 String dir2=targetDir + "/"+ file[i].getName(); copyDirectiory(dir1, dir2); } } }
读文件(推荐使用BufferedReader读取)
/** * 利用BufferedReader读取 * @param filePath * @return * @throws IOException */ public static String readFileByBufferedReader(String filePath) throws IOException { File file = new File(filePath); if(!file.isDirectory() || file.exists()){ FileInputStream fileInputStream = new FileInputStream(file); InputStreamReader inStreamReader = new InputStreamReader(fileInputStream, "utf-8"); BufferedReader reader = new BufferedReader(inStreamReader); String temp = null; StringBuffer buffer = new StringBuffer(); while((temp = reader.readLine()) != null){ buffer.append(temp+"\n"); } return buffer.toString(); } return null; }
/** * 利用FileInputStream读取文件 * @param filePath * @return * @throws IOException */ public static String readFileByInputStream(String filePath) throws IOException { File file = new File(filePath); if(!file.isDirectory() || file.exists()){ FileInputStream fileInputStream= new FileInputStream(file); byte[] buf = new byte[1024]; StringBuffer sb= new StringBuffer(); while((fileInputStream.read(buf)) != -1){ sb.append(new String(buf)); } return sb.toString(); } return null; }
写文件
/** * 利用StringBuffer写文件 * @param filePath * @return */ public static boolean writeFileByStringBuffer(String filePath) throws IOException { File file = new File(filePath); if(!file.exists()){ file.createNewFile(); } FileOutputStream output = new FileOutputStream(file,true); for(int i=0;i<10000;i++){ StringBuffer sb = new StringBuffer(); sb.append("这是第"+i+"行:前面介绍的各种方法都不关用,为什么总是奇怪的问题 "); output.write(sb.toString().getBytes("utf-8")); } output.close(); return true; }
相关文章推荐
- 发布一个简单实用的xml文件操作帮助类
- java常用的文件读写操作
- 简单实用的文件操作-流读写器
- Java 文件简单读操作
- 安卓、java常用文件操作(复制,读取,写入,转换等)
- java 文件的读取与写入(简单操作)
- HDFS上的常用文件操作java code实现
- 黑马程序员——java 文件的常用操作
- Java:简单的文件操作,将用字符表示的16进制数转为对应的2进制内容保存到新文件中
- [Java工具类]Spring常用工具类 1.文件资源操作和Web相关工具类
- 详解JAVA常用的时间操作【实用】
- 文章标题 Java中io流的一些简单操作(包含文件复制,向硬盘中写入文本文件,以及io流高级应用序列化和反序列化)
- java文件操作-简单网页计数器遇到的问题
- JDBC--实现用java连接oracle数据库的操作(简单实用)
- java.io.File操作文件的一些常用方法
- java中关于文件操作常用工具类
- java.io.file 的常用文件操作
- java:File类中对文件或目录进行操作的常用方法以及遍历文件夹下面的文件
- Rhythmk 一步一步学 JAVA (19) JAVA IO 文件常用操作
- java----使用socket模拟简单的http请求服务器,响应简单的文件请求操作