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

来源:互联网 发布:库管软件 编辑:程序博客网 时间:2024/05/18 00:39
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();
}
}
}







原创粉丝点击