1、InputStream(输入字节流)
InputStream是一个定义了Java流式字节输入模式的抽象类,该类的所有方法在出错时都会引发一个IOExceptin异常。
方法 描述
int available() 返回当前可读的输入字节数
void close() 关闭输入流。关闭之后若再读取则会产生IOException
void mark(int numBytes) 再输入流当前带点放置一个标志。
boolean markSupported() 如果调用的流支持mark()/reset()就返回true
int read()
int read(byte buffer[])
int read(byte buffer[],int offset,int numBytes)
void reset()
long skip(long numBytes)
2、OutputStream
void close()
void flush()
void write(int b)
void write(byte buffer[])
void write(byte buffer[],int offset,int numBytes)
3、FileInputStream (文件输入流)
FileInputStream(String filepath)
FileInputStream(File fileObj)
4、FileOutputStream (文件输入流)
FileOutputStream(String filePaht)
FileOutputStream(File fileObj)
FileOutputStream(String filePath,boolean append)
package IO;
import java.io.*;
public class StreamDemo {
public static void main(String[] args) {
//创立一个文件
File f = new File("E:\\Java\\iodemo.txt");
//将数据写入文件中
OutputStream out = null;
try {
out = new FileOutputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = "Hello World!!";
byte b[] = str.getBytes();
try {
out.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//读文件
InputStream in = null;
try {
in = new FileInputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte b1[] = new byte[1024];
int i = 0;
try {
i = in.read(b1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(new String(b1,0,i));
}
}