IO流的输入/输出是从程序的角度来讲的。(从外部(磁盘)流到内存或者程序中,称为输入流【数据从程序外流向程序】。从内存输出到磁盘中,称为输出流【数据从程序流出】)。
1、按照方向来分类:输入流(InputStream)、输出流(OutputStream)。
2、按照类型来分类:字节流(XXXStream)、字符流(Writer、Reader)。
3、按照操作方式来分类:节点流、过滤流
4、转换流(InputStremReader、OutPutStreamReader)
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class InputStreamTest { public static void main(String[] args) { //1、定义一个文件流 FileInputStream fis = null; try { //2、创建流 fis = new FileInputStream("d:/1.txt"); //3、创建一个文件输入流 byte[] array = new byte[1024]; //4、创建一个字节数组来存取读取的信息 int len = 0; //5、循环读取数据,只要len大于0说明读取到元素,可以直接对元素进行操作 while((len = fis.read(array))!=-1) { System.out.write(array,0,len); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(fis != null) { //读取完成后使用close方法关闭流 fis.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class CopyFile { public static void main(String[] args) { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("d:/1.txt"); //创建一个文件输出流 fos = new FileOutputStream("d:/temp.txt"); byte[] array = new byte[1024]; int len = 0; //将数据通过输入流读取到程序中 while((len = fis.read(array)) != -1) { //将数据通过输出流输出,此时是一个文件输出流,就把数据输出到文件中 fos.write(array, 0, len); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { //关闭流 try { if(fos != null) { fos.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(fis != null) { fis.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class CharacterTest { public static void main(String[] args) { BufferedReader br = null; PrintWriter pw = null; try { br = new BufferedReader(new FileReader("d:/1.txt")); //使用文件字符流写文件,加上缓冲流提高效率,写用PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("d:/temp.txt"))); String str = null; while((str = br.readLine()) != null) { pw.println(str); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if(pw != null) { pw.close(); } if(br != null) { try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FilterTest { public static void main(String[] args) { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //在文件输入流的基础上加入一个缓冲流,以此提高效率 bis = new BufferedInputStream(new FileInputStream("C:\\Users\\asus\\Pictures\\1.jpg")); bos = new BufferedOutputStream(new FileOutputStream("d:/temp.jpg")); byte[] array = new byte[1024]; int len = 0; while((len = bis.read(array)) != -1) { bos.write(array, 0, len); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { //对于缓冲流,一定要关闭,不然如果缓冲区不满就不会刷新,可以通过bos.flust()刷新缓冲 try { if(bis != null) { bis.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(bos != null) { bos.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Consoletest { public static void main(String[] args) { BufferedReader br = null; try { //把字节流转换为字符流方便进行字符处理 br = new BufferedReader(new InputStreamReader(System.in)); String str = null; while((str = br.readLine()) != null) { if("exit".equals(str)) { break; } System.out.println(str); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(br != null) { br.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }