Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.psv.javaoop.lesson11.task1;
- import java.io.*;
- public class FileLineNumerator {
- private final String inFileName;
- private final String outFileName;
- public FileLineNumerator(String inFileName, String outFileName) {
- this.inFileName = inFileName;
- this.outFileName = outFileName;
- }
- public void runLineNumeration() {
- try (BufferedWriter writer = new BufferedWriter(new FileWriter(outFileName))) {
- BufferedReader reader = new BufferedReader(
- new FileReader(inFileName));
- String l;
- int i = 1;
- while ((l = reader.readLine()) != null) {
- writer.write(i + ". " + l);
- writer.newLine();
- System.out.println(i + ". " + l);
- i++;
- }
- reader.close();
- writer.close();
- } catch (FileNotFoundException e) {
- System.out.println("Sorry, file is not found: " + e.getMessage());
- } catch (IOException e) {
- System.out.println("Sorry, IO error: " + e.getMessage());
- }
- }
- }
Add Comment
Please, Sign In to add comment