Advertisement
desislava_topuzakova

5. Line Numbers

Jan 22nd, 2022
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. package StreamsFilesDirectories_Exercise;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.nio.file.Files;
  8. import java.nio.file.Path;
  9. import java.util.List;
  10.  
  11. public class LineNumbers_05 {
  12.     public static void main(String[] args) throws IOException {
  13.         //1. прочитаме всички редове
  14.         //2. обхождаме всеки един от редовете -> отпечатвам го с номер на реда
  15.         String path = "C:\\Users\\I353529\\Desktop\\04. Java-Advanced-Files-and-Streams-Exercises-Resources\\inputLineNumbers.txt";
  16.         List<String> allLines = Files.readAllLines(Path.of(path));
  17.         PrintWriter writer = new PrintWriter("output_line_numbers.txt");
  18.         int number = 1;
  19.         for (String line : allLines) {
  20.             writer.println(number + ". " + line);
  21.             number++;
  22.         }
  23.  
  24.         writer.close(); //спирам да пиша във файла и файлът се затваря
  25.     }
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement