• 常用的字节输入流有:InputStream ,FileInputStream,BufferedInputStream

  • 常用的字节输出流有:OutputStream,FileOutputStream,BufferedOutputStream
  • 常见的字符输入流有:Reader,InputStreamReader,FileReader,BufferedReader
  • 常见的字符输出流有:Writer,OutputStreamWriter,FileWriter,BufferedWriter
  • Buffered修饰的流是缓存的流,信息从内存中操作,效率比一般流操作效率高

demo如下:

public class Iotest {
public static void main(String[] args) {
File file = new File("E:\\iotest\\wl.txt");
/*
* 字节流 向文件中保存内容
*/
try (OutputStream os = new FileOutputStream(file, true)) {
String str = "\r\n hello world";
byte[] ss = str.getBytes("UTF-8");
os.write(ss);
} catch (IOException e) {
e.printStackTrace();
}
/*
* 字节流 BufferedInputStream 向文件中保存信息
*/
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, true))) {
String str2 = "\r\n buffered hello world";
byte[] bytes = str2.getBytes("UTF-8");
bos.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} /*
* 字节流 从文件中读取文件信息
* */
try (InputStream inputStream = new FileInputStream(file)) {
byte[] input = new byte[1024];
int len = 0;
while ((len = inputStream.read(input)) != -1) {
System.out.println(new String(input, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} /*
* 字节流 BufferInputStream 从文件中读取内容
* */
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
int len = 0;
byte[] bytes = new byte[1024];
while ((len = bis.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} /*
* 字符流 向文件中保存信息
* */
try (Writer writer = new FileWriter(file, true)) {
String str2 = "\r\n 你好 中国!";
writer.write(str2);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} /*
* 字符流 BufferWriter 向文件中保存内容
* */
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file, true))) {
bw.write("\r\n Buffered 你好 中国!");
bw.flush();
} catch (IOException e) {
e.printStackTrace();
} /*
* 字符流 读取文件中信息
* */
try (Reader reader = new FileReader(file)) {
char[] r = new char[1024];
int len = 0;
while ((len = reader.read(r)) != -1) {
System.out.println(new String(r, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} /*
* 字符流 BufferReader 从文件中读取信息
* */
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

IO流读写数据简单示例的更多相关文章

  1. MapReduce从HBase读写数据简单示例

    就用单词计数这个例子,需要统计的单词存在HBase中的word表,MapReduce执行的时候从word表读取数据,统计结束后将结果写入到HBase的stat表中. 1.在eclipse中建立一个ha ...

  2. 八: IO流,数据的读写传输

    IO流概括图: IO流的分类:  按流: 输入流(InputStream和Reader):从硬盘或者别的地方读入内存 输出流(OutputStream和Writer):从内存里向硬盘或别的地方输出 按 ...

  3. C++ IO流_数据的旅行之路

    1. 前言 程序中的数据总是在流动着,既然是流动就会有方向.数据从程序的外部流到程序内部,称为输入:数据从程序内部流到外部称为输出. C++提供有相应的API实现程序和外部数据之间的交互,统称这类AP ...

  4. IO流-输入输出的简单实例

    InputStream和OutputStream 抽象类InputStream和OutputStream是IO流最底层的两个抽象类,所有输入/输出流的类都基于这两个类. 这两个类里最核心的三个方法是r ...

  5. io流对数据的读写

    import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; i ...

  6. 161228、Java IO流读写文件的几个注意点

    平时写IO相关代码机会挺少的,但却都知道使用BufferedXXXX来读写效率高,没想到里面还有这么多陷阱,这两天突然被其中一个陷阱折腾一下:读一个文件,然后写到另外一个文件,前后两个文件居然不一样? ...

  7. 在c#中IO流读写操作

    1.使用FileStream读写文件 文件头: using System;using System.Collections.Generic;using System.Text;using System ...

  8. 161108、Java IO流读写文件的几个注意点

    平时写IO相关代码机会挺少的,但却都知道使用BufferedXXXX来读写效率高,没想到里面还有这么多陷阱,这两天突然被其中一个陷阱折腾一下:读一个文件,然后写到另外一个文件,前后两个文件居然不一样? ...

  9. Java IO流读写文件的几个注意点

     平时写IO相关代码机会挺少的,但却都知道使用BufferedXXXX来读写效率高,没想到里面还有这么多陷阱,这两天突然被其中一个陷阱折腾一下:读一个文件,然后写到另外一个文件,前后两个文件居然不 ...

随机推荐

  1. TP5隐藏入口文件

    1,进入根目录,打开public文件夹,里面有个.htaccess文件 2,将这段代码改成?s= 3,不修改该文件,想要隐藏入口文件则会报错 4,改了文件之后是 5,改了入口文件为了隐藏  .php

  2. 【开源】Springboot API 一键生成器

    Springboot API 一键生成器 写这个项目,最大的想法就是:不做CRUD 程序猿 Springboot 在我们平时开发项目当中,是如此的常用.然而,比如平时我们写的一些: XX 管理系统 X ...

  3. Python+Appium自动化测试(4)-使用weditor进行元素定位

    一,weditor的安装与使用 首选需要在电脑上配置好Python环境 下载安装命令如下,加上镜像下载速度更快: pip install weditor -i https://pypi.tuna.ts ...

  4. 微信小程序实时将less编译为wxss

    1.npm或者yarn全局安装wxss-cli npm install -g wxss-cli 2.运行waxes-cli命令(mp_wx为小程序目录) wxss ./mp_wx 实时监听mp_wx目 ...

  5. 1.ffmpeg、ffplay、ffprobe命令使用

    1.学前知识 1.1视频码率值 码率公式: 码率(kbps)=文件大小(KB)*8/时间(秒) 所以码率和视频文件大小成正比的,不过码率超过一定值后,人眼是看不出效果的. 接下来,我们便先来学习ffm ...

  6. lumen-ioc容器测试 (4)

    lumen-ioc容器测试 (1) lumen-ioc容器测试 (2) lumen-ioc容器测试 (3) lumen-ioc容器测试 (4) lumen-ioc容器测试 (5) lumen-ioc容 ...

  7. jdk可视化工具系列——检视阅读

    jdk可视化工具系列--检视阅读 参考 java虚拟机系列 RednaxelaFX知乎问答 RednaxelaFX博客 JConsole--Java监视与管理控制台 jconsole介绍 JConso ...

  8. poj1655 Balancing Act (dp? dfs?)

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14247   Accepted: 6026 De ...

  9. EF 表中中多次指定了列名解决办法

    这个问题是我们实际开发中遇到过的问题. 可能的原因:数据库在执行数据表迁移的时候,数据表执行成功,最后插入EF数据迁移表__MigrationHistory的时候,没有把所有的命令行完整插入,缺失了一 ...

  10. 进程相关的API函数

    0x01. ID与句柄 如果我们成功创建一个进程之后,CreateProcess会返回四个数据,id和句柄 句柄的话就是 当前进程私有的句柄表索引(这是当前进程,给别进程也没用) 每个进程都有一张自己 ...