troubleshooting Question

Java: Error when reading a file

Avatar of isames
isames asked on
Java
2 Comments1 Solution128 ViewsLast Modified:
All,

I'm trying to read and output the contents of a file called credentials.txt. The code is below and attached is the error I'm getting.

package testingrandommethods;
import java.util.Scanner;
import java.io.*;

public class TestingRandomMethods {

   
    public static void main(String[] args) throws IOException {    //I had an error until I added "throws IOException" when trying to open the file.
         
        FileInputStream fileByteStream = null;
        Scanner credFile = null;
        String credData = "";

        //Attempt to open file
        fileByteStream = new FileInputStream("credentials.txt");
        credFile = new Scanner(fileByteStream); //credFile now contains what's in credentials.txt.
        System.out.println("Reading and printing data.");
        credData = credFile.next(); //credData now contains what's in credentials.txt as well. It allows me to read through the file.
       
        while (credData.charAt(0)){
            System.out.println(credData);
           
            credData = credFile.next();
        }
        System.out.println(credData);
       
        //Close the file
        System.out.println("Closing the credential.txt file.");
        fileByteStream.close();
        return;
    }
   
}

ErrorMessage.png
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros