流与文件(java流概述)

【在忙中偷闲,总结些东西,既方便自己,也方便有需要的你们,利人利己何乐而不为!

  再编辑流之前,先来个前言,有需要看看,不需要的自动略过】

[前言:Socket套接字,它也是java中一个类,由它来实现两台主机的交互、由它搭建起的一个输入输出流的管道;例如:浏览器向服务器发送请求,需要Socket架起输出流管道,而服务器响应浏览器发送的请求,则需要由Socket架起输入流管道,由输入流把【浏览器发送的请求信息===>服务器进行的接收请求===>处理请求给予响应】应响应回浏览器的信息发送到浏览器,这样浏览器就显示了相应的内容咯]




1.Java流概述:

(1).InputStream(字节输入流)
InputStream是字节输入流,InputStream是一个抽象类,所有继承了InputStream的类都是字节输入流,主要了解以下子类即可:


例:
public static void main(String[] args) {
try {
//定义一个输入字节流
InputStream in = new FileInputStream("c:/cc.txt");
//只读取一个字节
System.out.println(in.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


例:正确的读取文件内容
public static void main(String[] args) {
InputStream in = null;
try {
//定义一个输入字节流
in = new FileInputStream("c:/cc.txt");
//创建一个临时变量来保存读取的字节
int temp = 0;
//不能这样写
// while(in.read()!=-1){
// System.out.println(in.read());
// }
//这样写可以读取所有的字节
while((temp=in.read())>0){
System.out.println(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//释放资源
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}




(2).OutputStream(字节输出流)
所有继承了OutputStream都是字节输出流


注意:当调用flush方法时,在close关闭流时就不自动调用flush方法了,否则会自动调用flush方法。




例:
public static void main(String[] args) {
OutputStream out = null;
try {
//创建文件的输出字节流
out = new FileOutputStream("c:/dd.txt");
//写入
out.write(123);
//强制清空输出流
out.flush();
System.out.println("success");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//释放资源
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

(3).字符流:每次读取2个字节(16位),reader  writer


Reader(字符输入流)
所有继承了Reader都是字符输如流.


例:
public static void main(String[] args) {
Reader reader = null;
try {
//创建字符输入流
reader = new FileReader("c:/cc.txt");
//读取文件的内容,按字符读取
System.out.println(reader.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//释放资源
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


(4).Writer(字符输出流)
所有继承了Writer都是字符输出流


例:
public static void main(String[] args) {
Writer writer = null;
try {
//创建字符输出流
writer = new FileWriter("c:/ff.txt");
//添加内容
writer.write("java1");
//追加内容
writer.append('a');
//强制清空输出流
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//释放资源
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}




参考源代码:字符输出流可以调节boolean类型来决定是否可以把内容追加到原文件中
public FileWriter(String fileName, boolean append) throws IOException {
        super(new FileOutputStream(fileName, append));
}


例:
public static void main(String[] args) throws IOException {
//true表示可以追加到原文件中,false表示不能追加到原文件中
Writer w = new FileWriter("c:/fff.txt",true);
//追加的内容
w.append("hello");
w.flush();
w.close();
}

(5).文件流主要分为:文件字节输入流、文件字节输出流、文件字符输入流、文件字符输出流
FileInputStream(文件字节输入流)


文件的复制:
public static void main(String[] args) {
//创建文件的输入和输出流
InputStream in = null;
OutputStream out = null;
try {
//创建文件类型的实现类
in = new FileInputStream("c:/cc.txt");
out = new FileOutputStream("d:/xiu.txt");
//循环把文件写到输出流中
int tmp = 0;
//复制文件
while((tmp=in.read())>0){
out.write(tmp);
}
//输出流要强制刷新
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//资源释放
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

(6).缓冲流:
缓冲流主要是为了提高效率而存在的,减少物理读取次数,缓冲流主要有:BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter,并且BufferedReader提供了实用方法readLine(),可以直接读取一行,BufferWriter提供了newLine()可以写换行符。

例:缓冲流会涉及【装饰模式】
public static void main(String[] args) throws IOException {
//创建输入输出类型
InputStream in = null;
OutputStream out = null;
//创建输入和输出缓冲流对象
in = new BufferedInputStream(new FileInputStream("c:/a.txt"));
out = new BufferedOutputStream(new FileOutputStream("c:/b.txt"));
//复制文件
int tmp=0;
while((tmp=in.read())!=-1){
out.write(tmp);
}
//释放资源
out.flush();
in.close();
out.close();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值