原文链接: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. SpringMVC归纳

    SpringMVC归纳 操作流程 配置前端控制器 在web.xml中配置 配置处理器映射器 在springmvc配置文件中配置 配置处理器适配器 在springmvc配置文件中配置 配置注解适配器和映 ...

  2. Windows无法停用设备,原因是某个程序正在使用它...

    有时候,当我们用完U盘需要弹出是,会出现“Windows无法停用设备,原因是某个程序正在使用它…”的黄色警告,很无奈.不过可以通过一些方法进行解决(win10版): 1. 打开“文件资源管理器”,选择 ...

  3. batchsize对收敛速度的影响

    想象一下,当mini-batch 是真个数据集的时候,是不是就退化成了 Gradient Descent,这样的话,反而收敛速度慢.你忽略了batch 增大导致的计算 batch 代价变大的问题.如果 ...

  4. fastjson中转字符串时格式化、显示null值等

    fastjson中object转string时的配置项,包括 1. 是否显示value为null的项 2. 是否格式化显示字符串 3. 日期是否格式化显示为可读字符串 ... 这些的配置均在Seria ...

  5. UI调试神器 for ios:Reveal的使用与破解

    aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAkACQAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aH

  6. ES6学习总结 (二)

    一:ES6为函数做了哪些扩展 参数的默认值 传统写法: function person(n,a){ var name =n || "zhangsan"; var age = a | ...

  7. Linux基础学习-网络管理

    Linux系统网络管理NetworkManager 1 启动网络管理服务和开机自启动 在rhel7中网路管理相关命令nmcli,nmtui,nmtui-edit,nm-connection-edito ...

  8. js替换函数用法

    定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replac ...

  9. pre-commit钩子,代码质量检查

    目前基本使用三款js代码质量检查工具: jslint, jshint, eslint.许多IDE里面也有对应的检查插件,在每次ctrl + s 保存文件的时候,检查当前文件是否符合规范,保证代码质量. ...

  10. cols

    题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...