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

来源:互联网 发布:淘宝刷钻平台网站 编辑:程序博客网 时间:2024/05/21 10:52

   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。。 成功拷贝写入内容