输入流
字节输入流:FileInputStream
//方法一:
public static void main(String args[]) throws IOException {
FileInputString fi = new FileInputStream("a.txt");
int by;
while((by=fi.read())!=-1){
System.out.println((char)by);
}
//关闭流输入
fi.close();
}
//方法二:
public static void main(String args[]) throws IOException {
FileInputString fi = new FileInputStream("a.txt");
byte[] bytes = new byte[1024];
int len;
while((len=fi.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
}
字节缓冲输入流:BufferedInputStream
创建BufferedInputStream将创建一个内部缓冲区数组.当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次很多字节
public static void main(String[] args){
BufferedInputStream bf = new BufferedInputStream(new FileInputStream("a.txt"));
byte[] bytes = new byte[1024];
int len;
while((len = bf.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
bis.close();
}
输出流
字节输出流:FileOutPutStream
public static void main(String[] args) throws Exception{
FileOutputStream fo = new FileOutputStream("a.txt");
fo.write("AGE".getBytes());
fo.close();
}
字节缓冲输入流:BufferedOutputStream
public static void main(String[] args) throws IOException {
//字节缓冲输出流:BufferedOutputStream(OutputStream out)
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a.txt"));
//写数据
bos.write("hello\r\n".getBytes());
bos.write("world\r\n".getBytes());
//释放资源
bos.close();
}