Java IO 的几个笔试题

有几道Java IO的题目如下:

题目一:

(1) 在判断e盘下是否有文件夹 mytemp,如果没有就创建 mytemp
(2) 在 e:\mytemp 目录下,创建文件 hello.txt
(3) 如果 hello.txt 已经存在,提示该文件已经存在,就不要再重复创建了。

代码:

/**
 * (1) 在判断e盘下是否有文件夹 mytemp,如果没有就创建 mytemp
 * (2) 在 e:\\mytemp 目录下,创建文件 hello.txt
 * (3) 如果 hello.txt 已经存在,提示该文件已经存在,就不要再重复创建了。
 */
public class Case1 {
    public static void main(String[] args){
        String filePath = "e:\\mytemp";
        File file = new File(filePath);
        //判断目录是否存在
        if(!file.exists()){
            //目录不存在,创建目录
            boolean result = file.mkdir();
            if(result){
                System.out.println("目录创建成功!");
            }else{
                System.out.println("目录创建失败!");
            }
        }
        file = new File(filePath,"hello.txt");
        if(file.exists()){
            System.out.println("hello.txt已存在,无需创建!");
            return ;
        }
        try {
            boolean result = file.createNewFile();
            if (result) {
                System.out.println("hello.txt 创建成功!");
            } else {
                System.out.println("hello.txt 创建失败!");
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

第一次执行main方法输出结果为:

目录创建成功!
hello.txt 创建成功!

并且还创建了文件夹和文件。
在这里插入图片描述
再次执行main方法后:

hello.txt已存在,无需创建!

题目二:

使用 BufferedReader 读取一个文本文件,为每行加上行号,再连同内容一并输出到屏幕上。

代码:

/**
 * (1) 使用 BufferedReader 读取一个文本文件,为每行加上行号,再连同内容一并输出到屏幕上。
 */
public class Case2 {
    public static void main(String[] args) throws IOException {
        String filePath = "e:\\case2.txt";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
        String readData;
        int i=0;
        while( (readData = bufferedReader.readLine())!=null){
            i++;
            System.out.println(i+". "+readData);
        }
        bufferedReader.close();
    }
}

e:\case2.txt 内容如下图所示:
在这里插入图片描述

输出结果为:

1. 窗外的麻雀在电线杆上多嘴
2. 你说这一句很有夏天的感觉
3. 手中的铅笔在纸上来来回回
4. 我用几行字形容你是我的谁
5. 秋刀鱼的滋味猫跟你都想了解
6. 初恋的香味就这样被我们寻回
7. 那温暖的阳光像刚摘的鲜艳草莓
8. 你说你舍不得吃掉这一种感觉
9. 雨下整夜我的爱溢出就像雨水
10. 院子落叶跟我的思念厚厚一叠
11. 几句是非也无法将我的热情冷却
12. 你出现在我诗的每一页
13. 雨下整夜我的爱溢出就像雨水
14. 窗台蝴蝶像诗里纷飞的美丽章节
15. 我接着写
16. 把永远爱你写进诗的结尾
17. 你是我唯一想要的了解
18. 雨下整夜我的爱溢出就像雨水
19. 院子落叶跟我的思念厚厚一叠
20. 几句是非也无法将我的热情冷却
21. 你出现在我诗的每一页
22. 那饱满的稻穗幸福了这个季节
23. 而你的脸颊像田里熟透的番茄
24. 你突然对我说七里香的名字很美
25. 我此刻却只想亲吻你倔强的嘴
26. 雨下整夜我的爱溢出就像雨水
27. 院子落叶跟我的思念厚厚一叠
28. 几句是非也无法将我的热情冷却
29. 你出现在我诗的每一页
30. 整夜我的爱溢出就像雨水
31. 窗台蝴蝶像诗里纷飞的美丽章节
32. 我接着写
33. 把永远爱你写进诗的结尾
34. 你是我唯一想要的了解

题目三

(1) 要编写一个 dog.properties,name = tome; age = 5; color = red
(2) 编写 Dog 类(name,age,color) 创建一个 dog对象,读取 dog.properties ,用相应的内容完成属性初始化,并输出。

代码:

/**
 * (1) 要编写一个 dog.properties
 * name = tome; age = 5; color = red
 *
 * (2) 编写 Dog 类(name,age,color) 创建一个 dog对象,读取 dog.properties ,
 * 用相应的内容完成属性初始化,并输出。
 */
public class Case3 {
    public static void main(String[] ars) throws IOException {
        Properties properties = new Properties();
        String propertiesPath = "e:\\dog.properties";
        //将属性写入到properties文件中
        properties.setProperty("name","Tom");
        properties.setProperty("age","5");
        properties.setProperty("color","black");
        properties.store(new FileOutputStream(propertiesPath),null);
        System.out.println("保存成功");

        //将属性加载到 properties对象中
        Properties properties1 = new Properties();
        properties1.load(new FileReader(propertiesPath));
        String name = (String) properties1.get("name");
        String age = (String)properties1.get("age");
        String color = (String) properties1.get("color");
        Dog dog = new Dog(name,age,color);
        System.out.println("dog = "+dog.toString());
    }
}

@Data
class Dog{
    private String name;
    private String age;
    private String color;

    public Dog(String name,String age,String color){
        this.name = name;
        this.age = age;
        this.color = color;
    }
}

执行main方法后,先创建 e:\dog.properties 文件
在这里插入图片描述
然后读取它。控制台输出结果为:

保存成功
dog = Dog(name=Tom, age=5, color=black)

以上就是 JavaIO的 几个笔试题 全部内容,感谢阅读!

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值