java.io包中定义了多个流类型来实现输入输出功能。可分为输入 输出流 、 字节流 字符流 、 接电流 处理流
所有流类型位于java.io包内 都分别继承了以下四种抽象流类型。
InputStream OutputStream Reader Writer
1、节点流:可以从一个特定的数据源(节点)读写数据。即可直接读写、获取。
处理流:不止一个管道。
2、带有Stream关键字都为字节流:InputStream OutputStream
字符流: Reader Writer
3、继承自InputStream的流都是用于向程序中输入数据的。单位为字节。
方法: int read() 读取一个字节 以整数的形式返回
int read(byte[] buffer) 读取一系列的字节并存储到一个数组buffer,返回读取的字节数。
int read(byte[] buffer, int offset, int length) 从offset位置开始读取length长度的字节,到buufer里。
void close() 关闭。
4、继承自OutputStream的流都是用于写数据的。单位为字节。
方法:void write(int b) 向输出流写入一个字节,内容为b的低八位。
void write(byte[] b) 将一个字节类型的数组中的数据写入到输出流
void write(byte[] b ,int off, int len) 讲一个字节类型的数组从off位置开始的len个字节写入到输出流中。
void flush() 将缓存中的数据全写入到目的地。close前使用。
void close() 关闭流,释放资源。
5、继承自Reader 和 Writer的流都是用于输入、输出数据的。单位为字符。方法类似Stream。
=======================================
具体流:
=======================================
6、
import java.io.*;
public class Streamtest
{
public static void main(String[] args)
{
int b;
FileInputStream in = null;
try{
in = new FileInputStream("D:\\test1\\Streamtest\\src\\Streamtest.java");
} catch (FileNotFoundException e){
System.out.println("file not found!");
System.exit(-1);
}
try{
int num = 0;
while((b = in.read()) != -1)
{
System.out.print((char)b);
num++;
}
in.close();
System.out.println();
System.out.println("共获取了" + num + "个字符");
} catch (IOException e1){
System.out.println("文件读取错误!");
System.exit(-1);
}
}
}
7、缓冲流:套接到相应的节点流上,对读写的数据提供缓冲功能。
BufferedInputStream
import java.io.*;
public class BufferStream {
public static void main(String[] args)
{
try
{
FileInputStream in = new FileInputStream("D:\\test1\\Streamtest\\src\\Streamtest.java");
BufferedInputStream bis = new BufferedInputStream(in);
int c = 0;
//System.out.print((char)bis.read());
for(int i=0;i<30&&(c = bis.read())!=-1;i++)
{
System.out.print((char)c + " ");
if(i == 10) bis.mark(1000);
}
System.out.println();
bis.reset();
for(int i=0;i<30&&(c = bis.read())!=-1;i++)
{
System.out.print((char)c + " ");
}
}catch(IOException e){
}
}
}
ps:mark方法是在当前位置做下标记,参数告知在接下来读出多少个字符内mark有效。 reset方法可以回到最近一次的mark位置。
8、BufferedReader BufferedWriter
import java.io.*;
public class BufferStream {
public static void main(String[] args)
{
String s;
try
{
FileWriter fw = new FileWriter("D:\\test0.txt");
FileReader fr = new FileReader("D:\\test0.txt");
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
for(int i=0;i<10;i++)
{
s = String.valueOf(Math.random());
bw.write(s);
bw.newLine();//换行
}
bw.flush();//缓冲区内容写入目标
while((s = br.readLine())!=null)
{
System.out.println(s);
}
}catch(IOException e){
System.out.println("file not exist !");
}
}
}
9、转换流:InputStreamReader 将InputStream转换为Rerader。 OutputStreamWriter 将OutputStream转换为Writer。
import java.io.*;
public class BufferStream {
public static void main(String[] args)
{
String s;
try
{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\test.txt"));
osw.write("i am end !");
System.out.println(osw.getEncoding());
osw.close();
osw = new OutputStreamWriter(new FileOutputStream("D:\\test.txt",true),"ISO8859_1");
osw.write("i an mumu !");
System.out.println(osw.getEncoding());
osw.close();
}catch(IOException e){
System.out.println("file not exist !");
}
}
}
10、数据流:DataInputStream DaraOutputStream
可以直接读取 写入各种类型的数据。
--------------------------------------------------------------------
end、