/*** 文件重命名<br>* @param fromPath 文件所在目录* @param suffix 目标文件的后缀格式*/public static void reName(String fromPath,String suffix) {File file = new File(fromPath);// 判断是不是文件目录if (file.isDirectory()) {//获取目录下所有文件File[] files = file.listFiles();int number = 0;//遍历目录下的文件for (File fileFrom : files) {number++;String fromFileName = fileFrom.getName();String toFileName = fromPath + File.separator + number + "."+suffix;File toFile = new File(toFileName);// 开始更名fileFrom.renameTo(toFile);System.out.println(fromFileName+"---成功重命名为---"+number + "."+suffix);}}}/*** 获得单个文件的大小(KB)* @param path 文件的路径*/public static float getSingleFileSize(String path) throws IOException {FileInputStream fis = null;File f = new File(path);float size = 0;try {if (f.exists()) {if (f.isDirectory()) {File flist[] = f.listFiles();for (int i = 0; i < flist.length; i++) {size = size + getSingleFileSize(flist[i].getPath());}} else {fis = new FileInputStream(f);size = fis.available();size = (float) (size / 1024);}} else {f.createNewFile();System.out.println("文件不存在");}} catch (Exception e) {e.printStackTrace();} finally {fis.close();}return size;}/*** 遍历目录下所有文件的大小(KB)* @param path 目录路径*/public static void getPathFileSize(String filePath) throws IOException {File file = new File(filePath);if (file.isDirectory()) {File[] files = file.listFiles();// 获取此目录下的文件列表for (File fileFrom : files) {String fileName = fileFrom.getName();String srcImageFile = fileFrom.getAbsolutePath();float fileSize = getSingleFileSize(srcImageFile);System.out.println(fileName + "----size=" + fileSize + "kb");}}}/*** 复制单个文件(原名复制)* @param oldPathFile 准备复制的文件源* @param newPathFile 拷贝到新绝对路径带文件名(注:目录路径需带文件名)* @return*/public static void CopySingleFileTo(String oldPathFile, String targetPath) {try {int bytesum = 0;int byteread = 0;File oldfile = new File(oldPathFile);String targetfile = targetPath + File.separator + oldfile.getName();if (oldfile.exists()) { //文件存在时InputStream inStream = new FileInputStream(oldPathFile); //读入原文件FileOutputStream fs = new FileOutputStream(targetfile);byte[] buffer = new byte[1444];while ((byteread = inStream.read(buffer)) != -1) {bytesum += byteread; //字节数 文件大小fs.write(buffer, 0, byteread);}inStream.close();}} catch (Exception e) {e.printStackTrace();}}
java读取文件(不断更新)
最新推荐文章于 2024-07-01 02:06:53 发布