字节流写数据的三种方式
void write(int b) 将指定的字节写入此文件输出流 一次写一个字节数据
void write(byte[] b) 将 b.length字节从指定的字节数组写入此文件输出流 一次写一个字节数组数据
void write(byte[] b, int off, int len) 将 len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流 一 次写一个字节数组的部分数据
FileOutputStream fos = new FileOutputStream(".\\src\\hello.txt"); fos.write(98); //将98对应的ASCII对应的字符写入hello.txt中 fos.write("ssss".getBytes()); //将ssss写入hello.txt fos.write("isme".getBytes(),0,4); //需要从第0位开始输入四个数据 fos.close();
以上代码执行后,会覆盖原有hello.txt中的内容
想要达成追加内容而不是覆盖时
FileOutputStream fos = new FileOutputStream(".\\src\\hello.txt",true); //在尾部加上参数true即可实现内容追加(非覆盖)
字节流读数据
FileInputStream fis = new FileInputStream(".\\src\\hello.txt"); int by; while ((by=fis.read())!=-1) { System.out.print((char)by); } fis.close();
fis.read():读数据
by=fis.read():把读取到的数据赋值给by
by != -1:判断读取到的数据是否是-1
以上代码执行后,将hello.txt中的内容读取到控制台
字节流复制文本文件
public static void main(String[] args) throws Exception { FileInputStream fileInputStream = new FileInputStream(".\\src\\hello.txt"); FileOutputStream fileOutputStream=new FileOutputStream(".\\src\\helloCopy.txt"); int a; while ((a=fileInputStream.read())!=-1){ //读写数据,复制文本文件(一次读取一个字节,一次写入一个字节) fileOutputStream.write(a); } fileInputStream.close(); fileOutputStream.close(); }
字节流读数据(一次读一个字节数组数据)
public static void main(String[] args) throws Exception { FileInputStream fileInputStream = new FileInputStream(".\\src\\hello.txt"); byte[] a=new byte[1024]; int len; while ((len=fileInputStream.read(a))!=-1){ //读写数据,复制文本文件(一次读取一个字节,一次写入一个字节) System.out.println(new String(a,0,len)); } fileInputStream.close(); }
以上在控制台输出了 hello.txt的内容,以上方法同样可复制图片
字节缓冲流
字节缓冲流介绍
lBufferOutputStream:该类实现缓冲输出流。 通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的每个字节导致底层系统的调用
BufferedOutputStream(OutputStream out) 创建字节缓冲输出流对象
lBufferedInputStream:创建BufferedInputStream将创建一个内部缓冲区数组。 当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次很多字节
BufferedInputStream(InputStream in) 创建字节缓冲输入流对象
字节缓冲输出流(输出到控制台)
public static void main(String[] args) throws Exception { BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(".\\src\\hello.txt")); bufferedOutputStream.write("A".getBytes()); bufferedOutputStream.write("1111".getBytes()); bufferedOutputStream.close(); }
字节缓冲流(一次读取一个字节数据,复制文件)
public static void main(String[] args) throws Exception { BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(".\\src\\hello.txt")); BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(".\\src\\helloCopy.txt")); int a; while((a=bufferedInputStream.read())!=-1){ bufferedOutputStream.write(a); } bufferedInputStream.close(); bufferedOutputStream.close(); }
字节缓冲流(一次读取一个字节数组数据,复制文件)
public static void main(String[] args) throws Exception { BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(".\\src\\hello.txt")); BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(".\\src\\helloCopy.txt")); byte[] a=new byte[1024]; //1024的整倍数 int len; while((len=bufferedInputStream.read(a))!=-1){ // bufferedOutputStream.write(a); bufferedOutputStream.write(a,0,len); } bufferedInputStream.close(); bufferedOutputStream.close(); }
浙公网安备 33010602011771号