title: Java PrintWriter - How do you split into a number of documents depending on how many lines the user wants?
body: <pre><code>public class FileSplitter {
  
      public static void main(String[] args) throws FileNotFoundException {
  
          // Enter the number of lines you would like in each document
          Scanner scan = new Scanner(System.in);
          System.out.println(
                  "Enter the number of lines you would like in each document(please note any remainders will go into a document of their own) ");
  
          // variables
          int numberOfLinesPerDocument = Integer.parseInt(scan.nextLine());
          int lineCounter = 0;
  
          int increase = 0;
          String lineBreak = String.valueOf(increase);
          File file = new File("test1.txt");
  
          File inputFile = getInputFileFromUser();
  
          try (Scanner fileScanner = new Scanner(inputFile)) {
  
              try (PrintWriter thePrintWriter = new PrintWriter(file)) {
                  while (fileScanner.hasNextLine()) {
                      String line = fileScanner.nextLine();
                      lineCounter++;
                      thePrintWriter.println(line);
                      if (lineCounter == numberOfLinesPerDocument) {
                          lineCounter = 0;
  
                          increase++;
                          thePrintWriter.close();
                          lineBreak = String.valueOf(increase);
                          file = new File("testing" + lineBreak + ".txt");
                      }
                  }
              }
          } catch (IOException ex) {
              System.out.println("System Failure: ");
          }
  
      }
  
      @SuppressWarnings("resource")
      private static File getInputFileFromUser() {
          Scanner userInput = new Scanner(System.in);
          System.out.print("Please enter path to input file path &gt;&gt;&gt; ");
          String path = userInput.nextLine();
  
          File inputFile = new File(path);
          if (inputFile.exists() == false) {
              System.out.println(path + " does not exist");
              System.exit(1);
          } else if (inputFile.isFile() == false) {
              System.out.println(path + " is not a file");
              System.exit(1);
          }
          return inputFile;
      }
  }
  </code></pre>
  
  <p>Please help me split this file into multiple files.  I have tried many things and none of them work.  So if i give the program 5 lines and the document has 500... that should split out 100 documents randomly.  I am only able to get one document to print with all 500 lines.  Please help, thanks.</p>