public class TestFile { public void getFiles(String path){ File file = new File(path); File[] files = file.listFiles(); for(File f: files){ String fName = f.getName(); if(!f.isFile()){ System.out.println(f.getParent()+"\\"+fName); String newPath = f.getAbsolutePath();//返回绝对路径 getFiles(newPath); }else{ System.out.println(f.getParent()+"\\"+fName); } } } public static void main(String[] args) { //用户输入路径 InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String path; try { path = br.readLine(); new TestFile().getFiles(path); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
希望对部分人有用。