/*
记住每个类相应的用法
*/
流的分类:
io包内定义了所有的流
分类:
方向:输入流、输出流
处理数据单位:字节流、字符流
功能不同:节点流、处理流

所有流类型,位于java.io包内,分别继承以下四种抽象流类型
InputStream 字节流、输入流
OutputStream 字节流、输出流
Reader 字符流、输入流
Writer 字符流、输出流

节点流:从一个特定的数据源(节点,如文件、内存)读写数据
处理流:“连接”在已存在的流(字节流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。

继承自四个抽象类的类:
InputStream:继承自 InputStream的流都是用于向程序中输入数据,且数据的单位为字节(8bit)。
继承自 InputStream的类:
节点流:
FileInputStream
PipedInputStream
ByteArrayInputStream
StringBufferInputStream
处理流:
FilerInputStream:
LineNumberInputStream
DataInputStream
BufferedInputStream
PushbackInputStream
SequenceInputStream
ObjectInputStream
InputStream的基本方法:
int read():读取一个字节,并以整数形式返回(0~255),若输出-1则已都导输入流末尾
int read(byte[] buffer) throws IOException
int read(byte[] buffer, int offset, int length) throws IOException
void close()

OutputStream的主要方法:
void flush() throws IOException : 将输出流中缓冲的数据全部写出到目的地

节点流类型
File(文件):
字符流: FileReader FileWriter
// One Class

FileReader fr = new FileReader("d:\\test.java");
c = fr.read(); // 读取一个字符
system.out.println((char)c);

// One Class

FileWriter fw = new FileWriter("d:\\test2.dat");
int c = 0;
fw.writer(c);

字节流: FileInputStream FileOutputStream
Memory Array
字符流: CharArrayReader CharArrayWriter
字节流: ByteArrayInputStream ByteArrayOutputStream
Memory String
字符流: StringReader StringWriter
Pipe:
字符流: PipedReader PipedWriter
字节流: PipedInputStre am PipedOutputStream
凡是Reader或者Writer都是字符,Stream都是字节

处理流类型:包在别的流上的流
Buffering(缓冲流):显著减少对IO读写的次数,保护硬盘
字符流: BufferedReader BufferedWriter
FileInputStream fis = new FileInputStream("d:\\test3.java");
BufferedInputStream bis = new BufferedInputStream(fis);
system.out.println(bis.read());
字节流:BufferInputStream BufferedOutputStream
Coverting between bytes and character(转换流):字节流转换为字符流
字符流:InputStreamReader OutputStreamWriter
// One Case

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d:test4.txt"));
osw.writer("asdsddeeffdgd"); 06-06

// One Case

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr); // br 这个对象有readLine() 方法,这个方法比较好用
String s = br.readLine(); // br.readLine() 这个方法阻塞等待前端输入数据
System.out.println(s);

Data conversion
字节流: DataInputStream DataOutputStream 提供了可以存取与机器无关的Java原始类型数据(int/double等)的方法,直接将数据以原始数
据类型存入存储设备,而且是UTF-8的,省空间。
构造方法分别为 DataInputStream(InputStream in)和 DataOutputStream(OutputStream out)
// One Case

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeDouble(Math.random()); // 先写入8个字节
dos.writeBoolean(true); // 再写入一个字节
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
DataInputStream dis = new DataInputStream(bais);
System.out.println(dis.readDouble()); // 先写进去的先读出来
System.out.println(dis.readBoolean()); // 后写进去的后读出来
dos.close; dis.close;

Print流
PrintWriter和PrintStream都属于输出流,分别针对字符和字节
PrintWriter和PrintStream提供了重载的print
println()方法用于多种数据类型的输出。
PrintWriter和PrintStream的输出操作不会抛出异常,用户通过检测错误状态获取错误信息
PrintWriter和PrintStream有自动flush功能。

// One Case

FileOutputStream fos = new FileOutputStream("d:\\test5.txt");
PrintStream ps = new PrintStream(fos);
if(ps != null){
System.setOut(ps); // 设置内容输出的位置,默认为屏幕
}
System.out.print("asdads");

// One Case

// 打印日志
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
FileWriter fw = new FileWriter("d:\\logFile.log", true); // 加 true是为了每次写入时在原来数据的后面写入,而不覆盖原来的日志
PrintWriter log = new PrintWriter(fw);
log.println("-------");
log.println("today's log");
log.fulsh
log.println("====" + new Date() + "====");
log.close();

Object Serialization:把整个Object 写入存储设备。(序列化:一个Object直接写到硬盘上)
字节流:ObjectInputStream ObjectOutputStream
// One Case 序列化地保存一个对象的属性

