zoukankan      html  css  js  c++  java
  • 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();
            }
            
            
            
            
            
        }
  • 相关阅读:
    bzoj 1017 魔兽地图DotR
    poj 1322 chocolate
    bzoj 1045 糖果传递
    poj 3067 japan
    timus 1109 Conference(二分图匹配)
    URAL 1205 By the Underground or by Foot?(SPFA)
    URAL 1242 Werewolf(DFS)
    timus 1033 Labyrinth(BFS)
    URAL 1208 Legendary Teams Contest(DFS)
    URAL 1930 Ivan's Car(BFS)
  • 原文地址:https://www.cnblogs.com/lonely-buffoon/p/5577732.html
Copyright © 2011-2022 走看看