- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class FileList {
- public static void main(String[] args) throws IOException{
- File dir = new File("E:\\黑马视频");
- List<File> list = new ArrayList<File>();
- fileToList(dir,list,0);
- File file = new File(dir,"视频列表.txt");
- writeToFile(list,file.toString());
- }
- public static void fileToList(File dir, List<File> list,int level)
- {
- level++;
- File[] files = dir.listFiles();
- for(File file:files)
- {
- if(file.isDirectory())
- fileToList(file,list,level);
- else
- {
- if(file.getName().endsWith(".avi"))
- list.add(file);
- // list.add(getLevel(level)+file);//加不进去啊
- }
- }
- }
- public static void writeToFile(List<File> list,String javaListFile)throws IOException
- {
-
- BufferedWriter bufw = null;
- try {
- bufw = new BufferedWriter(new FileWriter(javaListFile));
-
- for(File f :list)
- {
- String path = f.getAbsolutePath();
- bufw.write(path);
- bufw.newLine();
- bufw.flush();
- }
- } catch (Exception e) {
- throw e;
- }
- finally
- {
- try {
- if(bufw!=null)
- bufw.close();
- } catch (Exception e) {
- throw e;
- }
- }
- }
- public static String getLevel(int level)
- {
- StringBuilder sb = new StringBuilder();
- sb.append("|--");
- for(int x=0;x<level;x++)
- {
- sb.insert(0," ");
- }
- return sb.toString();
- }
- }
复制代码
我想进行目录分层,可是加不进去那个分隔符。不知道哪里问题,求解啊 |