当前位置:   article > 正文

通过代码创建一个文件a.txt 然后写一个方法,控制台循环输入内容,然后通过gbk的编码格式保存到 a.txt中(要求可以追加),直到输入exit结束 再写一个方法,要求用字符缓冲流读取a.tx_(1)创建一个txt文件,写入1000个“a”字符,并打印出来; (2)修改文件第100至199的字

(1)创建一个txt文件,写入1000个“a”字符,并打印出来; (2)修改文件第100至199的字

通过代码创建一个文件a.txt

然后写一个方法,控制台循环输入内容,然后通过gbk的编码格式保存到

a.txt中(要求可以追加),直到输入exit结束

再写一个方法,要求用字符缓冲流读取a.txt中的内容到控制台打印出来

package com.Work1;

import org.junit.Test;

import java.io.*;
import java.util.Scanner;

/**
 * @Author: 廾匸
 * @Date: 2020/12/4 18:24
 * @Description:
 * @version: 1.01
 */
public class Dome {
    @Test
    public void test01() throws IOException {
        File file = new File("G:/作业/a.txt");
        file.createNewFile();
       input(file);
       show(file);

    }
    public static void input(File file) throws IOException {
        while (true) {
            Scanner in = new Scanner(System.in);
            System.out.println("请输入:");
            String str = in.nextLine();
            if (str.equals("exit")) {
                break;
            }
            OutputStreamWriter writer = new OutputStreamWriter(
                    new FileOutputStream(file, true), "gbk");
            writer.write(str + "\n");
            writer.flush();
        }
    }
    public void show(File file) throws IOException {
        if(!file.exists()){
            return;
        }
        InputStream input = new BufferedInputStream(new FileInputStream(file));
        InputStreamReader reader = new InputStreamReader(input,"gbk");
        int len;
        while((len = reader.read()) != -1){
            System.out.print((char)len);
        }
        reader.close();
    }
}
  • 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
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/292914
推荐阅读
相关标签
  

闽ICP备14008679号