Java IO Notes (一)
原文链接: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
- different threads
- same JVM
- 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()
andwrite()
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 aFileOutputStream
. SinceFileOutputStream
is a subclass ofOutputStream
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
andOutputStream
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 anInputStreamReader
.
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 (一)的更多相关文章
- java.IO输入输出流:过滤流:buffer流和data流
java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...
- Java:IO流与文件基础
Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...
- Java IO之字符流和文件
前面的博文介绍了字节流,那字符流又是什么流?从字面意思上看,字节流是面向字节的流,字符流是针对unicode编码的字符流,字符的单位一般比字节大,字节可以处理任何数据类型,通常在处理文本文件内容时,字 ...
- java Io流向指定文件输入内容
package com.hp.io; import java.io.*; public class BufferedWriterTest{ public static void main(String ...
- java Io文件输入输出流 复制文件
package com.hp.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java ...
- java Io流更新文件内容
package com.hp.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOut ...
- java IO流详解
流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- java.io.NotSerializableException: test.io.file.Student
java.io.NotSerializableException: test.io.file.Student at java.io.ObjectOutputStream.writeObject0 ...
- java.io.IOException: mark/reset not supported
java.io.IOException: mark/reset not supported at java.io.InputStream.reset(InputStream.java:348) at ...
随机推荐
- SPOJ BALNUM Balanced Numbers 平衡数(数位DP,状压)
题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? ...
- Nuget~管理自己的包包
很久很久以前,自己就想有个包包,最近又从网上淘了一个,价格不便宜呢,99块,还是个小腰包,不过作工还算精良,我喜欢的类型,帆布休闲包,可以将我的手机,耳机,水,小烟,小酒,小伞都放里,方便至极,哈哈!
- Oracle RAC/Clusterware 多种心跳heartbeat机制介绍 RAC超时机制分析
ORACLE RAC中最主要存在2种clusterware集群件心跳 & RAC超时机制分析: 1.Network Heartbeat 网络心跳 每秒发生一次: 10.2.0.4以后网络心跳 ...
- Android(java)学习笔记132:eclipse 导入项目是提示:某些项目因位于工作空间目录中而被隐藏。
导致这个错误的原因是工程重名了: 并不是仅仅指文件夹重名,相信很多人也曾经修改过文件夹的名称,可惜没什么用处,关键是修改工程里面的一个文件! 也就是.project这个文件! 用记事本打开,修改一下& ...
- Spring中c3p0连接池 jar包下载 c3p0-0.9.2.1 jar包和mchange-commons-java-0.2.3.4 jar 包
c3p0-0.9.2.1 jar包和mchange-commons-java-0.2.3.4 jar 包 下载地址: https://pan.baidu.com/s/1jHDiR7g 密码 tyek
- 使用mfc CHtmlView内存泄露解决方法
第一步,谷歌有文章说CHtmlView部分api使用BSTR没释放: 解决方法是重写一下接口: CString GetFullName() const; CString GetFullName() c ...
- 详解wordpress如何把文件保存到阿里云OSS上!
自己搞了一个Wordpress的博客,装完之后一直晾着没管,最近闲来开荒.为了减小服务器的带宽.存储.CUP的压力,决定把博客中的所有文件都保存到阿里云OSS上面. 关于这个问题,自己去调用OSS的S ...
- 【网络基础】【TCP/IP】私有IP地址段
私有IP地址段 Class A:10.0.0.0 - 10.255.255.255 Class B:172.16.0.0 - 172.31.255.255 Class C:192.168.0. ...
- 【laravel】【转发】laravel 导入导出excel文档
1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel ,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和 导出 ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...