/**新建一个类把下面代码放进去,注意要设置basePath(你要读取的文件夹),读取和写入的方法也都写好了.你可以根据自己的需求掉用就行了**/ static String basePath="/home/csvDir"; /** * 查找文件夹下所有符合csv的文件 * * @param dir 要查找的文件夹对象 * */ public static void findFile(File dir) throws IOException{ File[] dirFiles = dir.listFiles(); for(File temp : dirFiles){ if(!temp.isFile()){ findFile(temp); } //查找指定的文件 if(temp.isFile() && temp.getAbsolutePath().endsWith(".txt") ){ System.out.println(temp.isFile() + " " + temp.getAbsolutePath()); readFileContent(temp); } } } /** * @param file 要读取的文件对象 * @return 返回文件的内容 * */ public static String readFileContent(File file) throws IOException{ FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); StringBuffer sb = new StringBuffer(); while(br.ready()){ sb.append(br.readLine()); } System.out.println(sb.toString()); return sb.toString(); } /** * @param file 要写入的文件对象 * @param content 要写入的文件内容 * */ public static void writeFileContent(File file,String content) throws IOException{ FileWriter fw = new FileWriter(file); fw.write(content); fw.flush(); fw.close(); } public static void main(String[] args) { try { findFile(new File(basePath)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }