Java IO流:
2015-05-30 09:03
435 查看
Java IO流:读出一系列字节的对象:输入流;
写入一系列字节的对象:输出流;
InputStream类:
读取方式:
read;
read(byte []b);
read(byte []b,int off,inr len ); //off:起始偏移,len:字节数
其它方法:
long skip(long n);//在输入流中跳过n个字节
int available();//在流不发生堵塞的情况下,可读取的字节数
Void close();//关闭输入流并释放资源
void mark(int readlimit) ;//在输入流的当前位置放置一个标记,如果读取的字节数多于readlimit设置的值,则流忽略这个标记。
void reset() ;//返回到上一个标记。
boolean markSupported() ;//测试当前流是否支持mark和reset方法。如果支持,返回true,否则返回false。
OnputStream类:
写出方式:
write(int b);
write(byte []b);
write(byte []b,int off,inr len ); //off:起始偏移,len:字节数
其它方法:
Void flush();//强制缓冲区的字节被写出
Void close();//关闭输出流并释放资源
FileInputStream和FileOutputStream
节点流,用于从文件中读取或往文件中写入字节流。如果在构造FileOutputStream时,文件已经存在,则覆盖这个文件。
FileOutputStream fs=new FileOutputStream(“1.txt”)
fs.write(“sfkjrgvwkj,fhntlegk”.getBytes());//write方法只能写入字节或字节数组,通过字符串的getByte()方法返回一个字节数组
fs.close();
FileInputStream fis=new FileInputStream("1.txt");
byte[] buf=new byte[100];
int len=fis.read(buf);
System.out.println(new String(buf,0,len));
fis.close();
BufferedInputStream和BufferedOutputStream
过滤流,需要使用已经存在的节点流来构造,提供带缓冲的读写,提高了读写的效率。
FileOutputStream fs=new FileOutputStream("1.txt");
BufferedOutputStream bfos=new BufferedOutputStream(fs);
bfos.write("sfkjrgvwkj,fhntlegk".getBytes());
//bfos.flush();
bfos.close();
FileInputStream fis=new FileInputStream("1.txt");
BufferedInputStream bfis=new BufferedInputStream(fis);
byte[] buf=new byte[100];
int len=bfis.read(buf);
System.out.println(new String(buf,0,len));
bfis.close();
PipedInputStream和PipedOutputStream
管道流,用于线程间的通信。一个线程的PipedInputStream对象从另一个线程的PipedOutputStream对象读取输入。要使管道流有用,必须同时构造管道输入流和管道输出流。
写入一系列字节的对象:输出流;
InputStream类:
读取方式:
read;
read(byte []b);
read(byte []b,int off,inr len ); //off:起始偏移,len:字节数
其它方法:
long skip(long n);//在输入流中跳过n个字节
int available();//在流不发生堵塞的情况下,可读取的字节数
Void close();//关闭输入流并释放资源
void mark(int readlimit) ;//在输入流的当前位置放置一个标记,如果读取的字节数多于readlimit设置的值,则流忽略这个标记。
void reset() ;//返回到上一个标记。
boolean markSupported() ;//测试当前流是否支持mark和reset方法。如果支持,返回true,否则返回false。
OnputStream类:
写出方式:
write(int b);
write(byte []b);
write(byte []b,int off,inr len ); //off:起始偏移,len:字节数
其它方法:
Void flush();//强制缓冲区的字节被写出
Void close();//关闭输出流并释放资源
FileInputStream和FileOutputStream
节点流,用于从文件中读取或往文件中写入字节流。如果在构造FileOutputStream时,文件已经存在,则覆盖这个文件。
FileOutputStream fs=new FileOutputStream(“1.txt”)
fs.write(“sfkjrgvwkj,fhntlegk”.getBytes());//write方法只能写入字节或字节数组,通过字符串的getByte()方法返回一个字节数组
fs.close();
FileInputStream fis=new FileInputStream("1.txt");
byte[] buf=new byte[100];
int len=fis.read(buf);
System.out.println(new String(buf,0,len));
fis.close();
BufferedInputStream和BufferedOutputStream
过滤流,需要使用已经存在的节点流来构造,提供带缓冲的读写,提高了读写的效率。
FileOutputStream fs=new FileOutputStream("1.txt");
BufferedOutputStream bfos=new BufferedOutputStream(fs);
bfos.write("sfkjrgvwkj,fhntlegk".getBytes());
//bfos.flush();
bfos.close();
FileInputStream fis=new FileInputStream("1.txt");
BufferedInputStream bfis=new BufferedInputStream(fis);
byte[] buf=new byte[100];
int len=bfis.read(buf);
System.out.println(new String(buf,0,len));
bfis.close();
PipedInputStream和PipedOutputStream
管道流,用于线程间的通信。一个线程的PipedInputStream对象从另一个线程的PipedOutputStream对象读取输入。要使管道流有用,必须同时构造管道输入流和管道输出流。
相关文章推荐
- windows下Java环境变量的配置置
- Java垃圾回收机制
- java-策略模式
- Eclipse快捷键指南
- hadoop实战–搭建eclipse开发环境及编写Hello World
- 编译hadoop eclipse的插件(hadoop1.0)
- 解决Eclipse Android XML视图预览不了的问题
- Java编码规范(The Elements of Java Style)
- 解决Eclipse ADT v22.0.1 升级不了v23.0.0,但又卸载不了的坑人问题
- Spring MVC项目配置
- 关于eclipse工程转到androidstudio 的问题
- Java Send EMail with Attachment
- Java send email(No attachment)
- Java基础——各种“进制”的知识
- JavaWeb系列之六(Servlet的学习)
- Unrecognized option: -jrockit Error: Could not create the Java Virtual Machine.
- Java基础——数据类型
- spring mvc 与 kindeditor 整合
- JSP 动作 JavaBean
- 【MyEclipse教程】—如何设置jsp页面默认utf-8编码