字节流代码 java_JAVA-基础-字节流

字节流

字节输出流OutputStream

OutputStream此抽象类,表示输出字节流的所有类的超类。操作的数据都是字节,定义了输出字节流的基本共性功能方法

输出流中定义都是写write方法,如下图:

cc7b9f04f4a93347fcd170c7886d55d9.png

FileOutputStream类

OutputStream有很多子类,其中子类FileOutputStream可用来写入数据到文件。

FileOutputStream类,即文件输出流,是用于将数据写入 File的输出流。

FileOutputStream类写入数据到文件中

public classFileOutputStreamDemo {public static void main(String[] args) throwsIOException {//需求:将数据写入到文件中。//创建存储数据的文件。

File file = new File("c:\\file.txt");//创建一个用于操作文件的字节输出流对象。一创建就必须明确数据存储目的地。//输出流目的是文件,会自动创建。如果文件存在,则覆盖。

FileOutputStream fos = newFileOutputStream(file);//调用父类中的write方法。

byte[] data = "abcde".getBytes();

fos.write(data);//关闭流资源。

fos.close();

}

}

给文件中续写和换行

我们直接new FileOutputStream(file)这样创建对象,写入数据,会覆盖原有的文件,那么我们想在原有的文件中续写内容怎么办呢?

继续查阅FileOutputStream的API。发现在FileOutputStream的构造函数中,可以接受一个boolean类型的值,如果值true,就会在文件末位继续添加。

给文件中续写数据和换行,代码演示:

public classFileOutputStreamDemo3 {public static voidmain(String[] args) {

File file= new File("c:\\file.txt");//定义FileOutputStream的引用

FileOutputStream fos = null;try{//创建FileOutputStream对象

fos = newFileOutputStream(file);//写出数据

fos.write("abcde".getBytes());

}catch(IOException e) {

System.out.println(e.toString()+ "----");throw new RuntimeException("文件写入失败,重试");

}finally{//一定要判断fos是否为null,只有不为null时,才可以关闭资源

if (fos != null) {try{

fos.close();

}catch(IOException e) {throw new RuntimeException("关闭资源失败");

}

}

}

}

}

字节输入流InputStream

int read():读取一个字节并返回,没有字节返回-1.

int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。

FileInputStream类构造方法

FileInputStream(File file)

FileInputStream(String name)

在读取文件中的数据时,调用read方法,实现从文件中读取数据

public classFileInputStreamDemo {public static void main(String[] args) throwsIOException {

File file= new File("c:\\file.txt");//创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。

FileInputStream fis = newFileInputStream(file);//读取数据。使用 read();一次读一个字节。

int ch = 0;while((ch=fis.read())!=-1){

System.out.pr }intln("ch="+(char)ch);//关闭资源。

fis.close();

}

}

读取数据read(byte[])方法

在读取文件中的数据时,调用read方法,每次只能读取一个,太麻烦了,于是我们可以定义数组作为临时的存储容器,这时可以调用重载的read方法,一次可以读取多个字符。

public classFileInputStreamDemo2 {public static void main(String[] args) throwsIOException {/** 演示第二个读取方法, read(byte[]);*/File file= new File("c:\\file.txt");//创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。

FileInputStream fis = newFileInputStream(file);//创建一个字节数组。

byte[] buf = new byte[1024];//长度可以定义成1024的整数倍。

int len = 0;while((len=fis.read(buf))!=-1){

System.out.println(new String(buf,0,len));

}

fis.close();

}

}

复制文件

读取一个已有的数据,并将这些读到的数据写入到另一个文件中。

e2bfce554317061766aeee95aaebe875.png

public classCopyFileTest {public static void main(String[] args) throwsIOException {//1,明确源和目的。

File srcFile = new File("c:\\YesDir\test.JPG");

File destFile= new File("copyTest.JPG");//2,明确字节流 输入流和源相关联,输出流和目的关联。

FileInputStream fis = newFileInputStream(srcFile);

FileOutputStream fos= newFileOutputStream(destFile);//3, 使用输入流的读取方法读取字节,并将字节写入到目的中。

int ch = 0;while((ch=fis.read())!=-1){

fos.write(ch);

}//4,关闭资源。

fos.close();

fis.close();

}

}

上述复制文件有个问题,每次都从源文件读取一个,然后在写到指定文件,接着再读取一个字符,然后再写一个,一直这样下去。效率极低。

缓冲数组方式复制文件

public classCopyFileByBufferTest {public static void main(String[] args) throwsIOException {

File srcFile= new File("c:\\YesDir\test.JPG");

File destFile= new File("copyTest.JPG");//明确字节流 输入流和源相关联,输出流和目的关联。

FileInputStream fis = newFileInputStream(srcFile);

FileOutputStream fos= newFileOutputStream(destFile);//定义一个缓冲区。

byte[] buf = new byte[1024];int len = 0;while ((len = fis.read(buf)) != -1) {

fos.write(buf,0, len);//将数组中的指定长度的数据写入到输出流中。

}//关闭资源。

fos.close();

fis.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值