How to list files in a file system based on the limit : java -



How to list files in a file system based on the limit : java -

how list files available in file scheme starting number , ending number?

like if there 500 files in c:\test\ how list files starting 1 20

give start number , end number based on list files available particular file path.

i trying in java

i tried thing , gives me files available given path

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(); seek { (int count = 0; count < fileslist.length; count++) { if (!fileslist[count].isdirectory() && fileslist[count].getname().endswith(split_and_save_working_file_extension.trim())) { // load pdf files pdffiles.add(fileslist[count].getname().replace(split_and_save_working_file_extension.trim(), "")); } } } grab (exception filesexception) { filesexception.printstacktrace(); //todo : log exception } { if (fileslist != null) fileslist = null; if (directorylist != null) directorylist = null; } log.debug("in loadallfiles execute end"); homecoming pdffiles; } think question misunderstood, if have 1000 files[file names can anything] , want restrict getting files name give starting number , ending number. 1 20 , want load 20 files alone.

an illustration without external libraries using plain 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 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)) { homecoming false; } homecoming 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); seek (directorystream<path> dirstream = files.newdirectorystream(path, filenamefilter)) { (path entry : dirstream) { system.out.printf("%-5s: %s%n", "entry", entry.getfilename()); } } grab (ioexception e) { // add together exception handling here e.printstacktrace(system.err); } } }

edit java 8 version

// list files starting 1 till 20 "-.*" public class filenamefilter { public static void main(string[] args) { final string fileslocation = "resources/"; seek { 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());}); } grab (ioexception e) { // add together exception handling here e.printstacktrace(system.err); } } }

edit 2 examples list first 20 files in directory. note order of files same run ls or dir in directory.

java 7 example

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); seek (directorystream<path> dirstream = files.newdirectorystream(path)) { int filecounter = 1; (path entry : dirstream) { system.out.printf("%-5s %2d: %s%n", "entry", filecounter++, entry.getfilename()); if (filecounter > max_files_to_list) { break; } } } grab (ioexception e) { // add together exception handling here e.printstacktrace(system.err); } } }

java 8 example

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/"; seek { 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());}); } grab (ioexception e) { // add together exception handling here e.printstacktrace(system.err); } } }

java file file-handling

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -