输入输出原理图
一、字节输出流 OutputStream(接口)
FileOutputStream类 即文件输出流,是用于将数据写入 File
的输出流。
构造方法 new FileOutputStream() 有功能 如果指定文件不存在,创建 如果指定文件存在,覆盖
是换行 续写new FileOutputStream(,true)
package com.oracle.demo01; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class demo1 { public static void main(String[] args) throws InterruptedException, IOException { //明确目的地new FileOutputStream("D:\demo1018\f.txt")有功能 //如果指定文件不存在,创建 //如果指定文件存在,覆盖 //FileOutputStream fos=new FileOutputStream("D:\demo1018\f.txt"); //续写 FileOutputStream fos=new FileOutputStream("D:\demo1018\f.txt",true); /*写一个字节 fos.write(100);//ascii码值 */ /*//写一个字节数组 //正数走ascii码表 //负数走中文码表 //byte bytes[]={-66,-67,-68,-69}; //fos.write(bytes); //fos.write(bytes,1,2); */ fos.write("新年好".getBytes()); //换行 fos.write(" hello".getBytes()); //释放资源 fos.close(); } }
处理异常:注意 资源释放
package com.oracle.demo01; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class demo2 { public static void main(String[] args) { FileOutputStream fos=null; try { fos = new FileOutputStream("D:\demo1018\f.txt",true); fos.write("新年好".getBytes()); fos.write(" hello".getBytes()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if (fos!=null) { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
字节输入流InputStream
int read():读取一个字节并返回,没有字节返回-1.
int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。
FileInputStream 从文件系统中的某个文件中获得输入字节
FileInputStream类读取数据read方法
package com.oracle.demo01; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class demo3 { public static void main(String[] args) throws IOException { //明确数据源 FileInputStream fis=new FileInputStream("D:\demo1018\f.txt"); //读一个字节 //int len=fis.read(); //System.out.println((char)len); //循环一个字节一个字节读文件 int len=0; while((len=fis.read())!=-1) { System.out.print((char)len); } //释放资源 fis.close(); } }
读取数据read(byte[])方法
原理图
代码:
package com.oracle.demo01; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Demo4 { public static void main(String[] args) throws IOException { //明确数据源 FileInputStream fis=new FileInputStream("D:\demo1018\f.txt"); //创建字节数组 byte bytes[]=new byte[1024]; //一个字节一个字节数组读 //System.out.print(new String(bytes,0,len)); //System.out.print(len); //循环一个字节一个字节数组读 int len=0; while ((len=fis.read(bytes))!=-1) { System.out.print(new String(bytes,0,len)); System.out.print(len); } fis.close(); } }
字节流复制
原理图:
代码
package com.oracle.demo01; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Copy { //字节流复制文件 public static void main(String[] args) throws IOException { //明确数据源 FileInputStream fis=new FileInputStream("D:\demo1018\f.txt"); //明确目的地 FileOutputStream fos=new FileOutputStream("D:\demo1018\a\f.txt"); FileOutputStream fos1=new FileOutputStream("D:\demo1018\a\dd.txt"); //一个一个字节复制 int len=0; /*while((len=fis.read())!=-1){ fos.write(len); }*/ byte bytes[]=new byte[1024]; len=0; while ((len=fis.read(bytes))!=-1) { fos1.write(bytes,0,len); } //释放资源 fis.close(); fos.close(); fos1.close(); } }