yg1a20 2020-12-04 04:38 采纳率: 7.1%
浏览 22

可以教一下我这个该怎么弄吗

Create a class called FlashCardReader. Import BufferedReader and FileReader. Declare a BufferedReader instance variable called reader.

The FlashCardReader constructor should have a string parameter to provide a filename and it should create a BufferedReader which reads from a file with the given filename. Compile. You would (should!) be alerted to a problem that the compiler has found.

Surround the line(s) with an appropriate try-catch block of code. You'll find the correct exception name on the compiler message. You'll need to import the Exception. In the catch block print out a helpful error message.

Write a method called getLine in the class. Have it return the next line of the contents of the file. Have a look at the API and the methods in BufferedReader to do this. (This is to test that your file reading is working. You may later change this, to make it work with your code in Parts 2 & 3).

Write a boolean method fileIsReady to find out whether the file is ready to be read. Use a method from the BufferedReader class. It should return false if there is some other reason why the FlashCardReader is unable to provide data, for example if there was a problem creating the BufferedReader or FileReader.

  • 写回答

1条回答 默认 最新

  • 陆卿之 2023-05-10 13:41
    关注

    译文:
    创建一个名为FlashCardReader的类。导入BufferedReader和FileReader。声明一个BufferedReader实例变量reader。

    FlashCardReader构造函数应该有一个字符串参数来提供一个文件名,它应该创建一个BufferedReader来读取一个给定文件名的文件。编译。您会(应该)收到编译器发现的问题的警报。

    用适当的try-catch代码块包围行。您将在编译器消息中找到正确的异常名称。您需要导入Exception。在catch块中打印出有用的错误信息。

    在类中编写一个名为getLine的方法。让它返回文件内容的下一行。看一下BufferedReader中的API和方法。(这是为了测试你的文件读取工作。稍后您可以对其进行更改,使其与第2部分和第3部分中的代码一起工作。

    编写一个布尔方法fileIsReady来确定文件是否准备好被读取。使用BufferedReader类中的方法。如果有其他原因导致FlashCardReader无法提供数据,例如创建BufferedReader或FileReader时出现问题,则应该返回false。

    package com.sin.test;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    //创建一个FlashCardReader类
    public class FlashCardReader {
        //声明BufferedReader变量
        private BufferedReader reader;
    
    
        /**
         * 构造方法
         * @param filename 读取的文件
         */
        public FlashCardReader(String filename) {
            try {
                reader = new BufferedReader(new FileReader(filename));
            } catch (IOException e) {
                //打印错误信息
                System.out.println("错误: " + e.getMessage());
            }
        }
    
        /**
         * getLine方法
         * @return 返回文件的一下内容
         */
        public String getLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                System.out.println("错误: " + e.getMessage());
                return null;
            }
        }
    
        /**
         * 编写boolean类型fileIsReady方法来确定文件是否准备好被读取
         * @return
         * @throws IOException
         */
        public boolean fileIsReady()  {
            try {
                return reader != null && reader.ready();
            } catch (IOException e) {
                // 如创建BufferedReader或者FileReader时出现错误,则打印异常,返回false;
                System.out.println("错误: " + e.getMessage());
                return false;
            }
        }
    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作