将控制台输入的内容保存到文件中---

因为控制台输入,所以一般用文本处理就可以了这里是为了复习一下


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

public class Test {
    public static void main(String[] args) throws IOException {
        System.out.println("请输入文本---");
        File file = new File("D:/新建文本文档.txt");
        InputStream in = System.in;
        Scanner sc = new Scanner(in);
        InputStreamReader isr = new InputStreamReader(in);//字节转换为字符
        char []c= new char[1024*8];
        int len = isr.read(c);
        FileWriter fw = new FileWriter(file);
        fw.write(new String(c,0,len));
        fw.close();
        sc.close();
    }
}

如果是使用扫描器进行扫描—


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

public class Test {
    public static void main(String[] args) throws IOException {
        System.out.println("请输入文本---");
        File file = new File("D:/新建文本文档.txt");
        InputStream in = System.in;
        Scanner sc = new Scanner(in);
        InputStreamReader isr = new InputStreamReader(in);//字节转换为字符
       /* char[] c = new char[1024 * 8];
        int len = isr.read(c);*/
        FileWriter fw = new FileWriter(file);
//        String next = sc.next();
        String next = sc.nextLine();
        while(sc.hasNext()) {
            fw.write(next);
            fw.flush();
            next = sc.nextLine();

        }

        fw.close();
        sc.close();
    }
}

但是这种结果会有最由于一行没有写进文件中。


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

public class Test {
    public static void main(String[] args) throws IOException {
        System.out.println("请输入文本---");
        File file = new File("D:/新建文本文档.txt");
        InputStream in = System.in;
        Scanner sc = new Scanner(in);
        InputStreamReader isr = new InputStreamReader(in);//字节转换为字符
       /* char[] c = new char[1024 * 8];
        int len = isr.read(c);*/
        FileWriter fw = new FileWriter(file);
//        String next = sc.next();
        String next = sc.nextLine();
        while(next!=null) {
            fw.write(next);
            fw.flush();
            next = sc.nextLine();

        }

        fw.close();
        sc.close();
    }
}

所以,当next不为空的时候,就解决了上面的问题;

但是又有一个问题,什么时候结束录入呢?


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

public class Test {
    public static void main(String[] args) throws IOException {
        System.out.println("请输入文本---");
        File file = new File("D:/新建文本文档.txt");
        InputStream in = System.in;
        Scanner sc = new Scanner(in);
        InputStreamReader isr = new InputStreamReader(in);//字节转换为字符
       /* char[] c = new char[1024 * 8];
        int len = isr.read(c);*/
        FileWriter fw = new FileWriter(file);
//        String next = sc.next();
        String next = sc.nextLine();
        while(!(next.equals("结束")|next.equals("exit"))) {
            fw.write(next);
            fw.write("\n");
            fw.flush();
            next = sc.nextLine();
        }
        fw.close();
        sc.close();
    }
}

while(!(next.equals(“结束”)|next.equals(“exit”)))我们可以换行后输入结束或者exit来作为结束的标志

其他-----为了加快读取和写入速度可以套上个缓冲管道;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CodeMartain

祝:生活蒸蒸日上!

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

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

打赏作者

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

抵扣说明:

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

余额充值