java当中的IO(一)mars笔记【图解】---------(在编译io程序时,会有编译异常,所以我们就要try...cath处理异常)

   io操作的目标


IO的流向



IO中的核心类



核心类的核心方法



   int len 是   len是length



import java.io.*;

public class Test {
	public static void main(String args[]){
		//声明输入流引用
		FileInputStream fis = null;
		try{
			//生成代表输入流的对象
			fis = new FileInputStream("E:/baidu player/A.txt");
			//生成一个字节数组
			byte [] buffer = new byte[100];
			//调用输入流对象的read方法,读取数据
			fis.read(buffer, 0, buffer.length);
			for(int i = 0; i < buffer.length; i++){
				System.out.println(buffer[i]);
			}			
		}catch(Exception e){
			System.out.println(e);
		}
		
	}
}


输出结果:

后面输出的96个结果都是0 ,他们把前面几个0覆盖了

  为什么是abcd是 97 98  99  100 ?    ,因为abcd ASCII码对应的是 97 98  99  100




public class Test {
	public static void main(String args[]){
		//声明输入流引用     读
		FileInputStream fis = null;
		try{
			//生成代表输入流的对象
			fis = new FileInputStream("E:/baidu player/A.txt");			
			//生成一个字节数组
			byte [] buffer = new byte[100];
			//调用输入流对象的read方法,读取数据
			 fis.read(buffer, 5, buffer.length - 5);
			 for(int i = 0; i < buffer.length; i ++ ){
				 System.out.println(buffer[i]);
			 }
		}catch(Exception e){
			System.out.println(e);
		}
		
	}
}

输出

在第5个零后97 98 99 100 101  ,为什么前5个是0, 因为 偏移量offeset偏移量为5

为什么是abcd是 97 98  99  100  101 ?    ,因为abcd ASCII码对应的是 97 98  99  100 101



第三种情况  去除其余的量的方法

 for(int i = 0; i < buffer.length; i ++ ){
				 String s = new String(buffer);
				 //调用一个String对象的trim方法,将会去除掉这个字符串
				 //的首尾空格和空字符
				 //"   abc def   ">>> "abc def" (中间空格保留)
				 s = s.trim();
				 System.out.println(s);
			 }





import java.io.*;

public class Test {
	public static void main(String args[]){
		//声明输入流引用     读
		FileInputStream fis = null;
		
		//声明输出流的引用  写
		FileOutputStream fos = null;
		try{
			//生成代表输入流的对象
			fis = new FileInputStream("E:/baidu player/A.txt");
			//生存代表输出流的对象
			fos = new FileOutputStream("E:/baidu player/Write.txt");
			//生成一个字节数组
			byte [] buffer = new byte[100];
			//调用输入流对象的read方法,读取数据
			int temp = fis.read(buffer, 0, buffer.length);
			
			fos.write(buffer, 0, temp);
						
		}catch(Exception e){
			System.out.println(e);
		}
		
	}
}

输出:  该文件夹下 生成Write.txt文件。   A.txt内容是abcde ,     write里面是 A.txt的内容 abcde。。 成功拷贝写入内容


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值