0

I'm trying to initialize an InputStream, but it is not allowing me. I have been able to initialize OutputStream.

public void readData() {
  String fileName = "clinicData.txt";
  Scanner inputStream;
  System.out.println("The file " + fileName +"\ncontains the following line:\n");
  
  try {
      inputStream = new Scanner (new File (fileName));
  } catch (FileNotFoundException e) {
      System.out.println("Error: opening the file " +fileName);
      System.exit(0);
  }
  while (inputStream.hasNextLine()) {
      String line = inputStream.nextLine();
      System.out.println (line);
  }
  inputStream.close();
}

The code above is the section I am working with if you need me to post the other section with the outputStream just tell me.

Nikolas Charalambidis
  • 29,749
  • 7
  • 67
  • 124
Zachary
  • 31
  • 8
  • Well, you aren't initializing an `InputStream` but a `Scanner`. Is the question how to replace this code with code that uses an `InputStream`? Or is the usage of a `Scanner` intended and the code doesn't work? If it's the latter, what exactly is the problem? Are you getting an error? The wrong results? – Mureinik May 20 '21 at 09:18
  • 1
    You should get your naming right to avoid confusion. Scanners and input streams are different things. You also should define "it is not allowing me", don't make us guess. – Thomas May 20 '21 at 09:18
  • You're not trying to create an `InputStream`, you're creating a `Scanner`. You should be posting the error you get; your main problem seems to be variable scope though, because you read and close the scanner outside the scope it's initialized. – daniu May 20 '21 at 09:20
  • You can find the answer here: https://stackoverflow.com/questions/28594252/how-to-initialize-an-inputstream-before-try-catch-block – Remus Crisan May 20 '21 at 09:20
  • The program does not work as it is an error straight up and I was told to use both Scanner and inputStream – Zachary May 20 '21 at 09:21
  • Does this answer your question? [How to Initialize an InputStream Before Try/Catch Block](https://stackoverflow.com/questions/28594252/how-to-initialize-an-inputstream-before-try-catch-block) – Remus Crisan May 20 '21 at 09:21
  • @RemusCrisan I have looked at that it did not help – Zachary May 20 '21 at 09:22

2 Answers2

1

You want to do this:

try (Scanner inputStream = new Scanner(new File(fileName))) {
    while (inputStream.hasNextLine()) {
        String line = inputStream.nextLine();
        System.out.println(line);
    }
} catch (FileNotFoundException e) {
    System.out.println("Error: opening the file " +fileName);
    System.exit(0);
}
  • The Scanner inputStream; is initialized in the try block, however, it is not guaranteed the initialization succeeds and you would try to access such instance in the while loop later, which would be erroneous. Move the while loop inside the try block.
  • Scanner is AutoCloseable, hence you can use try-with-resources and omit inputStream.close()
Nikolas Charalambidis
  • 29,749
  • 7
  • 67
  • 124
0

You can try getting the file in FileInputStream and then read that using Scanner as shown below

try  
{  
    FileInputStream fis=new FileInputStream("clinicData.txt");       
    Scanner sc=new Scanner(fis);

    while(sc.hasNextLine())  
        {  
          System.out.println(sc.nextLine());
        }  
    sc.close(); 
}  
catch(IOException e)  
{  
    e.printStackTrace();  
}
SSharma2203
  • 163
  • 1
  • 12