Hi HashCode. An interesting question, help make such a program, if not difficult, of course. Write 1 it prints the first ten files, write 2 it prints the next ten, and so on until the files run out in the directory (they are taken from the folder, regular txt files) . In general, like this). Here's a code to help you, although I don’t know if it can help:

import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; public class page { public static void main(String[] args) { String s = null; File file = new File("C:\\prov"); String list[] = file.list(); int len = new File("C:\\prov\\").list().length; ArrayList<String> pages = new ArrayList<String>(); for (int i = 1; i <= len; i++) { for (int itt = 0; itt < list.length; itt++) { File fileread = new File("C:\\prov\\" + list[itt]); BufferedReader brl = null; try { brl = new BufferedReader(new InputStreamReader( new FileInputStream(fileread), "UTF-8")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // pages.add("page-" + i); try { while ((s = brl.readLine()) != null) { pages.add(s); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } paginate(pages, 10); }// main end static <T> void paginate(List<T> pages, int pagesPerOut) { for (int i = 0; i < pages.size(); i += pagesPerOut) { System.out.println(paginate(pages, i, pagesPerOut)); System.out.println("=============================="); } } static <T> List<T> paginate(List<T> pages, int start, int count) { int end = start + count > pages.size() ? pages.size() : start + count; return pages.subList(start, end); } } 

Of course, I understand that they will not write code like this, but there may be good people who are ready to help.

  • four
    Brilliant question ... - Barmaley
  • Aha)) I now wonder how to do this in Java? - vanekk1
  • @Barmaley not help? - vanekk1
  • And what have you got here? - nitrocaster
  • I didn’t know how to make the first ten files appear when entering (this is not a problem), their contents - vanekk1

1 answer 1

That sketched maybe help. Without exception handling, a very raw version. Pages starting from scratch.

 import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class GetFileList { public static void main(String args[]) { TxtFiles file = new TxtFiles("D:\\"); //Путь к папке Scanner sc = new Scanner(System.in); int pageNum = 0; do { System.out.println("Please enter the page(Beginning from 0, -1 for exit):"); pageNum = sc.nextInt(); file.printPage(pageNum); } while (pageNum != -1); System.out.println("GoodBY!"); } } class TxtFiles { private File file; private String pathToTxtFiles; private File[] files; private List<String> txtFiles; public TxtFiles(String path) { pathToTxtFiles = path; file = new File(path); files = file.listFiles(); txtFiles = new ArrayList<String>(); importOnlyTxtFiles(); } private void importOnlyTxtFiles() { for (File fl : files) { if (fl.toString().endsWith(".txt")) txtFiles.add(fl.toString()); } } public void printPage(int numOfPage) { if (numOfPage == -1) return; if (numOfPage < 0 || numOfPage > txtFiles.size() / 10) { System.out.println("Wrong page!"); return; } int limit = (numOfPage < txtFiles.size() / 10) ? numOfPage * 10 + 10 : txtFiles.size(); for (int i = numOfPage * 10; i < limit; i++) { System.out.println(txtFiles.get(i)); } } } 
  • Thank you very much, but could you suggest an approximate algorithm for implementing this in a servlet, that is, the page numbers should be links - vanekk1
  • one
    If you have time to understand, can you [here] [1]? [1]: primefaces.org/showcase/ui/datatablePagination.jsf - sonniy