System.in、System.out和System.err 标准输入输出流
每当开始执行一个java程序时,系统都会先行创建System.in、System.out和System.err三个基本数据流对象。
System类中的三个静态常量:
public static final InputStream in
public static final PrintStream out
public static final PrintStream err
System.in:标准输入数据流对象,负责将用户所输入的键盘数据传送到程序中予以处理。
System.out:标准输出数据流对象,负责将程序执行的结果输出到显示器屏幕上。
System.err:标准错误输出流对象,同样属于标准输出数据流对象,此对象是System.out对象的变种,负责将程序执行时所产生的错误提示信息输出到显示器屏幕上。
例:
package pc;
import java.io.IOException;
import java.util.Scanner;
public class demo1 {
public static void main(String[] args) {
System.err.println("System.err");
System.out.println("System.out");
try {
System.out.print("请输入:");
int t = System.in.read(); // 读取的数据时整型的ASCII值
System.out.println(t);
System.out.println(((char)t));
} catch (IOException e) {
e.printStackTrace();
}
Scanner in = new Scanner(System.in);
System.out.print("请输入: ");
int t = in.nextInt();
System.out.println(t);
}
}
/*
结果:
System.err
System.out
请输入:a
97
a
请输入:5
5
*/
字节流(以Stream结尾的流) 字符流(以Reader和Writer结尾的流)
例:将整型12存入文件:
以字节型:12 -> 1100 将二进制数值存入文件
以字符型:1 -> 49, 2 -> 50 将这两个转换后的ASCII码值存入文件
除了最开始的四个基类,后面每一对输入输出流附一个例子
四大基类:InputStream、OutStream、Reader、Writer
所有输入流都是抽象类InputStream或抽象类Reader的子类
所有输出流都是抽象类OutStream或抽象类Writer的子类
InputStream类
-
-
Constructor and Description InputStream()
-
-
-
Modifier and Type Method and Description int available() 返回从该输入流中可以读取(或跳过)的字节数的估计值,而不会被下一次调用此输入流的方法阻塞。 void close() 关闭此输入流并释放与流相关联的任何系统资源。 void mark(int readlimit) 标记此输入流中的当前位置。 boolean markSupported() 测试这个输入流是否支持 mark和 reset方法。 abstract int read() 从输入流读取数据的下一个字节。 int read(byte[] b) 从输入流读取一些字节数,并将它们存储到缓冲区 b 。 返回字节数 int read(byte[] b, int off, int len) 从输入流读取最多 len字节的数据到一个字节数组。 void reset() 将此流重新定位到上次在此输入流上调用 mark方法时的位置。 long skip(long n) 跳过并丢弃来自此输入流的 n字节数据。
-
-
并不是并不是所有的InputStream的子类都支持InputStream中的所有方法,如skip(),mark(),reset()等方法只对某些子类有用。
-
汉字占两个字节,用字节流可能会乱码
Reader类
-
-
Modifier Constructor and Description protected Reader() 创建一个新的字符流阅读器,其关键部分将在阅读器本身上同步。 protected Reader(Object lock) 创建一个新的字符流阅读器,其关键部分将在给定对象上同步。
-
-
-
Modifier and Type Method and Description abstract void close() 关闭流并释放与之相关联的任何系统资源。 void mark(int readAheadLimit) 标记流中的当前位置。 boolean markSupported() 告诉这个流是否支持mark()操作。 int read() 读一个字符 int read(char[] cbuf) 将字符读入数组。 abstract int read(char[] cbuf, int off, int len) 将字符读入数组的一部分。 int read(CharBuffer target) 尝试将字符读入指定的字符缓冲区。 boolean ready() 告诉这个流是否准备好被读取。 void reset() 重置流。 long skip(long n) 跳过字符
-
OutputStream类
-
-
Constructor and Description OutputStream()
-
-
-
Modifier and Type Method and Description void close() 关闭此输出流并释放与此流相关联的任何系统资源。 void flush() 刷新此输出流并强制任何缓冲的输出字节被写出。 | 彻底完成输出并清空缓冲区 void write(byte[] b) 将 b.length字节从指定的字节数组写入此输出流。 void write(byte[] b, int off, int len) 从指定的字节数组写入 len个字节,从偏移 off开始输出到此输出流。 abstract void write(int b) 将指定的字节写入此输出流。
-
Writer
-
-
Modifier Constructor and Description protected Writer() 创建一个新的人物流作家,其关键部分将在输出流本身上同步。 protected Writer(Object lock) 创建一个新的字符流写入器,其关键部分将在给定对象上进行同步。
-
-
-
Modifier and Type Method and Description Writer append(char c) 将指定的字符附加到此输出流。 Writer append(CharSequence csq) 将指定的字符序列附加到此输出流。 Writer append(CharSequence csq, int start, int end) 将指定字符序列的子序列附加到此输出流。 abstract void close() 关闭流,先刷新。 abstract void flush() 刷新流。 void write(char[] cbuf) 写入一个字符数组。 abstract void write(char[] cbuf, int off, int len) 写入字符数组的一部分。 void write(int c) 写一个字符 void write(String str) 写一个字符串 void write(String str, int off, int len) 写一个字符串的一部分。
-
FileInputStream 文件输入流
-
-
Constructor and Description FileInputStream(File file) 通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。 FileInputStream(FileDescriptor fdObj) 创建 FileInputStream通过使用文件描述符 fdObj ,其表示在文件系统中的现有连接到一个实际的文件。 FileInputStream(String name) 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
-
-
-
Modifier and Type Method and Description int available() 返回从此输入流中可以读取(或跳过)的剩余字节数的估计值,而不会被下一次调用此输入流的方法阻塞。 void close() 关闭此文件输入流并释放与流相关联的任何系统资源。 protected void finalize() 确保当这个文件输入流的 close方法没有更多的引用时被调用。 FileChannel getChannel() 返回与此文件输入流相关联的唯一的FileChannel对象。 FileDescriptor getFD() 返回表示与此 FileInputStream正在使用的文件系统中实际文件的连接的 FileDescriptor对象。 int read() 从该输入流读取一个字节的数据。 int read(byte[] b) 从该输入流读取最多 b.length个字节的数据为字节数组。 int read(byte[] b, int off, int len) 从该输入流读取最多 len字节的数据为字节数组。 long skip(long n) 跳过并从输入流中丢弃 n字节的数据。
-
FileOutputStream 文件输出流
-
-
Constructor and Description FileOutputStream(File file) 创建文件输出流以写入由指定的 File对象表示的文件。 FileOutputStream(File file, boolean append) 创建文件输出流以写入由指定的 File对象表示的文件。 FileOutputStream(FileDescriptor fdObj) 创建文件输出流以写入指定的文件描述符,表示与文件系统中实际文件的现有连接。 FileOutputStream(String name) 创建文件输出流以指定的名称写入文件。 FileOutputStream(String name, boolean append) 创建文件输出流以指定的名称写入文件。
-
-
-
Modifier and Type Method and Description void close() 关闭此文件输出流并释放与此流相关联的任何系统资源。 protected void finalize() 清理与文件的连接,并确保当没有更多的引用此流时,将调用此文件输出流的 close方法。 FileChannel getChannel() 返回与此文件输出流相关联的唯一的FileChannel对象。 FileDescriptor getFD() 返回与此流相关联的文件描述符。 void write(byte[] b) 将 b.length个字节从指定的字节数组写入此文件输出流。 void write(byte[] b, int off, int len) 将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。 void write(int b) 将指定的字节写入此文件输出流。
-
package pc;
import java.io.*;
import java.util.Arrays;
public class FileInputStream_FileOutputStream {
public static void main(String[] args) {
File file = new File("a.txt"); // 也可以不用FIle创建文件,FileOutputStream构造时文件不存在会先创建文件
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
String str = "hello";
byte[] b = str.getBytes();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(b);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long n = file.length();
byte[] bytes = new byte[Math.toIntExact(n)];
try (FileInputStream fis = new FileInputStream(file);){ // 自动关闭流
fis.read(bytes);
System.out.println(new String(bytes));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();0
}
}
}
- jdk1.7后可以用try语句自动关闭流,如上例。可以自动关闭的流实现了AutoCloseable接口。
FileReader
-
-
Constructor and Description FileReader(File file) 创建一个新的 FileReader ,给出 File读取。 FileReader(FileDescriptor fd) 创建一个新的 FileReader ,给定 FileDescriptor读取。 FileReader(String fileName) 创建一个新的 FileReader ,给定要读取的文件的名称。
-
-
-
String getEncoding() 返回此流使用的字符编码的名称。
-
FileWriter
-
-
Constructor and Description FileWriter(File file) 给一个File对象构造一个FileWriter对象。 FileWriter(File file, boolean append) 给一个File对象构造一个FileWriter对象。 FileWriter(FileDescriptor fd) 构造与文件描述符关联的FileWriter对象。 FileWriter(String fileName) 构造一个给定文件名的FileWriter对象。 FileWriter(String fileName, boolean append) 构造一个FileWriter对象,给出一个带有布尔值的文件名,表示是否附加写入的数据。
-
-
-
String getEncoding() 返回此流使用的字符编码的名称。
-
package pc;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FIleReader_FileWriter {
public static void main(String[] args) {
FileWriter fw = null;
String str = "你好啊";
try {
fw = new FileWriter("你好.txt");
fw.write(str);
} catch (IOException e) {
e.printStackTrace();
}finally {
try{
if(fw != null){
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
try(FileReader fr = new FileReader("你好.txt")){
char[] chars = new char[100];
int hasRead = -1;
while((hasRead = fr.read(chars)) != -1){
System.out.println(new String(chars));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedInputStream 缓冲区输入数据流
A BufferedInputStream为另一个输入流添加了功能,即缓冲输入和支持mark和reset方法的功能。 当创建BufferedInputStream时,将创建一个内部缓冲区数组(内存中)。 当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次有多个字节。 mark操作会记住输入流中的一点,并且reset操作会导致从最近的mark操作之后读取的所有字节在从包含的输入流中取出新的字节之前重新读取。
-
-
Constructor and Description BufferedInputStream(InputStream in) 创建一个 BufferedInputStream并保存其参数,输入流 in ,供以后使用。 BufferedInputStream(InputStream in, int size) 创建 BufferedInputStream具有指定缓冲区大小,并保存其参数,输入流 in ,供以后使用。
-
-
-
Modifier and Type Method and Description int available() 返回从该输入流中可以读取(或跳过)的字节数的估计值,而不会被下一次调用此输入流的方法阻塞。 void close() 关闭此输入流并释放与流相关联的任何系统资源。 void mark(int readlimit) 见的总承包 mark的方法 InputStream 。 boolean markSupported() 测试这个输入流是否支持 mark和 reset方法。 int read() 见 read法 InputStream的一般合同。 int read(byte[] b, int off, int len) 从给定的偏移开始,将字节输入流中的字节读入指定的字节数组。 void reset() 见 reset法 InputStream的一般合同。 long skip(long n) 见 skip法 InputStream的一般合同。
-
BufferedOutputStream
-
该类实现缓冲输出流。 通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的每个字节导致底层系统的调用。
-
-
Constructor and Description BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。 BufferedOutputStream(OutputStream out, int size) 创建一个新的缓冲输出流,以便以指定的缓冲区大小将数据写入指定的底层输出流。
-
-
-
Modifier and Type Method and Description void flush() 刷新缓冲输出流。 void write(byte[] b, int off, int len) 从指定的字节数组写入 len个字节,从偏移 off开始到缓冲的输出流。 void write(int b) 将指定的字节写入缓冲的输出流。
-
package pc;
import java.io.*;
public class FIle_Buffer_Stream {
public static void main(String[] args) {
FileInputStream fis = null;
BufferedInputStream bis = null;
byte[] bytes = new byte[100];
try{
fis = new FileInputStream("a.txt");
bis = new BufferedInputStream(fis);
int len = bis.read(bytes);
System.out.println(new String(bytes, 0, len));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try{
if(fis != null){
fis.close();
}
if(bis != null){
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try{
fos = new FileOutputStream("b.txt");
bos = new BufferedOutputStream(fos);
bos.write(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try{
if(bos != null){
bos.close();
}
if(fos != null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
BufferedReader
-
BufferedReader 属于一种间接读取对象,也就是说它无法直接读取存在文件或内存中的数据内容,而是提供个其他Reader对象以缓冲区的方式暂存数据,以此来减少对原数据的存取次数。
-
-
Constructor and Description BufferedReader(Reader in) 创建使用默认大小的输入缓冲区的缓冲字符输入流。 BufferedReader(Reader in, int sz) 创建使用指定大小的输入缓冲区的缓冲字符输入流。
-
-
-
Modifier and Type Method and Description void close() 关闭流并释放与之相关联的任何系统资源。 Stream lines() 返回一个 Stream ,其元素是从这个 BufferedReader读取的行。 void mark(int readAheadLimit) 标记流中的当前位置。 boolean markSupported() 告诉这个流是否支持mark()操作。 int read() 读一个字符 int read(char[] cbuf, int off, int len) 将字符读入数组的一部分。 String readLine() 读一行文字。 boolean ready() 告诉这个流是否准备好被读取。 void reset() 将流重置为最近的标记。 long skip(long n) 跳过字符
-
BufferedWriter
-
BufferedWriter会先将所有目标数据写入缓冲区,而后再执行写入操作,转而提供给其他Writer对象使用。
-
-
Constructor and Description BufferedWriter(Writer out) 创建使用默认大小的输出缓冲区的缓冲字符输出流。 BufferedWriter(Writer out, int sz) 创建一个新的缓冲字符输出流,使用给定大小的输出缓冲区。
-
-
-
Modifier and Type Method and Description void close() 关闭流,先刷新。 void flush() 刷新流。 void newLine() 写一个行分隔符。 void write(char[] cbuf, int off, int len) 写入字符数组的一部分。 void write(int c) 写一个字符 void write(String s, int off, int len) 写一个字符串的一部分。
-
package pc;
import java.io.*;
import java.util.Scanner;
public class File_Buffer_er {
public static void main(String[] args) {
try (FileWriter fw = new FileWriter("键盘输入.txt");
BufferedWriter bw = new BufferedWriter(fw);
){
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
bw.write(sc.nextLine());
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
try(FileReader fr = new FileReader("键盘输入.txt");
BufferedReader br = new BufferedReader(fr);
) {
String s = null;
while((s = br.readLine() )!= null){ // 判断一次读一行
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
DataOutputStream 数据(格式化)数据流
-
当程序执行DataOutputStream时,会先将数据按照用户指定的格式导入DataOutputStream对象中,在通过外部的OutputStream对象执行写入操作。
-
-
Constructor and Description DataOutputStream(OutputStream out) 创建一个新的数据输出流,以将数据写入指定的底层输出流。
-
-
-
Modifier and Type Method and Description void flush() 刷新此数据输出流。 int size() 返回计数器的当前值 written ,到目前为止写入此数据输出流的字节数。 void write(byte[] b, int off, int len) 写入 len从指定的字节数组起始于偏移 off基础输出流。 void write(int b) 将指定的字节(参数 b的低8位)写入底层输出流。 void writeBoolean(boolean v) 将 boolean写入底层输出流作为1字节值。 void writeByte(int v) 将 byte作为1字节值写入底层输出流。 void writeBytes(String s) 将字符串作为字节序列写入基础输出流。 void writeChar(int v) 将 char写入底层输出流作为2字节值,高字节优先。 void writeChars(String s) 将字符串写入底层输出流作为一系列字符。 void writeDouble(double v) 双参数传递给转换 long使用 doubleToLongBits方法在类 Double ,然后写入该 long值基础输出流作为8字节的数量,高字节。 void writeFloat(float v) 浮子参数的转换 int使用 floatToIntBits方法在类 Float ,然后写入该 int值基础输出流作为一个4字节的数量,高字节。 void writeInt(int v) 将底层输出流写入 int作为四字节,高位字节。 void writeLong(long v) 将 long写入底层输出流,为8字节,高字节为首。 void writeShort(int v) 将 short写入底层输出流作为两个字节,高字节优先。 void writeUTF(String str) 使用UTF-8编码以机器无关的方式将字符串写入基础输出流。
-
DataInputStream
-
在执行DataInputStream对象时,会先通过其他类型的InputStream对象将数据读入缓冲区中,在通过用户指定的数据格式按序读取缓冲区内的所有数据内容。
-
-
Constructor and Description DataInputStream(InputStream in) 创建使用指定的底层InputStream的DataInputStream。
-
-
-
Modifier and Type Method and Description int read(byte[] b) 从包含的输入流中读取一些字节数,并将它们存储到缓冲区数组 b 。 int read(byte[] b, int off, int len) 从包含的输入流读取最多 len个字节的数据为字节数组。 boolean readBoolean() 见的总承包 readBoolean的方法 DataInput 。 byte readByte() 见的总承包 readByte的方法 DataInput 。 char readChar() 见 readChar方法的总合同 DataInput 。 double readDouble() 见 readDouble方法 DataInput的总体合同。 float readFloat() 见 readFloat法 DataInput的一般合同。 void readFully(byte[] b) 见的总承包 readFully的方法 DataInput 。 void readFully(byte[] b, int off, int len) 见的总承包 readFully的方法 DataInput 。 int readInt() 见 readInt方法 DataInput的一般合同。 long readLong() 见的总承包 readLong的方法 DataInput 。 short readShort() 见 readShort方法 DataInput的一般合同。 int readUnsignedByte() 见的总承包 readUnsignedByte的方法 DataInput 。 int readUnsignedShort() 见 readUnsignedShort法 DataInput的一般合同。 String readUTF() 见 readUTF法 DataInput的一般合同。 static String readUTF(DataInput in) 从流in读取以UTF-8格式编码的Unicode字符串的表示; 这个字符串然后作为String返回。 int skipBytes(int n) 见 skipBytes法 DataInput的一般合同。
-
package pc;
import java.io.*;
public class DateaInputStream_DataOutputStream {
public static void main(String[] args) {
try (FileOutputStream os = new FileOutputStream("Data.txt");
DataOutputStream dos = new DataOutputStream(os);) {
dos.writeBoolean(true);
dos.writeChar('s');
dos.writeUTF("data"); // 写入字符串
dos.writeDouble(2.3333);
dos.writeInt(5);
} catch (IOException e) {
e.printStackTrace();
}
try (FileInputStream fis = new FileInputStream("Data.txt");
DataInputStream dis = new DataInputStream(fis)) {
System.out.println(dis.readBoolean());
System.out.println(dis.readChar());
System.out.println(dis.readUTF()); // 读取字符串
System.out.println(dis.readDouble());
System.out.println(dis.readInt());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
PipedOutputStream 管道数据流
-
管道输出流可以连接到管道输入流以创建通信管道。 管道输出流是管道的发送端。
-
-
Constructor and Description PipedOutputStream() 创建一个尚未连接到管道输入流的管道输出流。 PipedOutputStream(PipedInputStream snk) 创建连接到指定管道输入流的管道输出流。
-
-
-
Modifier and Type Method and Description void close() 关闭此管道输出流,并释放与此流相关联的任何系统资源。 void connect(PipedInputStream snk) 将此管道输出流连接到接收器。 void flush() 刷新此输出流并强制任何缓冲的输出字节被写出。 void write(byte[] b, int off, int len) 从指定的字节数组写入 len个字节,从偏移量 off开始输出到这个管道输出流。 void write(int b) 写入指定 byte到管道输出流。
-
PipedInputStream
-
管道输入流应连接到管道输出流; 管道输入流然后提供写入管道输出流的任何数据字节。
-
-
Constructor and Description PipedInputStream() 创建一个 PipedInputStream ,所以它还不是已连接 PipedInputStream(int pipeSize) 创建一个 PipedInputStream ,使其尚未连接 PipedInputStream(PipedOutputStream src) 创建一个 PipedInputStream ,使其连接到管道输出流 src 。 PipedInputStream(PipedOutputStream src, int pipeSize) 创建一个 PipedInputStream ,使其连接到管道输出流 src ,并为管道缓冲区使用指定的管道大小。
-
-
-
Modifier and Type Method and Description int available() 返回可以从该输入流读取而不阻塞的字节数。 void close() 关闭此管道输入流,并释放与流相关联的任何系统资源。 void connect(PipedOutputStream src) 使此管道输入流连接到管道输出流 src 。 int read() 从这个管道输入流读取数据的下一个字节。 int read(byte[] b, int off, int len) 从这个管道输入流读取最多 len个字节的数据到字节数组。 protected void receive(int b) 接收一个字节的数据。
-
package pc;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class Piped_Stream {
public static void main(String[] args) {
try (PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos);) {
pos.write(25);
System.out.println(pis.read());
} catch (IOException e) {
e.printStackTrace();
}
}
}
ObjectInputStream 对象输入流 ObjectInputStream 对象输出流
- 以对象为单位进行读写。
- 调用ObjectOutputStream输出的对象必须实现Serializable接口。
package pc;
import java.io.*;
public class Object_Stream {
public static void main(String[] args) {
try(FileOutputStream fos = new FileOutputStream("Object.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);) {
oos.writeObject(new Student(12));
oos.writeObject(new Student(12));
oos.writeObject(new Student(14));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try(FileInputStream fis = new FileInputStream("Object.txt");
ObjectInputStream ois = new ObjectInputStream(fis);) {
Object s = null;
while((s = ois.readObject()) != null){
System.out.println((Student)s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
class Student implements Serializable{
private int age;
private String sno;
private String name;
public Student(int age) {
this.age = age;
}
public Student(int age, String sno, String name) {
this.age = age;
this.sno = sno;
this.name = name;
}
@Override
public String toString() {
return age + "";
}
}
表格内容摘自java api文档