java学习之路---IO类----字节流
来源:互联网 发布:js 获取div属性 编辑:程序博客网 时间:2024/04/30 20:43
在java io中包含流的操作主要有两种 字节流和字符流,两类都有输入输出操作。。字节流输出主要是OutputStream,输入InputStream,在字符流中输出主要是:Writer,输入主要是:Reader
java io 操作也有步骤,以文件为例
(1)使用File类来打开一个文件
(2)通过字节流或字符流的子类指定输出的位置
(3)进行读写操作
(4)关闭输入输出
1.字节流
字节流只要是以操作byte数据,以byte数组为主,组要操作类是OutputStream,InputStream
2.字节输出流
看下源码:
public abstract class OutputStream implements Closeable, Flushable {
public abstract void write(int b) throws IOException;
public void write(byte b[]) throws IOException {
write(b, 0, b. length);
}
public void write(byte b[], int off, int len) throws IOException {
if (b == null ) {throw new NullPointerException();} else if ((off < 0) || (off > b.length) || (len < 0) ||((off + len) > b. length) || ((off + len) < 0)) {throw new IndexOutOfBoundsException();} else if (len == 0) {return;}for (int i = 0 ; i < len ; i++) {write(b[off + i]);}}
public void flush() throws IOException {
}
public void close() throws IOException {
}
}
关于它的实现,它实现了一个Closeable(关闭)和flushable(刷新)
它一共就这么多方法
3.一般字节输出流的文件操作用FileOutputStream
public class OutputStreamDemo {
public static void main(String[] args) throws Exception{
//第一步,找到一个文件:
File f= new File("f:" +File.separator+"a.txt");
//第二步,通过字节流的子类来实例化
OutputStream out= null;
out= new FileOutputStream(f);
//第三步,进行读写操作
String str= "hello world";
byte b[]=str.getBytes();
out.write(b);
//第四步,关闭流
out.close();
}
}
补充:如果上面文件不存在,会自动创建
4.你反复的执行上面的程序,你会发现每一运行都会覆盖原来的内容,现在我想在后面继续写,那就要用它的另外的构造方法
public class OutputStreamDemo1 {
public static void main(String[] args) throws Exception{
File f= new File("f:" +File.separator+"a.txt");
OutputStream out= new FileOutputStream(f, true);
String str= "hello like";
byte b[]=str.getBytes();
for(int i=0;i<b.length;i++){
out.write(b[i]); //这里用一个一个的写入,和上面的效果一样,看自己喜欢那个就用那个
}
out.close();
}
}
补充:如果需要换行:String str="\r\n hello like";
5.字节输入流
看源码:
public abstract class InputStream implements Closeable {
由于方法比较多,就不全部列举了,自己可以去看下
6.字节出入流文件操作
public class InputStreamDemo {
public static void main(String[] args) throws Exception {
File f= new File("f:"+File.separator +"a.txt" );
InputStream in= new FileInputStream(f);
byte b[]=new byte[1024];
in.read(b);
in.close();
System. out.println(new String(b));
}
}
结果:
hello worldhello
从结果中发现后面还有很多空格,这是因为,我们的byte[]数组大小1024,所以我们要做处理
public class InputStreamDemo1 {
public static void main(String[] args) throws Exception{
File f= new File("f:"+File.separator +"a.txt" );
FileInputStream in= new FileInputStream(f);
byte b[]=new byte[1024];
int len=in.read(b);
in.close();
System. out.println("读入数据的长度:" +len);
System. out.println("内容为" +new String(b,0,len));
}
}
结果:
读入数据的长度:21
内容为hello worldhello like
以上虽然解决了,但是治标不治本,它还是开辟那么的空间,我们要做到需要好多就开辟好多
public class InputStreamDemo2 {
public static void main(String[] args) throws Exception{
File f= new File("f:" +File.separator+"a.txt");
InputStream in= new FileInputStream(f);
byte b[]=new byte[(int)f.length()];
in.read(b);
in.close();
System. out.println(new String(b));
}
}
结果:
hello worldhello like
还可以用另外一种读取方法:
public class InputStreamDemo3 {
public static void main(String[] args)throws Exception {
File f= new File("f:"+File.separator +"a.txt" );
InputStream in= new FileInputStream(f);
byte b[]=new byte[1024];
int temp=0;
int len=0;
while((temp=in.read())!=-1){//读到文件最后才会出现-1
b[len]=( byte)temp;
len++;
}
in.close();
System. out.println(new String(b, 0,len));
}
}
结果:
hello worldhello like
- java学习之路---IO类----字节流
- [学习笔记]Java IO之字节流
- java学习之IO字节流
- [学习笔记]Java IO之字节流
- Java基础-IO学习之字节流
- Java IO之字节流
- Java IO之字节流
- Java IO之字节流
- Java IO 之字节流
- Java IO之字节流
- Java IO之字节流
- java IO 之 字节流
- Java学习笔记之IO流中的文件字节流
- Java学习篇之IO流(字节流)
- java基础学习IO流之字节流 十一 -4
- Java IO学习笔记之字节流和字符流
- Java学习笔记之 IO包 字节流
- Java学习笔记之IO(二):InputStream输入字节流
- 链表
- Hadoop1.2.1源码解析系列:JobTracker(一)——JobTracker初始化
- 词干提取(stemming)和词形还原(lemmatization)
- linux下配置JAVA常用的编程环境
- Win32多线程之信号量(Semaphores)
- java学习之路---IO类----字节流
- 诫子书--摘抄
- HDU 1299 Diophantus of Alexandria
- The 2013 ACM-ICPC Asia Changsha Regional Contest(2013区域赛练习)
- OAuth2.0协议学习
- v$session/v$process视图涉及的相关会话信息的查询
- SqlServer2005内存分配
- winform 加密连接数据库
- 持续集成工具——Hudson