【骐程】输入输出流FileInputStream和FileOutputStream

19 篇文章 0 订阅

1对于两种流的认识:

字节流:可以操作任何文件
字符流:操作纯文本文件

**2抽象基类 字节流 字符流

输入流 Inputerstream Reader
输出流 Outputstream Writer**

3操作数据流具体看代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class OutPut {
	public static void main(String[] args) {
		//造文件
		File file = new File("C:\\Users\\Administrator\\Desktop\\今天学习1211\\12.22\\hellow.txt");
		OutPut op = new OutPut();
		op.outStream(file);
		op.inPut(file);
	}
	
	public void  outStream(File file){
		FileOutputStream fos = null;
		try {
		//造流
		fos = new FileOutputStream(file);	
		//写数据
		int score = 100; 
		String string = "你好啊!加油!"+score;
		fos.write(string.getBytes());
		System.out.println("数据写入成功!");
		
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			//关闭流
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		
	}
}
	public void inPut(File file) {
		//创建流
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
			//创建读入数组的流
			byte[] by = new byte[1024];
			StringBuffer sBuffer = new StringBuffer();
			int len = 0;
			while((len = fis.read(by))!=-1) {
				String string = new String(by, 0, len);
				sBuffer.append(string);//拼接流
			}
			System.out.println(sBuffer);
			System.out.println("over!");
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//关闭流
			if(fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

ps:对于写入流fos.write(int a)这个方法比较的的特别,它写进去的数字会先编译为二进制,然后再有ASC码经行写入数据,而且一次只能写入最大七位二进制数,所以对于数组如果超过七位就不能呈现正确的数字。
我们一般都用byte[] 来处理输入和输出!字节流可以处理任何可存出的数据,计算机中文件、数据等都是以字节形式存在的!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值