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 ...
随机推荐
- Processing一些常用技巧
一些常用技巧总结: Tweak模式 快速查找函数用法 显示与输入中文注释 代码快速对齐 批量添加注释符 Tweak模式 Tweak模式是非常有用的功能,自3.0版本后,它就正式整合到Processin ...
- eclipse中代码注释及其他常用快捷键
html代码注释/取消注释 Ctrl + Shift + c php代码注释/取消注释 Ctrl + / (4)Ctrl+Shift+/ 说明: ...
- helm install
reference 前提:已安装k8s:v1.10.4 helm install on master(无需下载官方tar包) 链接:https://pan.baidu.com/s/1Ji3Ru1pTQ ...
- 01_3Java Application初步
01_3Java Application初步 l Java源文件以“java”为扩展名.源文件的基本组成部分是类(class),如本例中的HelloWorld类. l 一个源文件中最多只有一个publ ...
- 关于lua 5.3 服务端热更新流程
脚本的热更新的流程都大同小异, 第一步先保存旧代码的块的数据, 第二部加载新的代码块,第三步将旧代码块的局部和全局数据拷贝到新代码块的对应的 变量中. 在服务器热更新中,主要考虑热更的内容是什么, 一 ...
- VS快捷键总结(开发中经常遇到)
1.窗口快捷键 (大家有没有发现但凡跟窗口挂上钩的快捷键当中都有一个W,那是因为W代表Windows也就是窗口的意思) Ctrl+W,W: 浏览器窗口 (浏览橱窗用有道的翻译是window shop ...
- 『jQuery』.html(),.text()和.val()的概述及使用--2015-08-11
如何使用jQuery中的.html(),.text()和.val()三种方法,用于读取,修改元素的html结构,元素的文本内容,以及表单元素的value值的方法 本节内容主要介绍的是如何使用jQu ...
- NOIP模拟赛 czy的后宫4
czy的后宫4 [问题描述] czy有很多妹子,妹子虽然数量很多,但是质量不容乐观,她们的美丽值全部为负数(喜闻乐见). czy每天都要带N个妹子到机房,她们都有一个独一无二的美丽值,美丽值为-1到- ...
- Linux - 链接概念详解
1> Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过 ...
- json_encode() 避免转换中文
json_encode() 避免转换中文 我们都知道,json_encode()可以将数据转换为json格式,而且只针对utf8编码的数据有效,而且在转换中文的时候,将中文转换成不可读的”\u***” ...