字节流介绍

字节流

字节流顶层的抽象inputstream和outputstream

字节输入流:InputStream

public sbstract class InputStream implements Closeable

InputStream是抽象的基类,必须使用其子类来使用。
核心方法:
int read() throws IOException:每次读取的是一个字节的数据,以ASCII码的形式返回,读取到文件的末尾返回-1;
int read(byte b[ ]) throws IOException:每次读取的数据读取到byte数组中,返回读取数据的有效个数,读到文件末尾处返回-1;
int readBytes(byte b[], int off, int len) throws IOException:每次读取的数据读到byte数组中,从偏移量offer开始,长度为len;

FileInputStream

read()方法示例:

public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  FileInputStream file=new FileInputStream("C:\\Users\\86181\\Desktop\\IO\\text2.txt");
  int read=file.read();
  System.out.println(read);
 }

int read(byte b[ ]) 方法示例:

public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  FileInputStream file=new FileInputStream("C:\\Users\\86181\\Desktop\\IO\\text2.txt");
  byte []bytes=new byte[10];
  **int read=file.read(bytes);**
  System.out.println("数据个数:"+read);
  System.out.println("数据内容:"+new String(bytes,0,read));//打印的数据从头到第read(数据个数)个;
 }

int readBytes(byte b[], int off, int len)方法示例:

public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  FileInputStream file=new FileInputStream("C:\\Users\\86181\\Desktop\\IO\\text2.txt");
  byte []bytes=new byte[10];
  **int read=file.read(bytes,3,5);**//从第3个数据起读5个数据
  System.out.println("数据个数:"+read);
  System.out.println("数据内容:"+new String(bytes));
 }

字节输出流:OutputStream

public abstract class OutPutStream implements Closeable,Flushable;

OutputStream是抽象的基类,必须使用其子类来使用。
核心方法:
void flush() throws IOException:刷新输出流并强制缓冲的字节被写出。
void write(int b) throws IOException:参数为ASCII码,将int值写入到输出流中,默认会覆盖原内容。
void write(byte b[ ]) throws IOException:将字节数组内容写入到输出流中,默认会覆盖原内容。
void write(byte b[ ],int off,int len) throws IOException:将字节数组从偏移位置offer写入len长度的数据到输出流中,默认会覆盖原内容。

FileOutputStream

write(int b)方法示例:

public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  FileOutputStream outputstream=new FileOutputStream("C:\\Users\\86181\\Desktop\\IO\\text2.txt");
  **outputstream.write(97);**    //97为ASCLL码,对应的字符为a,也可以直接给‘a’,他会直接转化
  outputstream.close();   //写完后关闭此流
 }

write(byte b[ ])方法示例:

public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  FileOutputStream outputstream=new FileOutputStream("C:\\Users\\86181\\Desktop\\IO\\text2.txt");
  **outputstream.write("abcde".getBytes());**   //调用getBytes()将字符串转化为byte数组传入
  outputstream.close();  //写完后关闭此流
 }

void write(byte b[ ],int off,int len)方法示例:

public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  FileOutputStream outputstream=new FileOutputStream("C:\\Users\\86181\\Desktop\\IO\\text2.txt");
  **outputstream.write("abcde".getBytes(),1,4);**   //从数组的第1一个位置,传入长度为4的数据
  outputstream.close();  //写完后关闭此流
 }

FileInputStream和FileOutStream

FileInputStream:文件输入流

FileInputStream主要是从磁盘上读取内容

//创建FileInputStream的两种类型
public FileInputStream(String name)
public FileInputStream(File file)

当传入的文件不存在时,会抛出FileNotFoundException异常。
int read()可通过循环来读取文件:

public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  FileInputStream inputstream=new FileInputStream("C:\\Users\\86181\\Desktop\\IO\\text2.txt");
  int i=0;
  while((i=inputstream.read()) != -1)    //说明数据没读完
  {
   System.out.print((char)i);   //通过强转直接输出字符
  }
 }

int read(byte bytes[ ])通过循环来读取文件:
(易错点:由于每次读取都会覆盖上一次读取到byte数组,最后一次不存在整数倍形式读取,就会造成一部分空间未被新数据覆盖,而产生脏数据)
解决方法:每次循环清空数组或每次取数组第0到第i个数据

public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  FileInputStream inputstream=new FileInputStream("C:\\Users\\86181\\Desktop\\IO\\text2.txt");
  byte[] bytes=new byte[2];
  int i=0;
  while((i=inputstream.read(bytes)) != -1)
  {
   **System.out.print(new String(bytes,0,i));**  //每次取bytes数组第0到第i个数据,否则就会产生上述易错点
  }
 }

FileOutputStream:文件输出流

FileOutputStream将数据写入到磁盘使用流该实例操作

public FileOutputStream(String name)  //以字符串形式传入路径
public FileOutputStream(String name,boolean append)  //默认时false:覆盖原内容; true:以追加的形式将新增内容写在末尾
public FileOutputStream(File file)   //以File实例形式传入路径
public FileOutputStream(File file,boolean append)

1.如果指定路径不存在,会自动创建该文件。
2.如果file存在,append参数没有传或者给定false,会清空原内容;如果file存在,append参数给定true,会追加内容。

public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  FileOutputStream outputstream=new FileOutputStream("C:\\Users\\86181\\Desktop\\IO\\text2.txt",true);
  outputstream.write("212".getBytes());
 }

例题:完成复制功能(source:源文件的地址,dest:复制的位置)

public static void copyFile(String sourse,String desc) throws IOException {
  File file=new File(sourse);
  FileInputStream in=new FileInputStream(file);
  String dexcPath=desc+File.separator+file.getName();
  FileOutputStream out=new FileOutputStream(dexcPath);
  byte []bytes = null;
  int i=0;
  while((i=in.read(bytes))!=-1)
  {
   out.write(bytes,0,i);
  }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值