原文链接:http://tutorials.jenkov.com/java-io/index.html

  • Java NIO  It contains classes that does much of the same as the Java IO and Java     Networking APIs, but Java NIO can work in non-blocking mode.
  • outputstream
  • inputstream
  • pipes

    1. different threads
    2. same JVM
    3. different from the pipe concept in Unix / Linux, where two processes running in different address spaces can communicate via a pipe
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream; public class PipeExample { public static void main(String[] args) throws IOException { final PipedOutputStream output = new PipedOutputStream();
final PipedInputStream input = new PipedInputStream(output); Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
output.write("Hello world, pipe!".getBytes());
} catch (IOException e) {
}
}
}); Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
int data = input.read();
while(data != -1){
System.out.print((char) data);
data = input.read();
}
} catch (IOException e) {
}
}
}); thread1.start();
thread2.start(); }
}
  • Warnning!!!  The read() and write() calls on the streams are blocking, meaning if you try to use the same thread to both read and write, this may result in the thread deadlocking itself.
  • There are many other ways than pipes that threads can communicate within the same JVM. In fact, threads more often exchange complete objects rather than raw byte data. But - if you need to exchange raw byte data between threads, Java IO's pipes are a possibility.

  • Networking

  • Basically this means that if you have code that is capable of writing something to a file, that same something could easily be written to a network connection. All that is required is that your component doing the writing depends on an OutputStream instead of a FileOutputStream. Since FileOutputStream is a subclass of OutputStream this should be no problem.
  • Java IO: Byte & Char Arrays

  • Reading Arrays via InputStream or Reader

byte[] bytes = new byte[1024];

//write data into byte array...

InputStream input = new ByteArrayInputStream(bytes);

//read first byte
int data = input.read();
while(data != -1) {
//do something with data //read next byte
data = input.read();
}
  • Writing to Arrays via OutputStream or Writer

ByteArrayOutputStream output = new ByteArrayOutputStream();

output.write("This text is converted to bytes".getBytes("UTF-8"));

byte[] bytes = output.toByteArray();

CharArrayWriter   toCharArray   ----- the same

  • System.in

  • connected to keyboard input of console programs
  • System.out

  • outputs the data you write to it to the console.
  • System.out

  • works like System.out except it is normally only used to output error texts.
try {
InputStream input = new FileInputStream("c:\\data\\...");
System.out.println("File opened..."); } catch (IOException e){
System.err.println("File opening failed:");
e.printStackTrace();
}
  • Exchanging System Streams

OutputStream output = new FileOutputStream("c:\\data\\system.out.txt");
PrintStream printOut = new PrintStream(output); System.setOut(printOut);

Now all data written to System.out should be redirected into the file "c:\\data\\system.out.txt".

  • Reader And Writer

  • They are intended for reading and writing text. The InputStream and OutputStream are byte based
  • Reader

 Reader reader = new FileReader("c:\\data\\myfile.txt");

    int data = reader.read();
while(data != -1){
char dataChar = (char) data;
data = reader.read();
}
  • Combining Readers With InputStreams

  • If you have an InputStream and want to read characters from it, you can wrap it in an InputStreamReader.
Reader reader = new InputStreamReader(inputStream);
  • Write

Writer writer = new FileWriter("c:\\data\\file-output.txt");

writer.write("Hello World Writer");
writer.close();

Java IO Notes (一)的更多相关文章

  1. java.IO输入输出流:过滤流:buffer流和data流

    java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...

  2. Java:IO流与文件基础

    Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...

  3. Java IO之字符流和文件

    前面的博文介绍了字节流,那字符流又是什么流?从字面意思上看,字节流是面向字节的流,字符流是针对unicode编码的字符流,字符的单位一般比字节大,字节可以处理任何数据类型,通常在处理文本文件内容时,字 ...

  4. java Io流向指定文件输入内容

    package com.hp.io; import java.io.*; public class BufferedWriterTest{ public static void main(String ...

  5. java Io文件输入输出流 复制文件

    package com.hp.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java ...

  6. java Io流更新文件内容

    package com.hp.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOut ...

  7. java IO流详解

    流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  8. java.io.NotSerializableException: test.io.file.Student

    java.io.NotSerializableException: test.io.file.Student    at java.io.ObjectOutputStream.writeObject0 ...

  9. java.io.IOException: mark/reset not supported

    java.io.IOException: mark/reset not supported at java.io.InputStream.reset(InputStream.java:348) at ...

随机推荐

  1. PostgreSQL: epoch 新纪元时间的使用

    新纪元时间 Epoch 是以 1970-01-01 00:00:00 UTC 为标准的时间,将目标时间与 1970-01-01 00:00:00时间的差值以秒来计算 ,单位是秒,可以是负值; 有些应用 ...

  2. 【Python图像特征的音乐序列生成】关于小样本的一些思考

    我之前就注意到,深度学习和音乐结合,尤其是从乐理出发进行结合(而不是纯粹的进行音乐生成),是一个尚未被深度挖掘的全新领域.可想而知,这个方向符合我要求的数据肯定是要自己搜集了. 自己搜集的数据,在量上 ...

  3. [Git]使用Git上传本地项目,并同步到Github上

    第一步:先要在github.com中创建一个仓库(New Repository). 第二步,打开Git Bash ① git init [+仓库名]:初始化仓库,执行之后可以在指定的仓库存放地上面看到 ...

  4. cluvfy stage命令用法

    1.获取集群验证工具cluvfy的帮助信息 grid@rac1:/home/grid>cluvfy -help USAGE: cluvfy [ -help ] cluvfy stage { -l ...

  5. phpmyadmin设置不密码,不登录直接进入

    1.config.sample.inc.php改为config.inc.php 2.加入或更改代码: [php] $cfg['Servers'][$i]['auth_type'] = 'config' ...

  6. python之*的魔性用法

    1. *在函数中的作用 聚合 在函数定义时聚合 def eat(args): print('我请你吃:',args) eat('蒸羊羔儿') # 输出结果 # 我请你吃: 蒸羊羔儿 打散 在函数执行时 ...

  7. Java字符串池(String Pool)深度解析(转)

    出自  http://www.cnblogs.com/fangfuhai/p/5500065.html 在工作中,String类是我们使用频率非常高的一种对象类型.JVM为了提升性能和减少内存开销,避 ...

  8. Bootstrap历练实例:动画的进度条

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. PAT 乙级 1048

    题目 题目地址:PAT 乙级 1048 思路 这道题坑的地方在于:即使B的长度小于A,仍然要对B补齐,也就是说最终结果的长度取决于A和B中长度更长的那一项:即只要A.B长度不一致,就要对短的一个进行补 ...

  10. django知识分支_1

    django知识分支 1.Cookie工作流程: 浏览器向服务器发出请求,服务器接收到浏览器的请求进行处理,服务器设置一个cookie发送给浏览器,浏览器将cookie保存,当需要再次登录的时候,浏览 ...