java系统输出_Java 文件,系统输入输出

在整个IO包中,流的操作就分为两种:

字节流:字节输出流OutputStream,字节输入流InputStream

字符流(一个字符等于两个字节):字符输出流Writer,字符输入流是Reader

IO操作的基本步骤

1,使用File找到一个文件

2,使用字节流和字符流的子类为OutputStream,InputStream,Writer,Reader进行实例化操作

3,进行读或写操作

4,关闭:close(),在流的操作中最终必须进行关闭

在java中"\r\n"表示换行

这里系统,文件以字节流输入

//InputStream 字节流

//字节流系统输入

InputStream inputStream = System.in;

Scanner cin = new Scanner(inputStream);

//字节流文件输入

InputStream inputStream = new FileInputStream("E:/temp.txt");

Scanner cin = new Scanner(inputStream);

//InputStreamReader 字节流转化为字符流

//字符流系统输入

InputStreamReader is = new InputStreamReader(System.in);

BufferedReader cin = new BufferedReader(is);

//字节流文件输入

InputStreamReader is = new InputStreamReader(new FileInputStream("E:/temp.txt"));

BufferedReader cin = new BufferedReader(is);

//经常用

Scanner cin = new Scanner(new BufferedInputStream(System.in));

BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));

文件输出:

PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream("E:/input.txt")));

== PrintWriter out = new PrintWriter(new FileOutputStream("E:/temp.txt));

PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

new Main().run(cin, out);

out.close(); // 不要忘了

这里out使用方法跟System.out.使用方法差不多

字节流与字符流区别:

字节流在操作的时候是直接与文件本身关联,不使用缓冲区,字节直接存到文件中;字符流在操作的时候是通过缓冲区与文件操作,字符到缓冲区然后再到文件中,所以字符流中存在一个flush()方法来刷新缓冲区。

综合比较来讲,在传输或者在硬盘上保存的内容是以字节的形式存在的,所以字节流的操作较多,但是在操作中文的时候字符流比较好用。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值