FAQ: Input and Output: Lesson - FileInputStream

This community-built FAQ covers the “FileInputStream” exercise from the lesson “Input and Output: Lesson”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Intermediate Java

FAQs on the exercise FileInputStream

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I assume that this sets i to be the next integer read from the file, and then checks whether that integer is not -1.

    while((i = inputFile.read()) != -1) {    
      System.out.print((char)i);
      j++; 
    }

which would be the same as doing:

    i = inputFile.read();
    while(i != -1) {
      System.out.print((char)i);
      i = inputFile.read();
    }

How would I read to contents of a file into a string?

The best I could do was:

    FileInputStream inStream = new FileInputStream(path);
    int i = 0;
    int j = 0;
    
    // get length first
    while((i = inStream.read()) != -1) {    
      j++; 
    }
    inStream.close();
    //j++;
    // j is the length
    
    // make an array of chars
    char[] charray = new char[j];
    FileInputStream inStream2 = new FileInputStream(path);
    for (int k = 0; k < j; k++) {
      i = inStream2.read();
      charray[k] = (char)i;  // error? since i can be negative?
    }
    inStream2.close();
    // make the array into a string
    String str = new String(charray);

I guess the assumption is that characters are encoded as one byte in the text file (UTF-8 or ASCII or something like that).

EDIT: I found a constructor for strings that uses bytes, so I guess its better to use that here.

modified code
    FileInputStream inStream = new FileInputStream(path);
    int i = 0;
    int j = 0;
    
    // get length first
    while((i = inStream.read()) != -1) {    
      j++; 
    }
    inStream.close(); 
    // j is the length
    
    // make an array of bytes
    byte[] bytes = new byte[j];
    FileInputStream inStream2 = new FileInputStream(path);
    for (int k = 0; k < j; k++) {
      i = inStream2.read();
      bytes[k] = (byte)i; // warning?
      // conversion warning because bytes use less space than ints?
    }
    inStream2.close();
    // make the array into a string
    String str = new String(bytes);

The task includes “throwing an IOException”, but Codecademy had never taught me how to do it or what it is in the first place (either in Learn Java or previous steps of Learn Intermediate Java)

1 Like

I’m disappointed that this part wasn’t clearly explained. Why should it be (not) equal to -1? What is (char), exactly? Not the datatype itself, but putting it in front of a variable in parentheses

int i = 0;
// A loop to access each character
while((i = inputFile.read()) != -1) {
// Printing each character as it’s reached
System.out.print((char)i);

(char) x would cast variable x as a char, meaning it would try to get the char version of what’s in x.

1 Like

I would appreciate if Codecademy told me about it in some degree of detail (at least, at some point). I don’t even know how to google it: I don’t know what this practice is called! :anguished:

Expecting someone to finish the last step of FileReader without looking at the solution honestly seems slightly naive.

The learn/description/explanation stuff uses this example on how to print each character in the file:

while (reader.ready()) {
  System.out.print((char) reader.read());
}

Though the solution clearly works, you will continue to fail the task until you somehow come up with the following solution:

int counter = 0;
while ((counter = reader.read()) != -1) {
  System.out.print((char)counter);
}

…which is not mentioned or explained anywhere. Nowhere was there ever mention of need for a counter variable either (apart of course from the “Create a counter variable set to 0” instruction which naturally didn’t make any sense before seeing the solution). It feels like something has been left out.

1 Like

I wanted to add another solution that I found to work (although it is not accepted by the program). Instead of checking if the counter is -1, I used the method ready() which would check if there was more content. I did need to use the (char) x to turn the unicode into a character which I didn’t discover until looking at the hint.

    FileReader fileOb = new FileReader(path);
    // int i = 0;
    while (fileOb.ready()) {
      // i ++;
      System.out.print((char)fileOb.read());
    }

I agree with others in this chat that the instructions and previous material is very vague on what this question wants you to do and how to do it. I encourage the codecademy support staff to revisit this question for more clarity.