IO流大集合
1.创建文件
2.IO流及其分类
1.字节流
3.输入流
4.输出流
public class CreatFile { public static void main(String[] args) { creat(); } static void creat(){ String path ="D:\\a\\qwqe.txt"; FileOutputStream f =null; String str ="qweqweqweqwe"; try { f=new FileOutputStream(path,true); f.write(str.getBytes(),0,4); System.out.println("1"); } catch (IOException e) { throw new RuntimeException(e); } } }
5.输入加输出拷贝图片
public class fileCopy { public static void main(String[] args) { String inputpass ="D:\\pic\\bg3.jpg"; String outputpass ="D:\\pic\\a\\bg3.jpg"; FileInputStream fileInputStream =null; FileOutputStream fileOutputStream =null; int readline; byte [] bytee =new byte[1024]; try { fileInputStream=new FileInputStream(inputpass); fileOutputStream =new FileOutputStream(outputpass); while ((readline= fileInputStream.read(bytee))!=-1){ fileOutputStream.write(bytee); } }catch (Exception e){ e.printStackTrace(); }finally { if (fileInputStream!=null){ try { fileInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } if (fileOutputStream!=null){ try { fileOutputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } }
2.字符流
public class zifuliu { public static void main(String[] args) { String filepath ="D:\\a\\a.txt"; FileReader fileReader=null; int raedline=0; char [] bytee =new char[1024]; try { fileReader=new FileReader(filepath); while ((raedline= fileReader.read(bytee))!=-1){ System.out.println(new String(bytee,0,raedline)); } }catch (Exception e){ e.printStackTrace(); }finally { if (fileReader!=null){ try { fileReader.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } }
写完之后用close最好,falsh也可以;
3.节点流、处理流
节点流:
对特定的数据(文件、数组、管道、字符串)进行操作;
处理流(包装流):
包装节点流使之功能更加强大,其中的一个属性是节点流,可以是数组,数据源等 ,只要是节点流都可以放但是只能放一个;