内存操作流
ByteArrayInputStream:主要将内容写入内存
ByteArrayOutputStream:主要将内容从内存输出
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class IOOperation {
public static void main(String[] args) throws IOException {
String str = "ROLLENHOLT";
byte[] byteArr = str.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(byteArr);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int temp = 0;
while ((temp = in.read()) != -1) {
char ch = (char) temp;
out.write(Character.toLowerCase(ch));
}
String outStr = out.toString();
in.close();
out.close();
System.out.println(outStr);
}
}
运行结果:rollenholt
管道流
管道流主要可以进行两个线程之间的通信。
PipedOutputStream:管道输出流
PipedInputStream:管道输入流
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
/**
* 消息发送类
*/
class Send implements Runnable {
private PipedOutputStream out = null;
public Send() {
this.out = new PipedOutputStream();
}
public PipedOutputStream getOut() {
return this.out;
}
@Override
public void run() {
String message = "hello , Rollen";
try {
out.write(message.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 接受消息类
*/
class Recive implements Runnable {
private PipedInputStream input = null;
public Recive() {
input = new PipedInputStream();
}
public PipedInputStream getInput() {
return this.input;
}
@Override
public void run() {
byte[] b = new byte[1024];
int len = 0;
try {
len = input.read(b);
} catch (IOException e) {
e.printStackTrace();
}
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("接受的内容为:" + new String(b, 0, len));
}
}
public class IOOperation {
public static void main(String[] args) throws IOException {
Send send = new Send();
Recive recive = new Recive();
// 管道连接
send.getOut().connect(recive.getInput());
new Thread(send).start();
new Thread(recive).start();
}
}
运行结果:接受的内容为:hello , Rollen
打印流
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class IOOperation {
public static void main(String[] args) throws IOException {
String fileName = "D:" + File.separator + "hello.txt";
File f = new File(fileName);
PrintStream print = new PrintStream(new FileOutputStream(f));
print.println("Rollen");
print.close();
}
}
运行结果:文件中写入Rollen
打印流格式化输出
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class IOOperation {
public static void main(String[] args) throws IOException {
String fileName = "D:" + File.separator + "hello.txt";
File f = new File(fileName);
PrintStream print = new PrintStream(new FileOutputStream(f));
String name = "Rollen";
int age = 20;
print.printf("姓名:%s. 年龄:%d.", name, age);
print.close();
}
}
运行结果:文件中写入 姓名:Rollen. 年龄:20.
使用OutputStream向屏幕上输出内容
import java.io.IOException;
import java.io.OutputStream;
public class IOOperation {
public static void main(String[] args) throws IOException {
OutputStream out = System.out;
out.write("Hello".getBytes());
out.close();
}
}
运行结果:输出Hello
输入输出重定向
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class IOOperation {
public static void main(String[] args) throws IOException {
// 此刻直接输出到屏幕
System.out.println("hello");
String fileName = "D:" + File.separator + "hello.txt";
File f = new File(fileName);
System.setOut(new PrintStream(new FileOutputStream(f)));
System.out.println("这些内容在文件中才能看到哦!");
}
}
运行结果:第一个打印语句输出到屏幕,第二个打印语句输出到hello.txt文件
System.in重定向
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class IOOperation {
public static void main(String[] args) throws IOException {
String fileName = "D:" + File.separator + "hello.txt";
File f = new File(fileName);
if (!f.exists()) {
return;
} else {
System.setIn(new FileInputStream(f));
byte[] b = new byte[1024];
int len = System.in.read(b);
System.out.println(new String("读入的内容:" + new String(b, 0, len)));
}
}
}