Home > Net >  Opening Files and Performing File Input [Java]
Opening Files and Performing File Input [Java]

Time:03-14

Goal of this assignment is to create a while loop that goes through file "flowers.dat" (I'll paste it later in this post) until the EoF is reached, then it should print a flowers name and if it'll grow in the shade or sun. Here's what I've got so far:

import java.io.*;  // Import class for file input.

public class Flowers
{
   public static void main(String args[]) throws Exception
   {
      // Declare variables here
      String flowerName;
      String sunOrShade;
      File flower = new File("flowers.dat");
      // Open input file.
      // Create BufferedReader object.

      BufferedReader reader;

      // Write while loop that reads records from file.

      while ((flowerName = reader.readLine()) != null) {
        System.out.println(flowerName   "is grown in the "   sunOrShade);

      } 
      // Print flower name and the words sun or shade.
    

      flower.close();    
      System.exit(0);
   } // End of main() method.

}

Here is the "flowers.dat". Here I noted that the Flower and sun/shade alternate, so it makes me think that I need to include a for loop in the while loop that alternates between each line, assigns one line to flowerName and the other to sunOrShade, then prints the line and it again until it reaches null.

Astilbe
Shade
Marigold
Sun
Begonia
Sun
Primrose
Shade
Cosmos
Sun
Dahlia
Sun
Geranium 
Sun
Foxglove
Shade
Trillium
Shade
Pansy
Sun
Petunia
Sun
Daisy
Sun
Aster
Sun

Also, I'm getting this error message. I'm not sure why it doesn't close the .dat file

Flowers.java30: error : cannot find symbol
flower.close();
      ^
symbol: method close()
location: variable flower of type File
Error: could not find or load main class Flowers

CodePudding user response:

The class File is just for the classpath, it is not about opening any stream. Not valid to call method close on the flower instance which is a File object.

CodePudding user response:

flower is declared as a File object and there is no close() method for this object. You need to initialize your BufferedReader (reader)

BufferedReader reader = new BufferedReader(new FileReader(flower));

and close that since that is what will be accessing the file and reading it. It should be reader.close(). That should solve the particular error you mentioned. But take it a littler further so that you don't need close the reader and allow it to close itself when things are done or an Exception occurs by using Try With Resources when setting up the reader:

File flower = new File("flowers.dat");
try (BufferedReader reader = new BufferedReader(new FileReader(flower))) {
    // Write while loop that reads records from file.
    while ((flowerName = reader.readLine()) != null) {
        //read in the next line to get the required lighting.
        sunOrShade = reader.readLine();
        System.out.println(flowerName   " is grown in the "   sunOrShade);
    }
} 
catch (FileNotFoundException ex) {
    ex.printStackTrace();
} 
catch (IOException ex) {
    ex.printStackTrace();
}

And you don't need System.exit(0); to end the application. It will end when the main() method is finished.

  • Related