IO流

IO流_第1张图片

文件流

FileInputStream FileOutputStream
写出数据

```java public class demo { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("fos.txt",true); // 数据追加
byte[] b = "哈哈哈哈".getBytes(); fos.write(b);

fos.close();
}

} ```

读取数据

```java public class demo { public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("fos.txt");

byte[] b=new byte[10];
    int len;

    // 读取b的长度个字节到数组中,返回读取到的有效字节个数,读取到末尾时,返回-1
    while((len=fis.read(b))!=-1){
        System.out.println(new String(b,0,len));
    }

    fis.close();
}

} ```

复制图片

```java public class demo { public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("C:/Users/hp/Desktop/1.png");

FileOutputStream fos = new FileOutputStream("aa.png");

    byte[] b=new byte[10];
    int len;

    while((len=fis.read(b))!=-1){
        fos.write(b, 0, len);
    }

    fos.close();
    fis.close();
}

} ```

Reader Writer
读取数据

```java public class demo { public static void main(String[] args) throws IOException { FileReader reader = new FileReader("fos.txt");

char[] cbuf=new char[10];
    int len;

    while((len=reader.read(cbuf))!=-1){
        System.out.println(new String(cbuf,0,len));
    }

}

} ```

写出数据

```java public class demo { public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("fos.txt",true);

char[] array = "哈哈哈哈哈".toCharArray();

    fw.write(array);
    fw.flush();
    fw.close();

}

} ```

缓冲流

```java public class demo { public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new FileReader("fos.txt"));

   String line  = null;

   while ((line = br.readLine()) != null) {   // 读取一行数据
       System.out.println(line);
   }
   br.close();

} } ```

你可能感兴趣的:(IO流)