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 Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

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 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)

3 Likes

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);

2 Likes

(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:

1 Like

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.

6 Likes

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.

2 Likes

Yeah one of the weird lesson explanation I am sorry. The need of creating a counter variable was not mentioned in the lesson. In the exercise we suddenly are to create a counter var. Why ?

And also , since maybe they are in same package/class , We created a FileReader object, not FileInputStream object. task 4 says close FileInputStream which we didn’t create. of course i used FileReader object but still, it feels lousy.

And while((counterVar = inputFile.read()) > 0) also works, and It didn’t create infinite loop.

I don’t know if Codecademy has any of their instructors reading this, but I would highly suggest re-writing the solution for this specific lesson. You present one solution in the lecture text then expect your students, who are supposed to be learning about this for the first time and thus won’t tend to deviate from what you teach them, to come up with a solution that is not mentioned at all during the lecture text. At least mention the second possible solution somewhere in the text or allow for the answer you show to the problem to be the solution.

Also, does anyone know if there is a difference between while(fileStreamVarName.ready()) and while((counterVar = fileStreamVarName.ready()) != -1) that would cause you to use the longer version with a counter variable? Is it more efficient for the system to use int counters over a boolean condition?

In step 3, it says " You can loop through a FileInputStream object using a counterVar as follows:". I think it should be changed to FileReader instead of FileInputStream