Java知识(IO流、.InputStream和OutputStream)

1.I/O流(输入输出流)

流的概念:数据流向某个对象的数据序列,并且到达这个对象的过程。
流的分类:
1.按流向分类:
输入流:数据源流向计算机内存的过程
输出流:把数据从程序流向目标数据源的过程
2.按流的基类分类:
输出流:OutputStream(字节输出流)和Writer(字符输出流)为基类
输入流:InputStream(字节输入流)和Reader(字符输入流)为基类
3.按处理数据单元划分:
(byte)字节流:以字节为数据单位来处理的流
(char)字符流:以字符为数据单位来处理的流
字节流 字符流

字节输出流 字节输入流 字符输出流 字符输入流

2.InputStream


方法:
java.io.InputStream包
方法说明
int read()从输入流中读取单个字节,返回所读取的字节数据
int read(byte[] b)从输入流中读取多个b.length长度的字节,并存储在字节数组b中,返回实际读取的字节
iny read(byte[] b,int off,int len)从输入流中读取最多len长度的字节,保存到字节数组b中,保存的位置从off开始
void close()关闭输入流
int avaliable()返回可以从输入流中读取的字节数目
skip(long n)从输入流跳过参数n指定数目的字节
mark(int readlimit)标记输入流中的当前位置,以便使用reset()方法复位到该标记的位置
void reset()将当前位置复位为上次调用mark()方法标记的位置

读取文件内容:
public class Ch01 {
/**
* 读取文件内容
*
*/
public static void main(String[] args) {
//磁盘路径两种表示方式
//1.双反斜线 \\
//2.斜线 /
try {
//从文件地址读取内容到程序中
InputStream is=new FileInputStream("D:/iodemo/ch01.txt");
//开始读取信息
//先定义一个字节数组存放数据
byte[] b=new byte[is.available()];
//声明一个int存储每次读取到的数据
int i=0;
//定义一个记录下标的变量
int index=0;
//循环读取每个数据
while((i=is.read())!=-1){
b[index]=(byte)i;
index++;
}
//如果把字节数组转换成字符串
System.out.println(new String(b));
// for (int i = 0; i <b.length; i++) {
// is.read(b);
// }
// System.out.println(new String(b));
is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
断点读取文件内容:
public class Ch05 {
public static void main(String[] args) {
//读取过程中暂停
//给当前位置做个标志
//下一次从标记位置开始读取
try {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:/iodemo/ch04.txt"));
byte[] b=new byte[bis.available()];
//设置断点
bis.mark(bis.read(b,0,b.length/2));
System.out.println(new String(b));
System.out.println("暂停读取...");
Thread.sleep(2000);
//reset将当前复位的位置设置成上次调用mark标记的位置
bis.reset();
bis.read(b);
System.out.println("继续读取...");
System.out.println(new String(b));
bis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

读取有两个文件流的序列流:
public class Ch06 {
public static void main(String[] args) {
try {
//第一个文件流
FileInputStream fis1=new FileInputStream("D:/iodemo/ch04.txt");
//第二个文件流
FileInputStream fis2=new FileInputStream("D:/iodemo/ch06.txt");
//合并到序列流中
SequenceInputStream sis=new SequenceInputStream(fis1, fis2);
byte[] b=new byte[fis1.available()+fis2.available()];
System.out.println(fis1.available());
int len=sis.read(b);
System.out.println(len);
sis.read(b, len, fis2.available());
System.out.println(new String(b));
sis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
读取三个或三个以上文件流的序列流:
public class Ch07 {
public static void main(String[] args) {
try {
//三个文件流
FileInputStream fis=new FileInputStream("D:/iodemo/a.txt");
FileInputStream fis2=new FileInputStream("D:/iodemo/b.txt");
FileInputStream fis3=new FileInputStream("D:/iodemo/c.txt");
//把三个流添加到集合中
Vector<FileInputStream> vector=new Vector<>();
vector.add(fis);
vector.add(fis2);
vector.add(fis3);
//合并在一个序列流中
SequenceInputStream sis=new SequenceInputStream(vector.elements());
byte[] b=new byte[fis.available()+fis2.available()+fis3.available()];
int off=0;
for (int i = 0; i < vector.size(); i++) {
//off相当于存放数据的起始下标位置
off+=sis.read(b,off,vector.get(i).available());
}
System.out.println(new String(b));
sis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

3.OutputScream
OutputScream方法
方法说明
void write(int c)将指定的字节输出到输出流中
void write(byte[] buf)将字节数组输出到输出流中
void write(byte[] b,int off,int len)将字节数组从off位置开始,长度为len的字节数据输出到输出流中
void close()关闭输出流
void flush()强制把任何被缓冲的已写的输出数据输出到输出流


直接传文件名,默认是覆盖原有内容,在文件名后面加上true,就不会覆盖内容,而是在后面追加新内容
覆盖内容: BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(文件路径));
不覆盖: BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(文件路径,true));
文件写入:
public class Ch08 {
public static void main(String[] args) {
try {
//把程序和目标建立连接
//直接传文件名,默认是覆盖原有内容
//在文件名后面加上true,就不会覆盖内容,而是在后面追加新内容
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D:/iodemo/buff.txt",true));
//把字符串转成字节数组
String str="你好";
bos.write(str.getBytes());
//把数据完全冲刷到目标源中
bos.flush();
bos.close();
System.out.println("文件写入成功");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值