BufferedReader and BufferedWriter problem in JAVA

0

I am having problems with a program, I need the program to process 10 numbers written in txt down and write me if they are correct or not. The error is as follows:

  

Exception in thread "main" java.io.IOException: Stream closed
  at java.io.BufferedWriter.ensureOpen (Unknown Source)
  at java.io.BufferedWriter.write (Unknown Source)
  at java.io.Writer.write (Unknown Source)
  at NifCheck.main (NifCheck.java:18)

Here I leave all the classes:

import java.io.*;

public class NifCheck {

public static void main(String[] args) throws IOException {
    FileReader fr = new FileReader("C:\Users\Pablo\Desktop\NIF.txt");
    BufferedReader leer = new BufferedReader(fr);
    FileWriter fw = new 
    FileWriter("C:\Users\Pablo\Desktop\NIFCheck.txt");
    BufferedWriter escribir = new BufferedWriter(fw);
    GetNum numeronif = new GetNum();
    calc cogerletra = new calc();
    Check checkeo = new Check();
    for (int x = 1;x <=10;x++) {
        String leidostr = leer.readLine();
        int num = numeronif.num(leidostr);
        String letra = cogerletra.letra(num);
        System.out.println("Llego a escribir");
        escribir.write(leidostr + letra);
        escribir.newLine();
        escribir.close();
    }
}
}
public class GetNum {

int num(String x) {
    int largo;
    largo = x.length();
    String num = x.substring(0, (largo-1));
    int num2;
    num2 = Integer.parseInt(num);
    return num2;
}
}
public class calc {
String letra(int x) {
    String letras = "TRWAGMYFPDXBNJZSQVHLCKE";
    String a = letras.substring(x%23,x%23+1);
    return a;
}
}
public class Check {
public String Check(int num, String letra, String letrastr) {
    String codfinal = String.valueOf(num) + letra;
    if (codfinal.equals(letrastr)) return "      Correcto";
    else return "      Incorrecto";
}
}
    
asked by KYG0H Gaming 27.11.2018 в 20:06
source

2 answers

0

It happens that within the iteration in the first reading you are closing the stream with

escribir.close();

place it after the for cycle.

    
answered by 28.11.2018 в 14:55
0

Readers and writers should not be closed by hand, but delegate their management to the virtual machine using try-with-resources

In short, in a try you can create elements that implement Closeable (your readers and writers do it), and Java takes care of closing them once you reach the end of the try block.

Your main should be like this:

public static void main(String[] args) throws IOException {
    try (FileReader fr = new FileReader("C:\Users\Pablo\Desktop\NIF.txt");BufferedReader leer = new BufferedReader(fr);
    FileWriter("C:\Users\Pablo\Desktop\NIFCheck.txt");BufferedWriter escribir = new BufferedWriter(fw)){
        GetNum numeronif = new GetNum();
        calc cogerletra = new calc();
        Check checkeo = new Check();
        for (int x = 1;x <=10;x++) {
            String leidostr = leer.readLine();
            int num = numeronif.num(leidostr);
            String letra = cogerletra.letra(num);
            System.out.println("Llego a escribir");
            escribir.write(leidostr + letra);
            escribir.newLine();
        }
    }
}
    
answered by 29.11.2018 в 13:55