1.创建对象
package com.jiawa.io;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;public class ByteStreamDemo {public static void main(String[] args) throws IOException {//1.创建对象//2.写出 输出流 OutputStream//本地文件 FileFileOutputStream fos = new FileOutputStream("myio\\a.txt");//2.写出数据fos.write(57);fos.write(55);//3.释放资源fos.close();FileInputStream fos2 = new FileInputStream("myio\\a.txt");//2.写出数据System.out.println(fos2.read());//3.释放资源fos.close();}}
读取文件内容
public class ByteStreamDemo2 {public static void main(String[] args) throws IOException {//1.创建对象//2.写出 输出流 OutputStream//本地文件 File//true 关键字 实现文件继续追加编写FileInputStream in = new FileInputStream("myio\\a.txt");for (int i = 0; i <20; i++) {int read = in.read();System.out.println(read);}in.close();}
循环读取
public class ByteStreamDemo2 {public static void main(String[] args) throws IOException {//1.创建对象//2.写出 输出流 OutputStream//本地文件 File//true 关键字 实现文件继续追加编写FileInputStream in = new FileInputStream("myio\\a.txt");int b;while ((b=in.read())!=-1) {System.out.println((char) b);}in.close();}}