1-二进制流的概述
二进制流有两个顶级类:InputStream和OutputStream, 下面的两个类是各种附属类。作为对比,二进制流的关系比字符流的更加多样化和复杂。关于二进制流,LineNumberInputStream和StringBufferInputStream两个类在JDK1.5中,尽量不要使用,因为它们已被弃用。
2- InputStream & OutputStream类
InputStream类是一个抽象类,所以您不能在 InputStream 类本身创建输出流对象。然而,这个类分为由关联类继承许多分支。在特定情况下,您可以从构造函数创建InputStream派生类的对象。
// Java.io.InputStream is an abstract class
// Could not initialize InputStream object directly, through class InputStream.
// Initialize InputStream object should be through its subclasses ..
InputStream fileStream =new FileInputStream("C:/test.txt");
// Input stream from the keyboard ..
InputStream is = System.in;
OutputStream类是一个抽象类,所以你无法通过的OutputStream类来创建输出流对象。然而,这个类分割成子分支发挥了重要作用。在某些情况下,您可以从子类的构造函数创建InputStream对象。
// Java.io.OutputStream is an abstract class
// Could not initialize OutputStream object directly, through class OutputStream.
// Initialize OutputStream object should be through its subclasses ..
// Stream write to file.
OutputStream os=new FileOutputStream("D:/outData.txt");
// Stream write to console.
OutputStream w=System.out;
HelloInputStream.java
package com.yiibai.tutorial.javaio.stream;
import java.io.FileInputStream;
import java.io.InputStream;
public class HelloInputStream {
public static void main(String[] args) {
try {
// Create InputStream object from subclass.
// This is Stream read file.
InputStream is = new FileInputStream("data.txt");
int i = -1;
// Read the turn of bytes in the stream.
// Each time the 8-bit read, convert it to int.
// Read the value of -1 means the end of the stream.
while ((i = is.read()) != -1) {
System.out.println(i + " " + (char) i);
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出结果如下:
HelloOutputStream.java
package com.yiibai.tutorial.javaio.stream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class HelloOutputStream {
public static void main(String[] args) {
try {
File dir = new File("C:/Test");
// Create directories if not exists
dir.mkdirs();
// Create output Stream write data to file.
OutputStream w = new FileOutputStream(
"C:/Test/test_outputStream.txt");
// Create array of bytes, write it to stream.
byte[] by = new byte[] { 'H', 'e', 'l', 'l', 'o' };
// write turn the bytes into the stream
for (int i = 0; i < by.length; i++) {
byte b = by[i];
// Write 1 byte.
w.write(b);
}
// Close the output stream, finish write file.
w.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出结果如下:
以上两个例子是简单的。以上两个例子是简单的。它们读或写每个字节。在下面的例子中,它们读或写同步不同的字节。这有助于提高处理速度。
InputStreamExample2.java
package com.yiibai.tutorial.javaio.stream;
import java.io.FileInputStream;
import java.io.InputStream;
public class InputStreamExample2 {
public static void main(String[] args) {
try {
// Create input stream, read a file.
InputStream in = new FileInputStream("data.txt");
// A temporary array to store data each reading
byte[] temp = new byte[10];
int i = -1;
// Reads some number of bytes from the input stream
// and stores them into the buffer array 'temp'.
// Return the number of bytes actually read.
// return -1 if end of stream.
while ((i = in.read(temp)) != -1) {
// Create String from bytes
String s = new String(temp, 0, i);
System.out.println(s);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
在下面图片的例子中说明了同时进行读取:
OutputStreamExample2.java
package com.yiibai.tutorial.javaio.stream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class OutputStreamExample2 {
public static void main(String[] args) {
try {
File dir = new File("C:/Test");
// Create directories if not exists.
dir.mkdirs();
// Create output Stream to write file.
OutputStream os = new FileOutputStream("C:/Test/test_writerOutputStream.txt");
// Create array of bytes, write bytes into the file above.
byte[] by = new byte[] { 'H', 'e', 'l', 'l', 'o', ' ', 31, 34, 92 };
byte[] by2 = new byte[] { 'H', 'e', 'l', 'l', 'o', ' ', 'b', 'o',
'y' };
// Write all of bytes in array into Stream.
os.write(by);
// Flush data in memory to file.
os.flush();
// Continue write the 2nd byte array to the stream
os.write(by2);
// Close the output stream, finish write file.
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3- ByteArrayInputStream & ByteArrayOutputStream类
ByteArrayInputStream包装字节数组(byte[] buf),并通过ByteArrayInputStream进行访问数组的元素。
ByteArrayOutputStream是一个字节流,其中包含的一个的数组字节流(byte[]buf) 能够通过自身数量的增加,以增加字节的大小。 每个字节每次被写入流时,这意味着分配这些字节到的数组。
当数组是充分分配元素,程序创建旧数组在一个新较长数组并拷贝元素为...(这是它自己的增加的字节数组的大小 - 如上所述)
ByteArrayOutputStream的一些方法:
// return the current contents of this output stream, as a byte array.
- byte[] toByteArray();
// String decoded from the buffer's contents.
- String toString() ;
// return the number of valid bytes in this output stream.
- int size();
ByteArrayInputStreamExample.java
package com.yiibai.tutorial.javaio.bytestream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteArrayInputStreamExample {
public static void main(String args[]) throws IOException {
// Byte Array.
byte[] bytes = new byte[] { 'H', 'e', 'l', 'l', 'o', ' ', 'I', 'O' };
// Using ByteArrayInputStream to read bytes array.
ByteArrayInputStream bInput = new ByteArrayInputStream(bytes);
System.out.println("Converting characters to Upper case ");
int c = 0;
// Read the turn of bytes in the stream.
// Cursor will move from the beginning to the end of the array array.
// Every time you read a byte pointer will move one step to the end.
while ((c = bInput.read()) != -1) {
char ch = (char) c;
ch = Character.toUpperCase(ch);
System.out.println(ch);
}
// Check whether this stream supports mark or not
boolean markSupport = bInput.markSupported();
System.out.println("Mark Support? " + markSupport);
// Move the cursor to the default location
// In this example, it will move to position 0 ..
bInput.reset();
char ch = (char) bInput.read();
System.out.println(ch);
// Read next byte
ch = (char) bInput.read();
System.out.println(ch);
System.out.println("Skip 4");
// Skip 4 bytes
bInput.skip(4);
ch = (char) bInput.read();
System.out.println(ch);
}
}
输出结果:
ByteArrayOutputStreamExample.java
package com.yiibai.tutorial.javaio.bytestream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ByteArrayOutputStreamExample {
public static void main(String args[]) throws IOException {
// Create ByteArrayOutputStream object.
// Object contains within it an array of bytes.
// Array with size 12 elements.
// If the number of elements to write to stream more than 12, the array will be replaced by
// new array has more elements, and copy the elements of old array into.
ByteArrayOutputStream bOutput = new ByteArrayOutputStream(12);
String s = "Hello ByteArrayOutputStream";
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch != 'a' && ch != 'e') {
bOutput.write(ch);
}
}
// Returns the current size of the buffer.
int size = bOutput.size();
System.out.println("Size = " + size);
byte[] bytes = bOutput.toByteArray();
String ss = new String(bytes);
System.out.println("New String = " + ss);
}
}
输出结果:
4- ObjectInputStream和ObjectOutputStream类
ObjectInputStream和ObjectOutputStream,并允许您对读取或写入流中。这些对象必须是可序列化的类型(这意味着它们可布置成队列)。
这里有些例子:
Student.java
package com.yiibai.tutorial.javaio.objstream;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = -5074534753977873204L;
private String firstName;
private String lastName;
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Pupil.java
package com.yiibai.tutorial.javaio.objstream;
import java.io.Serializable;
public class Pupil implements Serializable {
private static final long serialVersionUID = -8501383434011302991L;
private String fullName;
public Pupil(String fullName) {
this.fullName= fullName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
ObjectOutputStreamExample.java
package com.yiibai.tutorial.javaio.objstream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;
public class ObjectOutputStreamExample {
public static void main(String[] args) throws IOException {
File dir = new File("C:/Test");
// Create directories if not exists.
dir.mkdirs();
// Create stream write to file.
FileOutputStream fos = new FileOutputStream(
"C:/Test/testObjectStream.txt");
// Create ObjectOutputStream object wrap 'fos'.
// Data written to this stream will be pushed to 'fos'.
ObjectOutputStream oos = new ObjectOutputStream(fos);
// Write String to Stream.
oos.writeUTF("This is student, pupil profiles");
// Note: Write Serializable object only.
// Write an Object to stream.
oos.writeObject(new Date());
Student student1 = new Student("Thanh", "Phan");
Student student2 = new Student("Ngan", "Tran");
Pupil pupil1 = new Pupil("Nguyen Van Ba");
oos.writeObject(student1);
oos.writeObject(pupil1);
oos.writeObject(student2);
oos.close();
System.out.println("Write successful");
}
}
运行结果:
这是一个关于文件写入对象的图示。它被依次处理。在未来阅读时,你要记住写这样可以读取它正确的顺序。
这是上述实例中提到的 ObjectInputStream EOF 读取文件的一个例子:
ObjectInputStreamExample.java
package com.yiibai.tutorial.javaio.objstream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Date;
public class ObjectInputStreamExample {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
// Create stream to read file.
FileInputStream fis = new FileInputStream(
"C:/Test/testObjectStream.txt");
// Create ObjectInputStream object wrap 'fis'.
ObjectInputStream ois = new ObjectInputStream(fis);
// Read String.
String s = ois.readUTF();
System.out.println(s);
// Read Object.
Date date = (Date) ois.readObject();
System.out.println("Date = " + date);
Student student1 = (Student) ois.readObject();
System.out.println("Student " + student1.getFirstName());
Pupil pupil = (Pupil) ois.readObject();
System.out.println("Pupil " + pupil.getFullName());
Student student2 = (Student) ois.readObject();
System.out.println("Student " + student2.getFirstName());
ois.close();
}
}
运行结果:
5- Class DataInputStream 和 DataOutputStream
DataInputStream
// Constructor
public DataOutputStream(OutputStream out)
// Write a character 16 bit (2-byte)
public void writeChar(int val)
// Write double 64 bit (8-byte)
public void writeDouble(double val)
// Write float 32 bit (4-byte)
public void writeFloat(float val)
// Write integer 32 bit (4-byte)
public void writeInt(int val)
// Write String UTF-8.
public void writeUTF(String obj)
....
DataInputStream
// Constructor
public DataInputStream(InputStream in)
// Read a character 16 bit (2 byte)
public char readChar()
// Read double 64 bit (8 byte)
public double readDouble()
// Read float 32 bit (4 byte)
public float readFloat()
// Read int 16 bit (4 byte)
public int readInt()
// Read UTF-8 String.
public String readUTF()
....
DataOutputStreamExample.java
package com.yiibai.tutorial.javaio.datastream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputStreamExample {
public static void main(String[] args) throws IOException {
int cityIdA = 1;
String cityNameA = "Green Lake City";
int cityPopulationA = 500000;
float cityTempA = 15.50f;
int cityIdB = 2;
String cityNameB = "Salt Lake City";
int cityPopulationB = 250000;
float cityTempB = 10.45f;
File dir = new File("C:/Test");
dir.mkdirs();
//
// Create FileOutputStream write to file.
//
FileOutputStream fos = new FileOutputStream("C:/Test/cities.txt");
// Create DataOutputStream object wrap 'fos'.
// The data write to 'dos' will be pushed to 'fos'.
DataOutputStream dos = new DataOutputStream(fos);
//
// Write data.
//
dos.writeInt(cityIdA);
dos.writeUTF(cityNameA);
dos.writeInt(cityPopulationA);
dos.writeFloat(cityTempA);
dos.writeInt(cityIdB);
dos.writeUTF(cityNameB);
dos.writeInt(cityPopulationB);
dos.writeFloat(cityTempB);
dos.flush();
dos.close();
}
}
运行 DataOutputStream 类实例类,并收到一个写出来数据文件。
DataInputStreamExample.java
package com.yiibai.tutorial.javaio.datastream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class DataInputStreamExample {
public static void main(String[] args) throws IOException {
// Stream to read file.
FileInputStream fis = new FileInputStream("C:/Test/cities.txt");
// Create DataInputStream object wrap 'fis'.
DataInputStream dis = new DataInputStream(fis);
//
// Read data.
//
int cityId1 = dis.readInt();
System.out.println("Id: " + cityId1);
String cityName1 = dis.readUTF();
System.out.println("Name: " + cityName1);
int cityPopulation1 = dis.readInt();
System.out.println("Population: " + cityPopulation1);
float cityTemperature1 = dis.readFloat();
System.out.println("Temperature: " + cityTemperature1);
//
// Read data.
//
int cityId2 = dis.readInt();
System.out.println("Id: " + cityId2);
String cityName2 = dis.readUTF();
System.out.println("Name: " + cityName2);
int cityPopulation2 = dis.readInt();
System.out.println("Population: " + cityPopulation2);
float cityTemperature2 = dis.readFloat();
System.out.println("Temperature: " + cityTemperature2);
dis.close();
}
}
运行结果:
6- SequenceInputStream
现在你已经熟悉了读取一个文件,并知道如何得到一个输入流。然而,你有时需要读取很多文件并加入数据写入另一个文件中,例如。这意味着你以串联小数据流方式,加入许多输入流在一起用来创建一个更大的流。我们现在讨论 java.io.SequenceInputStream 中的类。没有对应的定义输出流...
// Constructor
// Create new Stream from Pairing two streams together
// which will be read in order
public SequenceInputStream(InputStream s1,InputStream s2)
// Create new Stream from Multi input stream.
// which will be read in order
public SequenceInputStream(Enumeration extends InputStream> e)
示例:
// Input stream read a file - File1.txt .
InputStream is1=new FileInputStream("File1.txt");
// Input stram read a file - File2.txt
InputStream is2=new FileInputStream("File2.txt");
// Create new Stream from two stream
SequenceInputStream sis=new SequenceInputStream(is1,is2);
7- PipedInputStream,PipedOutputStream
8- Class PrintStream
// PrintStream is the subclass of FilterOutputStream.
// It can wrap a binary output stream (OutputStream) , ..
// Constructor :
// Wrap a OutputStream
public PrintStream(OutputStream out)
public PrintStream(OutputStream out,boolean autoFlush)
// Write to file ..
public PrintStream(String fileName)
// Some methods
public void println(String s)
public void print(char ch)
// Write an Object
public void print(Object obj)
// Write long value (64bit)
public void print(long n)
public PrintStream append(java.lang.CharSequence csq) .
// ... (more see javadoc)
现在我们已经知道如何通过 try-catch 捕捉异常。
try {
// Do something here
// Error divided by 0
int i=10/0;
}
// The catch block is executed
catch(Exception e) {
// Print out message
System.out.println("Error on try..."+e.getMessage());
// Print 'stack trace' to Console.
// How to get the text "stack trace"?
e.printStackTrace();
}
这是你经常看到的“堆栈跟踪”,当有东西出错时。
下面的示例是用来检索字符串“堆栈跟踪”
GetStackTraceString.java
package com.yiibai.tutorial.javaio.printstream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class GetStackTraceString {
private static String getStackTraceString(Exception e) {
// Create ByteArrayOutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Data written to this stream will be pushed to 'baos'.
PrintStream printStream = new PrintStream(baos);
// Prints this throwable and its backtrace to 'printStream'
e.printStackTrace(printStream);
printStream.close();
byte[] bytes = baos.toByteArray();
String s = new String(bytes);
return s;
}
public static void main(String[] args) {
try {
// Do something here
// Error divided by 0
int i = 10 / 0;
}
// The catch block is executed
catch (Exception e) {
// Print out message
System.out.println("Error on try..." + e.getMessage());
// Get the text "stack trace"
String s = getStackTraceString(e);
System.out.println("Stack Trace String " + s);
}
}
}
下面是打印“堆栈跟踪”到控制台的代码,但使用printStackTrace(PrintStream)方法,而不是使用printStackTrace()方法,默认为打印“堆栈跟踪”到控制台。
// PrintStream to write to the Console
PrintStream os = System.out;
// Exception e ..
// Write 'stack trace' to 'os'. This means print to the Console.
e.printStackTrace(os);
// In essence it is equivalent to calling:
e.printStackTrace();
¥ 我要打赏
纠错/补充
收藏
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。