폴더 리스트 가져오기
String path="C:\";File dirFile=new File(path);
File []fileList=dirFile.listFiles();
for(File tempFile : fileList) {
if(tempFile.isFile()) {
String tempPath=tempFile.getParent();
String tempFileName=tempFile.getName();
System.out.println("Path="+tempPath);
System.out.println("FileName="+tempFileName);
/*** Do something withd tempPath and temp FileName ^^; ***/
}
}
정렬해서 가져오기
import java.io.FileFilter;
import java.io.IOException; import java.util.Arrays;
import java.util.Date;
import org.apache.commons.io.comparator.LastModifiedFileComparator;
import org.apache.commons.io.filefilter.FileFileFilter;
public class LastModifiedFileComparatorTest {
public static void main(String[] args) throws IOException {
File directory = new File(".");
// get just files, not directories
File[] files = directory.listFiles((FileFilter) FileFileFilter.FILE);
System.out.println("Default order");
displayFiles(files);
Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
System.out.println("\nLast Modified Ascending Order (LASTMODIFIED_COMPARATOR)");
displayFiles(files);
Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
System.out.println("\nLast Modified Descending Order (LASTMODIFIED_REVERSE)");
displayFiles(files);
}
public static void displayFiles(File[] files) {
for (File file : files) {
System.out.printf("File: %-20s Last Modified:" + new Date(file.lastModified()) + "\n", file.getName());
}
}
}
댓글
댓글 쓰기