class T implements Serializable    // Serializable 为序列化标记接口,不包含任何方法,标记给编译器看
{
int i = 10;
int j = 9;
double d = 2.3;
int k = 15;
} T t = new T();
t.k = 8;
FileOutputStream fos = new FileOutputStream("d:/test6.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(t);
oos.flush();
oos.close(); FileInputStream fis = new FileInputStream("d:/test6.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
T tReaded = (T)ois.readObject();
System.out.println(tReaded.i + " " + tReaded.j + " " + tReaded.d + " " + tReaded.k);

transient 关键字(透明的)某个属性是透明的,序列化的时候不予考虑,该属性对应的值为默认值。

Byte Streams
程序使用8-bit 字节的比特流执行输入输出
文件I/O byte流:FileInputStream/FileOutputStream

Always Close Streams

public class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally { // 关闭一个流
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}

FileReader/FileWriter

Java I/O Basic的更多相关文章

  1. Java fundamentals of basic IO

    IO is a problem difficult to handle in various of systems because it  always becomes a bottleneck in ...

  2. java实现HTTP Basic认证

    这两天一直在调试EMQ的API,通过HTTP的GET请求,可以查询到订阅列表信息,在浏览器中测试时,需要输入用户名和密码,然后才能显示出结果,输错或者不输入会返回401错误. 通过浏览器输入用户名和密 ...

  3. java 发送带Basic Auth认证的http post请求实例代码

    构造http header private static final String URL = "url"; private static final String APP_KEY ...

  4. java 发送带Basic Auth认证的http post请求

    构造http header private static final String URL = "url"; private static final String APP_KEY ...

  5. [JavaEE] Testing the Java EE Application : Basic Arquillian integration test

    We have model like this: package com.pluralsight.bookstore.model; import javax.persistence.*; import ...

  6. 【JAVA】【Basic】MacOS上搭建JAVA开发环境

    1. JRE 1.1. 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index.html 1.1.1. dmg格式安装: ...

  7. 【JAVA】【Basic】概念

    1. 历史 1.1. Sun, Green Project, 90年代初,为机顶盒提供一个统一的语言层,oak-->Java, James Gosling, Sun World 1995:JAV ...

  8. Flink Program Guide (1) -- 基本API概念(Basic API Concepts -- For Java)

    false false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable {mso-style-n ...

  9. Java资源大全中文版(Awesome最新版)

    Awesome系列的Java资源整理.awesome-java 就是akullpp发起维护的Java资源列表,内容包括:构建工具.数据库.框架.模板.安全.代码分析.日志.第三方库.书籍.Java 站 ...

随机推荐

  1. 通讯录(ios自带无界面)

    1,添加框架AddressBook.framework 2,请求权限认证,在Appdelegate.m文件中 - (BOOL)application:(UIApplication *)applicat ...

  2. jQuery.lazyload详解

    <SCRIPT src="jquery.js" type=text/javascript></SCRIPT> <SCRIPT src="jq ...

  3. SQL ISNULL 函数

    sql 中 NULL 值的处理:微软的 ISNULL() 函数用于规定如何处理 NULL 值.NVL(), IFNULL() 和 COALESCE() 函数也可以达到相同的结果.语法ISNULL ( ...

  4. 原生Ajax写法(GET)

    ajax的GET提交方式的原生代码: var xhr = null; if(window.XMLHttpRequest){ xhr = new XMLHttpRequest(); }else if(w ...

  5. ZJOI2016 Round 1 之前

    day 0 中午要出发了,很虚.. 主要原因: 1.在转语言 2.模板还没有系统整理过 3.最近代码能力感觉要狗带 4.急于想为联赛翻盘... MARK几个未完成的任务 1.字符串处理再去看看..实在 ...

  6. AC自动机(二维) UVA 11019 Matrix Matcher

    题目传送门 题意:训练指南P218 分析:一行一行的插入,一行一行的匹配,当匹配成功时将对应子矩阵的左上角位置cnt[r][c]++;然后统计 cnt[r][c] == x 的数量 #include ...

  7. Redis入门指南

    随着互联网业务对性能需求日益强烈,作为Key/Value存储的Redis具有数据类型丰富和性能表现优异的特点.如果能够熟练地驾驭它,不管是把它用做缓存还是存储,对很多大型应用都很多帮助.新浪作为世界上 ...

  8. UVa12298 Super Poker II(母函数 + FFT)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/23590 Description I have a set of super poker cards, ...

  9. apache activemq 学习笔记

    0.activemq的概念 activemq实现了jms(java Message server),用于接收,发送,处理消息的开源消息总线. 1.activemq和jms的区别 jms说白了就是jav ...

  10. Storm on Yarn :原理分析+平台搭建

    Storm on YARN: Storm on YARN被视为大规模Web应用与传统企业应用之间的桥梁.它将Storm事件处理平台与YARN(Yet Another Resource Negotiat ...