ByteArrayToDataStream.java
public class ByteArrayToDataStream {
public static void main(String[] args) {
// 节点流(节点对象为字节数组)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//数据处理流,套接在字节数组节点流上。方便写数据
DataOutputStream dos = new DataOutputStream(baos);
try {
//由于套接了数据处理流,所以可以直接写long,double类型数据
dos.writeLong(999999999999999L);
dos.writeDouble(Math.random());
ByteArrayInputStream bais = new ByteArrayInputStream(baos
.toByteArray());
//数据处理流,套接在字节数组节点流上。方便读数据
DataInputStream dis = new DataInputStream(bais);
System.out.println(dis.readLong());
System.out.println(dis.readDouble());
dos.close();
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
TestBufferedReader.java
/**
* Title:TestBufferedReader.java
* Description:BufferedReader为处理缓冲流,其中的readline()
* 方法很好用,经常套接在别的节点流上使用。
*
* @author winwin
* @version 1.0
* @since May 20, 20092:55:21 PM
*/
public class TestBufferedReader {
public static void main(String[] args) {
try {
//创建一个文件节点输入流
FileReader fr = new FileReader("d:\\test.txt");
//创建一个处理缓冲流
BufferedReader br = new BufferedReader(fr);
String string = null;
while((string=br.readLine())!=null){
System.out.println(string);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
TestByteArryStream.java
public class TestByteArryStream {
public static void main(String[] args) {
//节点流,节点为字符数组,存储在内存中
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteArrayInputStream bis = null;
int a = 0;
byte[] bytes = {'a','b','c','1','2','3'};
try {
bos.write(bytes);
bis = new ByteArrayInputStream(bos.toByteArray());
while((a = bis.read())!=-1){
System.out.print((char)a);
}
bos.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
TestFileInputStream.java
/**
* Title:TestFileInputStream.java
* Description:FileInputStream为输入流,字节流,节点流(操作的节点为文件),
* 通过此流可以读取文件中的内容到程序中,但由于是字节流,将不能正确
* 读取中文。
* @author winwin
* @version 1.0
* @since May 20, 200911:12:45 AM
*/
public class TestFileInputStream {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
int read;
String string = "D:\\test.txt";
try {
//在文件上创建输入流
fileInputStream = new FileInputStream(string);
//按字节读取文件,直至结尾(-1),不能读取中文
while((read=fileInputStream.read())!=-1){
System.out.print((char)read);
}
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
文章来源:
http://java999.blog.51cto.com/259217/160015