IO流练习题

题1:

1.判断D盘work文件夹中是否有mytemp文件夹,如果没有就创建mytemp文件夹

2.在d:\work\mytemp目录下,创建文件hello.txt,若hello.txt已经存在。提示该文件已经存在,就不要在重复创建

3.并且在hello.txt文件中,写入hello-world~~

/*题目:
*1.判断D盘work文件夹中是否有mytemp文件安家,如果没有就创建mytemp文件夹
*2.在d:\\work\\mytemp目录下,创建文件hello.txt,若hello.txt已经存在。提示该文件已经存在,就不要在重复创建
*3.并且在hello.txt文件中,写入hello-world~~~
* */
​
//1.判断D盘work文件夹中是否有mytemp文件夹,如果没有就创建mytemp文件夹
//1.1)设置文件夹的位置
String fileName = "d:\\work\\mytemp";
//1.2)创建File对象
File file = new File(fileName);
​
//1.3)判断该文件夹是否存在
if(file.exists()){
    System.out.println("mytemp该文件夹存在...");
}else{
    System.out.println("mytemp文件夹不存在...");
    System.out.println("创建该文件夹...");
​
    //1.4)若文件夹不存在,则创建该文件夹
    if(file.mkdirs()){
        System.out.println("mytemp文件夹创建成功...");
    }else {
        System.out.println("mytemp文件夹创建失败...");
    }
​
}
​
//2.在d:\\work\\mytemp目录下,创建文件hello.txt
//2.1)设置文件创建的地址
String fileName02 ="d:\\work\\mytemp\\hello.txt";
//2.2)创建File对象
File file1 = new File(fileName02);
​
try {
    //2.3)判断文件是否存在
    if (file1.exists()){
        System.out.println("hello.txt文件已经存在,请不再重建...");
​
        //3.并且在hello.txt文件中,写入hello-world~~~
​
        BufferedWriter bw = new BufferedWriter(new FileWriter(fileName02));
        bw.write("hello-world~~~");
        bw.close();
​
    }else {
        //2.4)若未创建该文件,则创建
        file1.createNewFile();
        System.out.println("hello.txt文件创建成功...");
    }
} catch (IOException e) {
    e.printStackTrace();
}

题2:

使用BufferedReader读取一个文本文件,为每一行加上行号,

再连同内容一并输出到屏幕上;

/*题目:
* 使用BufferedReader读取一个文本文件,为每一行加上行号,
* 再连同内容一并输出到屏幕上
* */
​
//1.设置读取文件的地址
String fileName = "d:\\work\\feature.txt";
//2.创建对象
BufferedReader br = new BufferedReader(new FileReader(fileName));
​
//3.接收每行的数据
String line = " ";
//4.记录所处的行数
int num = 0;
​
//5.循环接收
while ((line = br.readLine()) != null){
​
        num++;
        System.out.println(num + ":" + line);
}
​
//6关闭流
br.close();

题3:

编写一个dog.properties

内容:{name=tom;age=5;color=red}

2.编写一个Dog类(name,age,color)创建一个dog对象

读取dog.properties用相应的内容完成属性初始化并输出

3.将创建的Dog对象,序列化到文件dog.txt文件

/*题目:
* 1.编写一个dog.properties
*   name=tom
*   age=5
*   color=red
* 2.编写一个Dog类(name,age,color)创建一个dog对象
*   读取dog.properties用相应的内容完成属性初始化 并输出
* 3.将创建的Dog对象,序列化到文件dog.txt文件
* */
​
//1.设置需要的文件路径
String filePath = "src\\dog.properties";
//2.创建Properties对象
Properties properties = new Properties();
//3.加载盘配置文件的键值对 到properties类
properties.load(new FileReader(filePath));
​
//4.根据键找出所对应的值 并进行赋给相应的变量
String  name = properties.get("name") + "";
int age = Integer.parseInt(properties.get("age") + "");
String color = properties.get("color") +"";
​
//5.将4中所获得的变量添加到Dog类中
Dog dog = new Dog(name,age,color);
​
System.out.println(dog);
​
//序列化:
​
//6.设置序列化到某文件的路径
String fileName = "d:\\work\\dog.dat";
//7.创建ObjectOutputStream对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
//8.写入dog类信息
oos.writeObject(dog);
​
//9.关闭流
oos.close();
​
System.out.println("序列化完成...");
@Test
//反序列化
public void text01() throws IOException, ClassNotFoundException {
​
    //1.设置需要反序列化的地址
    String fileName = "d:\\work\\dog.dat";
    //2.创建ObjectInputStream对象
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
    //3.读取Dog类的信息,并进行强壮
    Dog dog = (Dog)ois.readObject();
​
    //4.关闭流
    ois.close();
​
    System.out.println("反序列化成功...");
    System.out.println(dog);
class Dog implements Serializable{
​
    private String name;
    private int age;
    private String color;
​
    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }
​
    @Override
    public String toString() {
        return  name + "\t" + age + "\t" + color ;
    }
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jhan&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值