列出一个文件夹下所有文件名创建新文件生成txt文件,写入指定字符串使用BufferedWriter使用PrintWriter按行读文件重命名移动文件依赖移动文件夹,相当于覆盖移动文件夹,某个文件夹,移动到另一个文件夹内移动文件,把文件移动到某个文件夹下移动到文件夹,自动识别操作文件还是文件夹列出一个文件夹下所有文件名publicvoidlistFileOnDir(StringfileDirPath){FilefileDirnewFile(fileDirPath);String〔〕filesOnDirfileDir。list();if(filesOnDir!null){log。info(listFileOnDir,filesOnDir:{},filesOnDir);for(StringfilePath:filesOnDir){try{StringfileFullPathfileDirfilePath;FilesingleFilenewFile(fileFullPath);log。info(listFileOnDir,singleFile:{},singleFile);}catch(Exceptione){log。error(listFileOnDirerror,e);}}}}创建新文件publicvoidcreateFile(Stringpath,Stringfilename)throwsIOException{FilefilenewFile(pathfilename);if(!file。exists())file。createNewFile();}生成txt文件,写入指定字符串使用BufferedWriterpublicvoidwriteContentToText(StringfileContent,StringfilePath,StringfileName)throwsIOException{生成的文件路径StringfullPathNamefilePathfileName;FilefilenewFile(fullPathName);if(!file。exists()){file。getParentFile()。mkdirs();}file。createNewFile();write解决中文乱码问题FileWriterfwnewFileWriter(file,true);OutputStreamWriterfwnewOutputStreamWriter(newFileOutputStream(file),UTF8);BufferedWriterbwnewBufferedWriter(fw);bw。write(fileContent);bw。flush();bw。close();fw。close();}使用PrintWriterpublicvoidwriteContentToTextOther(StringfileContent,StringfileFullPath,StringfileName){FileOutputStreamfosnull;PrintWriterpwnull;try{FilesendFilenewFile(fileFullPath);if(!sendFile。exists()){sendFile。createNewFile();创建目标文件}fosnewFileOutputStream(sendFile);pwnewPrintWriter(fos);pw。write(fileContent。toCharArray());pw。flush();fos。close();}catch(IOExceptione){log。error(writerfileerror,e);}finally{if(pw!null){pw。close();}}}按行读文件publicvoidreadFileOnLine(StringfullFilePath){try{FilefilenewFile(fullFilePath);InputStreamReaderinputReadernewInputStreamReader(newFileInputStream(file),utf8);BufferedReaderbfnewBufferedReader(inputReader);按行读取字符串StringlineContent;while((lineContentbf。readLine())!null){TODO:按行解析读取的文件,测试是不是不同编码的文件都能解析log。info(readFileOnLine,lineContent:{},lineContent);}bf。close();inputReader。close();}catch(IOExceptione){e。printStackTrace();}} InputStream读取出文件是字节流,InputStreamReader可以将字节流转换为字符流 newInputStreamReader时可指定编码:FilefilenewFile(fullFilePath);InputStreamReaderinputReadernewInputStreamReader(newFileInputStream(file),utf8);重命名publicvoidrenameFile(StringoldFullFilePath,StringnewFullFilePath){FilestartFilenewFile(oldFullFilePath);FileendFilenewFile(newFullFilePath);if(startFile。renameTo(endFile)){log。info(文件移动成功!目标路径:{},endFile。getAbsolutePath());}else{log。info(文件移动失败!起始路径:{},startFile。getAbsolutePath());}}移动文件 移动文件一系列操作主要用org。apache。commons。io下的包依赖dependencygroupIdorg。apache。commonsgroupIdcommonslang3artifactIdversion3。7versiondependencypublicvoidmoveFile(FilesingleFile,StringchangeDir,StringfilePathOnDir){StringnewGenFilePathchangeDirfilePathOnDir;FilebackedGeneratedCardFilenewFile(newGenFilePath);try{FileUtils。moveFile(singleFile,backedGeneratedCardFile);}catch(Exceptione){log。error(moveFileerror,e);}}移动文件夹,相当于覆盖publicvoidmoveDirectory(StringoldDirPath,StringnewDirPath){FileoldDirnewFile(oldDirPath);FilenewDirnewFile(newDirPath);try{FileUtils。moveDirectory(oldDir,newDir);}catch(Exceptione){log。error(moveDirectoryerror,e);}}移动文件夹,某个文件夹,移动到另一个文件夹内publicvoidmoveDirToDirectory(StringoldDirPath,StringnewDirPath){FileoldDirnewFile(oldDirPath);FilenewDirnewFile(newDirPath);try{FileUtils。moveDirectoryToDirectory(oldDir,newDir,true);}catch(Exceptione){log。error(moveDirToDirectoryerror,e);}}移动文件,把文件移动到某个文件夹下publicvoidmoveFileToDirectory(StringsrcFilePath,StringnewDirPath){FileoldFilenewFile(srcFilePath);FilenewDirnewFile(newDirPath);try{FileUtils。moveFileToDirectory(oldFile,newDir,true);}catch(Exceptione){log。error(moveFileToDirectoryerror,e);}}移动到文件夹,自动识别操作文件还是文件夹publicvoidmoveToDirectory(StringsrcPath,StringnewDirPath){FilesrcInfonewFile(srcPath);FilenewDirnewFile(newDirPath);try{FileUtils。moveToDirectory(srcInfo,newDir,true);}catch(Exceptione){log。error(moveFileToDirectoryerror,e);}}