• 常用的字节输入流有: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. 在Windows7系统中设置虚拟内存大小

    当我们的电脑物理内存空间不够用时,操作系统就会自动从硬盘空间上分出一块空间来当内存使用,这就是虚拟内存.可以说虚拟内存是物理内存的补充,是备用的物理内存.一般来说,如果电脑里的程序不多,占用内存资源不 ...

  2. 多测师_讲解python__004 函数

    # 函数:一个工具,随调随用# 降级代码冗余## 增加代码的复用性,提高开发效率,为了不成为cv战士## 提高程序扩展性## 函数有两个阶段:定义阶段,调用阶段.## 定义时:只检查函数体内代码语法, ...

  3. MeteoInfoLab脚本示例:SeaWiFS HDF Grid数据

    SeaWiFS HDF Grid数据读取,特别是涉及到了文件的众多属性数据的读取,数据取对数后绘图.脚本程序: #Add data file f = addfile('D:/Temp/hdf/S199 ...

  4. 安装Node,创建vue项目,运行及打包

    1.安装node js 下载地址:http://nodejs.cn/download/ 2.安装完成后运行Node.js command prompt(node -v查看安装版本) 3.安装npm(由 ...

  5. 【应用服务 App Service】发布到Azure上的应用显示时间不是本地时间的问题,修改应用服务的默认时区

    问题情形 应用程序发布到App Service后,时间显示不是北京时间,默认情况为UTC时间,比中国时间晚 8 个小时. 详细日志 无 问题原因 Azure 上所有的服务时间都采用了 UTC 时间. ...

  6. Win10中装Win10---virtualbox虚拟机的安装及拓展

    最近在准备一档专栏时,发现我电脑中已经把一些环境配置完了,卸掉重装又显得麻烦,于是我就求助于虚拟机,虚拟机确实是个很好的东西,不久前我的一个伙伴向我请教虚拟机怎么装,发现这玩意三言两语还很难说清,于是 ...

  7. django—路由相关

    django不同版本的路由配置 django 2之前,配置urlpatterns使用的是url方法 django 2之后,配置urlpatterns使用的是path方法 path与url的区别: ur ...

  8. buuctf-misc 菜刀666

    解压出一个666666.pcapng的文件,我们拖进wireshark 因为是菜刀吗?一般都是post连接,于是我们过滤post数据 http.request.method==POST 然后分析流量, ...

  9. c++ 获取当前时间周初凌晨时间戳(获取当前时间周一凌晨时间戳)

    UINT64 GetWeekBeginTime(){ time_t t; t = time(0); tm* t_tm = localtime(&t); t_tm->tm_hour = 0 ...

  10. 理解DES算法

    首先 了解对称密码加密技术:采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密.但是有时候密钥不用完全相同 只要相似也可以.因为用一个密钥可 ...