Didart

Write Every Third Line

Jan 25th, 2023
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. package StreamsFilesAndDirectories4;
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5.  
  6. public class WriteEveryThirdLine {
  7.     public static void main(String[] args) throws IOException {
  8.  
  9.         String inPath = "input.txt";
  10.         String outPath = "output.txt";
  11.  
  12.         Scanner in = new Scanner(new FileReader(inPath));
  13.         PrintWriter out = new PrintWriter(new FileWriter(outPath));
  14.  
  15.         int counter = 1;
  16.         String line = in.nextLine();
  17.         while (in.hasNextLine()) {
  18.             if (counter % 3 == 0) {
  19.                 out.println(line);
  20.             }
  21.             counter++;
  22.             line = in.nextLine();
  23.         }
  24.         out.close();
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment