打开文件并执行文件输入 [Java]

发布于 2025-01-14 06:01:29 字数 1503 浏览 4 评论 0原文

此作业的目标是创建一个 while 循环,该循环遍历文件“flowers.dat”(我将在本文后面粘贴它),直到到达 EoF,然后它应该打印花的名称,如果它'会在阴凉处或阳光下生长。这是我到目前为止所得到的:

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.

}

这是“flowers.dat”。在这里,我注意到 Flower 和 sun/shade 是交替的,所以这让我觉得我需要在 while 循环中包含一个 for 循环,在每一行之间交替,将一行分配给flowerName,另一行分配给 sunOrShade,然后打印该行再次执行,直到达到 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

此外,我收到此错误消息。我不确定为什么它不关闭 .dat 文件

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城月光淡如水﹏ 2025-01-21 06:01:29

类 File 仅用于类路径,它与打开任何流无关。在作为 File 对象的 Flower 实例上调用 close 方法无效。

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.

宫墨修音 2025-01-21 06:01:29

flower 被声明为 File 对象,并且该对象没有 close() 方法。您需要初始化 BufferedReader (reader)

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

并关闭它,因为它将访问并读取文件。它应该是reader.close()。这应该可以解决您提到的特定错误。但更进一步,这样您就不需要关闭阅读器,并在设置阅读器时使用尝试使用资源,在事情完成或发生异常时允许它自行关闭

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

:不需要 System.exit(0); 来结束应用程序。当main()方法完成时它就会结束。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文