0

I have recently started learning about file handling in java. However, in this code (down below), I am trying to close the file at the end of all the reading and writing but am facing an error in doing it this way.

package trycatch;

import java.util.Scanner;
import org.omg.CORBA.DataInputStream;

import java.*;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Writer;

public class Source {

public static void main(String[] args) throws Exception {

    Scanner input = new Scanner(System.in);
    try {
    File f = new File("record.txt");

    FileOutputStream writing = new FileOutputStream(f);
    DataOutputStream write = new DataOutputStream(writing);
    write.writeUTF("What are the things that you want to do");

    String str;

    FileInputStream reading = new FileInputStream(f);
    java.io.DataInputStream read = new java.io.DataInputStream(reading);
    str = read.readUTF();
    System.out.println(str);
    }
    catch(FileNotFoundException e) {

        System.out.println("The system collapsed");
    }
    finally {
        write.close(); // write cannot be resolved
        read.close();  // read cannot be resolved
    }

    input.close();
}
}

I am trying out the finally keyword but can you tell me why my IDE cannot recognize read and write when I write it there?

write cannot be resolved

  • Despite the question got closed as duplicate. Would you mind to accept the answer that helped most? – dpr Jul 26 '17 at 07:20

4 Answers4

1

You are declaring write inside the try-block. It can't be resolved inside the finally block as this is a different scope.

You need to declare write before the try-block to make it accessible in finally:

DataOutputStream write = null;
try {
...
    write = new DataOutputStream(writing);
...
} finally {
    if (write != null) {
        write.close();
    }
}

With recent versions of Java you could/should use the try-with-resource construct to ensure proper resource handling. With this you can omit the finally-block and the JVM will take care of closing your resources when the try-block is left:

try (DataOutputStream write = new DataOutputStream(writing)) {
    ...
}
dpr
  • 10,591
  • 3
  • 41
  • 71
1

Your read and write fields are local to try block, finally can't access then.Initialize it outside of try.

leo017
  • 45
  • 9
1

Try it like that:

package trycatch;

import java.util.Scanner;
import org.omg.CORBA.DataInputStream;

import java.*;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Writer;

public class Source {

public static void main(String[] args) throws Exception {

    Scanner input = new Scanner(System.in);
    DataOutputStream write = null;
    java.io.DataInputStream read = null;
    try {
    File f = new File("record.txt");

    FileOutputStream writing = new FileOutputStream(f);
    write = new DataOutputStream(writing);
    write.writeUTF("What are the things that you want to do");

    String str;

    FileInputStream reading = new FileInputStream(f);
    read = new java.io.DataInputStream(reading);
    str = read.readUTF();
    System.out.println(str);
    }
    catch(FileNotFoundException e) {

        System.out.println("The system collapsed");
    }
    finally {
        if (write != null)
          write.close(); // write cannot be resolved
        if (read != null)
          read.close();  // read cannot be resolved
    }

    input.close();
}
}
Garlic
  • 66
  • 4
0

write and read are created in the try block and their scope is only in the block. Move the declaration where you are declaring input and it should work.

Arpit
  • 323
  • 4
  • 13