java-字节流和字符流和Properties练习

目录

前言

       这是我从网上看到的一些题目,自己写了代码,但是只注重了功能,可能有些代码的质量不高,有时间再优化或者修改吧。

题目1

  1. 利用字节输出流一次写一个字节的方式,向D盘的a.txt文件输出字符‘a’
    代码如下:
public class test01 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("day12\\a.txt");
        fileOutputStream.write(88);
        fileOutputStream.close();
    }
}

题目2

  1. :利用字节输出流一次写一个字节数组的方式向D盘的b.txt文件输出内容:“i love java”

代码如下:

import java.io.FileOutputStream;
import java.io.IOException;

public class Test02 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("day12\\a.txt");
        byte[] b = "i love java".getBytes();

        fileOutputStream.write(b);

        fileOutputStream.close();
    }
}

题目3

  1. 有一c.txt 文件中内容为:HelloWorld
    在c.txt文件原内容基础上,添加五句 I love java,而且要实现一句一行操作(注:原文不可覆盖)。
    代码如下:
public class Test03 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("day12\\a.txt",true);
        byte[] b = "i love java\r\n".getBytes();
        byte[] a = "\r\n".getBytes();
        fileOutputStream.write(a);
        for(int i = 0;i < 5;i++){
            fileOutputStream.write(b);
            fileOutputStream.flush();
        }


        fileOutputStream.close();

    }
}

题目4

  1. 利用字节输入流读取文件a.txt的内容,文件内容确定都为纯ASCII字符
    ,使用循环读取,一次读取一个字节,直到读取到文件末尾。将读取的字节输出到控制台
    代码如下:
public class Test04 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("day12\\a.txt");
        int i = -1;
        while ((i =fileInputStream.read()) != -1){
            System.out.println((char)i);
        }
        fileInputStream.close();
    }
}

题目5

  1. 利用字节输入流读取文件b.txt的内容,文件内容确定都为纯ASCII字符
    ,使用循环读取,一次读取一个字节数组,直到读取到文件末尾,将读取到的字节数组转换成字符串输出到控制台。

代码如下;

public class Test05 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("day12\\a.txt");
        byte[] b = new byte[1024];
        int len = 0;
        while((len = fileInputStream.read(b))!= -1){
            System.out.println(new String(b,0,len));;
        }
        fileInputStream.close();
    }
}

题目6

  1. 利用字节流将E盘下的a.png图片复制到D盘下(文件名保存一致)
    代码如下:
public class Test06 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("c:\\Users\\wei\\Pictures\\成功失败法.png");
        FileOutputStream fileOutputStream = new FileOutputStream("day12\\a.png");
        int len = 0;
        while((len = fileInputStream.read()) != -1){
            fileOutputStream.write(len);
        }

        fileInputStream.close();
        fileOutputStream.close();


    }
}

题目7

  1. 请用户从控制台输入信息,程序将信息存储到文件Info.txt中。可以输入多条信息,每条信息存储一行。当用户输入:”886”时,程序结束。

代码如下:

public class Test07 {
    public static void main(String[] args) throws IOException {

        Scanner scanner = new Scanner(System.in);
        FileOutputStream fileOutputStream = new FileOutputStream("day12\\Info.txt",true);
        String s;
        while((s = scanner.next()).equals("886") == false){
            fileOutputStream.write((s+"\r\n").getBytes());
            fileOutputStream.flush();
        }

        fileOutputStream.close();
    }
}

题目8

  1. 我有一个文本文件score.txt,我知道数据是键值对形式的,但是不知道内容是什么。请写一个程序判断是否有"lisi"这样的键存在,如果有就改变其实为"100"
  • score.txt文件内容如下:

      		zhangsan = 90
      		lisi = 80
      		wangwu = 85
    

代码如下:

public class Test08 {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("day12\\score.txt"));

        Set<String> strings = properties.stringPropertyNames();
        for (String key:strings
             ) {
            if(key.equals("list")){
                properties.setProperty("list","100");
            }
        }

        properties.store(new FileOutputStream("day12\\score.txt"),"change");
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值