Java_137_IO_程序读(InputStream)_FileInputStream_程序写(OutputStream)_FileOutStream_FileOutStream(路径,追加或覆盖)

程序-读

package IOStudy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 处理数据角度
 * 字节流:音频,视频,excel
 * 字符流:文本
 * 
 * 能使用字符流就能使用字节流,但能使用字节流不一定能使用字符流.
 * 
 * 以程序为中心,进来叫输入,出去叫输出
 * 
 * 节点流,处理流
 * 始终处于第一线的是节点流,为了提升性能我们包装叫做处理流,没有节点流处理流发挥不了任何的作用.
 * 
 * 节点流
 * ByteArray../File..
 * 
 * 处理流
 * DataInputStream/BufferedInputStream/Object..
 * 
 * 四个抽象类
 * InputStream 字节输入流的父类,数据单位为字节 int read()/void close()
 * OutputStream 字节输出流的父类,数据单位为字节 void write(int)/void flush()/void close()
 * Reader 字符输入流的父类,数据单位为字符 int read()/void close()
 * Writer 字符输出流的父类,数据单位为字符 void write(String)/void flush()/void close()
 * 
 * Java虚拟机不直接对接文件,只能向操作系统进行申请
 * Java->OS->文件
 * Closeable方法通知操作系统可以关闭这个资源
 * 
 * OutputStream
 * Closeable释放资源,Flushable(手动刷新) 避免数据驻留在内存中
 * 
 * 
 * FileOutputStream处理一切
 * FileWriter处理字符
 * @author pmc
 */

/**
 * 第一个程序:理解操作步骤
 * 分段读取
 * 1.创建源
 * 2.选择流
 * 3.操作
 * 4.释放资源
 * @author pmc
 *
 */
public class IOAbsrtractTest5 {
	public static void main(String[] args){
		//1.创建源
		File src=new File("txt.txt");
		//2.选择流
		InputStream is=null;
		try {
			is=new FileInputStream(src);
			//3.操作
//			int temp=is.read();
//			System.out.println(temp);
			byte[] flush=new byte[1024];//缓冲容器
			int len=0;//接收长度
			while((len=is.read(flush))!=-1){//is.read(byte[]) 返回一个读取长度 源码: return read(b, 0, b.length);
//				System.out.print(len);
				String str=new String(flush,0,len);
				System.out.print(str);
			}
//			System.out.print(len);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			//4.释放资源
			try {
				if(is!=null){//避免空指针异常
					is.close();	
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

程序-写

package IOStudy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 处理数据角度
 * 字节流:音频,视频,excel
 * 字符流:文本
 * 
 * 能使用字符流就能使用字节流,但能使用字节流不一定能使用字符流.
 * 
 * 以程序为中心,进来叫输入,出去叫输出
 * 
 * 节点流,处理流
 * 始终处于第一线的是节点流,为了提升性能我们包装叫做处理流,没有节点流处理流发挥不了任何的作用.
 * 
 * 节点流
 * ByteArray../File..
 * 
 * 处理流
 * DataInputStream/BufferedInputStream/Object..
 * 
 * 四个抽象类
 * InputStream 字节输入流的父类,数据单位为字节 int read()/void close()
 * OutputStream 字节输出流的父类,数据单位为字节 void write(int)/void flush()/void close()
 * Reader 字符输入流的父类,数据单位为字符 int read()/void close()
 * Writer 字符输出流的父类,数据单位为字符 void write(String)/void flush()/void close()
 * 
 * Java虚拟机不直接对接文件,只能向操作系统进行申请
 * Java->OS->文件
 * Closeable方法通知操作系统可以关闭这个资源
 * 
 * OutputStream
 * Closeable释放资源,Flushable(手动刷新) 避免数据驻留在内存中
 * 
 * 
 * FileOutputStream处理一切
 * FileWriter处理字符
 * @author pmc
 */

/**
 * 文件字节输出流
 * 1.创建源
 * 2.选择流
 * 3.操作
 * 4.释放资源
 * @author pmc
 *
 */
public class IOAbsrtractTest6 {
	public static void main(String[] args){
		//1.创建流
		File src=new File("dest.txt");
		//2.选择流
		OutputStream os=null;
		try{
			os=new FileOutputStream(src,true);//自动创建该文件,true追加,false覆盖
			//3.写内容
			String msg="io is so easy ";
			byte[] by=msg.getBytes();
			os.write(by,0,by.length);
			os.flush();
			//3.1读
			InputStream in=new FileInputStream(src);
			byte[] temp=new byte[1024];
			int s=0;
			s=in.read(temp);
			String str=new String(temp,0,s);
			System.out.println(str);
			in.close();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			//4.释放资源
			if(os!=null){
				try{
					os.close();
				}catch(Exception e){
					e.printStackTrace();		
				}
				
			}
		}
	}
}

程序-写 练习

package IOStudy;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class IOAbsrtractTest8 {
	public static void main(String[] args){
		File src=new File("temp.txt");
		OutputStream out=null;
		try {
			out=new FileOutputStream(src,true);
			String str="学习使我快乐\n";
			out.write(str.getBytes());
			out.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr_Pmc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值