Jav*_*ons 1 java file file-handling
如何列出文件系统中可用的文件的起始编号和结束编号?
就好像有500文件在那里C:\Test\如何列出从1到20的文件一样
start number and end number根据此列表给出可用于特定文件路径的文件.
我在java中尝试这个
我试过这样的事情,它给了我给定路径的所有可用文件
public static List<String> loadAllFiles(String filesLocation) {
//find OS
//String osName = System.getProperty("os.name");
//replace file path based on OS
filesLocation = filesLocation.replaceAll("\\\\|/", "\\"+System.getProperty("file.separator"));
List<String> pdfFiles = new ArrayList<String>();
if (log.isDebugEnabled()) {
log.debug("In loadAllFiles execute start");
}
File directoryList = new File(filesLocation);
File[] filesList = directoryList.listFiles();
try {
for (int count = 0; count < filesList.length; count++) {
if (!filesList[count].isDirectory() && filesList[count].getName().endsWith(SPLIT_AND_SAVE_WORKING_FILE_EXTENSION.trim())) {
// load only PDF files
pdfFiles.add(filesList[count].getName().replace(SPLIT_AND_SAVE_WORKING_FILE_EXTENSION.trim(), ""));
}
}
} catch (Exception filesException) {
filesException.printStackTrace();
//TODO : Log the exception
} finally {
if (filesList != null)
filesList = null;
if (directoryList != null)
directoryList = null;
}
log.debug("In loadAllFiles execute end");
return pdfFiles;
}
I think the question is misunderstood, Say if i have 1000 files[file names can be anything] and i want to restrict getting the files name like i will give starting Number and ending number. like 1 to 20 and i want to load those 20 files alone.
Run Code Online (Sandbox Code Playgroud)
没有使用普通Java 7的外部库的示例
import java.io.IOException;
import java.nio.file.DirectoryStream;
import static java.nio.file.DirectoryStream.Filter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
// list files starting with 1 till 20 "-.*"
public class FileNameFilter {
private static final Filter<Path> fileNameFilter = new Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
if (!Files.isRegularFile(entry)) {
return false;
}
return entry.getFileName().toString().matches("^([1][0-9]{0,1}|2[0]{0,1})-.*");
}
};
public static void main(String[] args) {
final String filesLocation = "resources/";
Path path = Paths.get(filesLocation);
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(path, fileNameFilter)) {
for (Path entry : dirStream) {
System.out.printf("%-5s: %s%n", "entry", entry.getFileName());
}
} catch (IOException e) {
// add your exception handling here
e.printStackTrace(System.err);
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 Java 8版本
// list files starting with 1 till 20 "-.*"
public class FileNameFilter {
public static void main(String[] args) {
final String filesLocation = "resources/";
try {
Files.walk(Paths.get(filesLocation))
.filter(p -> p.getFileName().toString().matches("^([1][0-9]{0,1}|2[0]{0,1})-.*"))
.forEach(entry -> {System.out.printf("%-5s: %s%n", "entry", entry.getFileName());});
} catch (IOException e) {
// add your exception handling here
e.printStackTrace(System.err);
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2
列出目录中前20个文件的示例.
note注意文件的顺序与运行ls或dir在目录中的顺序相同.
Java 7的例子
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileListLimiter {
private static final int MAX_FILES_TO_LIST = 20;
public static void main(String[] args) {
final String filesLocation = "resources/";
Path path = Paths.get(filesLocation);
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(path)) {
int fileCounter = 1;
for (Path entry : dirStream) {
System.out.printf("%-5s %2d: %s%n", "entry", fileCounter++, entry.getFileName());
if (fileCounter > MAX_FILES_TO_LIST) {
break;
}
}
} catch (IOException e) {
// add your exception handling here
e.printStackTrace(System.err);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Java 8的例子
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class FileListLimiter {
private static final int MAX_FILES_TO_LIST = 20;
public static void main(String[] args) {
final String filesLocation = "resources/";
try {
Files.walk(Paths.get(filesLocation))
.filter(p -> p.toFile().isFile())
.limit(MAX_FILES_TO_LIST)
.forEach(entry -> {System.out.printf("%-5s: %s%n", "entry", entry.getFileName());});
} catch (IOException e) {
// add your exception handling here
e.printStackTrace(System.err);
}
}
}
Run Code Online (Sandbox Code Playgroud)