InputStream用法
package io;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class InputStreamTest {
public static void fileInputStreamTest() throws IOException {
String fileName = "src/io/a.txt";
System.out.println("read():");
try (FileInputStream fileInputStream = new FileInputStream(fileName);) {
int a = 0;
while ((a = fileInputStream.read()) != -1) {
System.out.print((char)a);
}
}
System.out.println("\nread(b):");
try (FileInputStream fileInputStream = new FileInputStream(fileName);) {
int len = 0;
byte[] b = new byte[1024];
while ((len = fileInputStream.read(b)) != -1) {
System.out.print(new String(b, 0, len));
}
}
System.out.println("\nread(b, off, len):");
try (FileInputStream fileInputStream = new FileInputStream(fileName);) {
int len = 0;
byte[] b = new byte[1024];
while ((len = fileInputStream.read(b, 0, 1024)) != -1) {
System.out.print(new String(b, 0, len));
}
}
System.out.println("\nskip(n):");
try (FileInputStream fileInputStream = new FileInputStream(fileName);) {
int len = 0;
byte[] b = new byte[3];
while ((len = fileInputStream.read(b, 0, 3)) != -1) {
System.out.print("[len:" + len + "]" + new String(b, 0, len));
System.out.print("[" + fileInputStream.skip(2) + "]");
System.out.println("[available:" + fileInputStream.available() + "]");
}
}
}
public static void byteArrayInputStreamTest() throws IOException {
System.out.println("read():");
byte[] b = new byte[]{'a', 'b', 'c'};
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b)) {
int a = 0;
while((a = byteArrayInputStream.read()) != -1 ) {
System.out.print((char)a);
}
}
System.out.println("\nread(byte[] b):");
byte[] dest = new byte[1024];
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b)) {
int len = 0;
while((len = byteArrayInputStream.read(dest)) != -1 ) {
System.out.print(new String(dest, 0, len));
}
}
System.out.println("\nreadAllBytes:");
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b)) {
dest = byteArrayInputStream.readAllBytes();
System.out.print(new String(dest, 0, dest.length));
System.out.println("\nreadNBytes:");
byteArrayInputStream.readNBytes(dest, 0, dest.length);
System.out.print(new String(dest, 0, dest.length));
}
}
public static void main(String[] args) throws IOException {
byteArrayInputStreamTest();
}
}
OutputStream用法
public class OutputStreamTest {
public static void fileOutputStreamTest() throws IOException {
String fileName = "src/io/b.txt";
try (FileOutputStream fileOutputStream = new FileOutputStream(fileName);) {
fileOutputStream.write('a');
byte[] b = new byte[]{'b', 'c'};
fileOutputStream.write(b);
}
}
public static void main(String[] args) throws IOException {
fileOutputStreamTest();
}
}
Reader用法
public static void fileReaderTest() throws IOException {
String fileName = "src/io/a.txt";
try (FileReader fileReader = new FileReader(fileName)) {
int a = 0;
while ((a = fileReader.read()) != -1) {
System.out.print((char)a);
}
}
System.out.println("");
try (FileReader fileReader = new FileReader(fileName)) {
int len = 0;
char[] b = new char[1024];
while ((len = fileReader.read(b)) != -1) {
System.out.print(new String(b, 0, len));
}
}
}
Writer用法
public static void fileWriterTest() throws IOException {
String fileName = "src/io/b.txt";
try (FileWriter fileWriter = new FileWriter(fileName);) {
fileWriter.write('a');
char[] b = new char[]{'b', 'c'};
fileWriter.write(b);
}
}