• FileOutputStream的三个write方法举例


    第一个方法write(byte[] b)举例:

    参数需要是字节数组的,字符串.getBytes()将字符串变为字节数组

    public static void main(String[] args) {
            
            File file = new File("e:\12332.txt");
            try {
                FileOutputStream fos = new FileOutputStream(file,true);
                fos.write("www.sina.com.cn".getBytes());
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }    

    第二个方法write(int b) ,会把b对应的字符输出97:a

    public static void main(String[] args) {
            
            File file = new File("e:\12332.txt");
            try {
                FileOutputStream fos = new FileOutputStream(file);
                int[] b = {49,50,97,98,99};
                for(int i = 0;i<b.length;i++){
                fos.write(b[i]);
                }
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }    

    第三个方法write(byte[] b,int off,int len)

     fis.read(b)是读取b字节数组总数的字节,当最后不够数组长度的时候就读出了剩下不够的字节总数

    给了c

    同样write方法写b字节数组,不够的话,就写剩下的总数,长度是c

    public static void main(String[] args) {
            
            File file = new File("e:\12332.txt");
            try {
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                DataOutputStream dos = new DataOutputStream(bos);
                
                FileInputStream fis = new FileInputStream(new File("e:\12223.txt"));
                byte[] b = new byte[1024];
                int c = 0;
                while((c=fis.read(b))!=-1){
                    dos.write(b, 0, c);;
                }
                fis.close();
                dos.close();
                bos.close();
                fos.close();
                System.out.println("运行到这");
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            
            
            
            
        }
  • 相关阅读:
    19-10-31-B
    19-10-30-Night-V
    19-10-30-C
    19-10-29-Night-X
    19-10-29-Z
    19-10-28-A
    19-10-27-S
    19-10-26-Night-D
    留言板
    优秀博客存档
  • 原文地址:https://www.cnblogs.com/lonely-buffoon/p/5577732.html
Copyright © 2020-2023  润新知