Guest User

Untitled

a guest
Apr 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. package com.psv.javaoop.lesson11.task1;
  2.  
  3. import java.io.*;
  4.  
  5. public class FileLineNumerator {
  6.  
  7. private final String inFileName;
  8. private final String outFileName;
  9.  
  10. public FileLineNumerator(String inFileName, String outFileName) {
  11. this.inFileName = inFileName;
  12. this.outFileName = outFileName;
  13. }
  14.  
  15. public void runLineNumeration() {
  16. try (BufferedWriter writer = new BufferedWriter(new FileWriter(outFileName))) {
  17. BufferedReader reader = new BufferedReader(
  18. new FileReader(inFileName));
  19. String l;
  20. int i = 1;
  21. while ((l = reader.readLine()) != null) {
  22. writer.write(i + ". " + l);
  23. writer.newLine();
  24. System.out.println(i + ". " + l);
  25. i++;
  26. }
  27. reader.close();
  28. writer.close();
  29.  
  30. } catch (FileNotFoundException e) {
  31. System.out.println("Sorry, file is not found: " + e.getMessage());
  32. } catch (IOException e) {
  33. System.out.println("Sorry, IO error: " + e.getMessage());
  34.  
  35. }
  36. }
  37. }
Add Comment
Please, Sign In to add comment