FileInputStream与FileoutputStream

由帮助文档知FileInputStream与FileoutStream分别集成自InputStream和OutputStream;

FileInputStream(File file)用于从文件读取数据;FileOutputStream(File file)用于将数据写入文件。

具体流程包括:

1.找到并确定源文件;

2.打开流;

3.关闭流;

4.操作流。

由帮助文档:FileoutputStream含有write方法有三个:

void

write(byte[] b)

Writes b.length bytes from the specified byte array to this file output stream.

void

write(byte[] b, int off, int len)

Writes len bytes from the specified byte array starting at offset off to this file output stream.

void

write(int b)

Writes the specified byte to this file output stream.

用代码实现:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos=new FileOutputStream("D:\\666\\he1,.java");
      //1
        fos.write(89);
       fos.write(90);
       //2
       byte[] a={91,92,93,94};
       fos.write(a);
       //3
       byte[] b={95,96,97,98,99};
       fos.write(b,2,2);
  fos.close();
    }

}

运行后文件内容为:YZ[\]^ab

 FileOutputStream fos=new FileOutputStream("D:\\666\\he1,.java");在 FileOutputStream中含有一个判断,它会将"D:\\666\\he1,.java"自动生成一个文件对象。作用相当于FileOutputStream fos1=new FileOutputStream(new File("D:\\666\\he1.java"));

FileOutputStream fos=new FileOutputStream("D:\\666\\he1,.java");
FileOutputStream fos1=new FileOutputStream(new File("D:\\666\\he1.java"));

此两行作用一样。

FileInputStream:

由帮助文档:FileInputStream也含有三个read方法:

int

read()

Reads a byte of data from this input stream.

int

read(byte[] b)

Reads up to b.length bytes of data from this input stream into an array of bytes.

int

read(byte[] b, int off, int len)

Reads up to len bytes of data from this input stream into an array of bytes.

1.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args) throws IOException {
        FileInputStream fis=new FileInputStream("D:\\666\\he1,.java");
        int a=0;while((a=fis.read())!=-1)
        {
            System.out.println(a);
fis.close;

        }
    }
}

打印结果:

89
90
91
92
93
94
97
98

2。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args) throws IOException {
        FileInputStream fis=new FileInputStream("D:\\666\\he1,.java");
        FileInputStream fis1=new FileInputStream("D:\\666\\he1,.java");
     byte[] a=new byte[10];
     int f=fis.read(a);
     System.out.println(new String(a));
        byte[] b=new byte[8];
        int f1=fis1.read(b,0,5);
        System.out.println(new String(b));
        fis.close();
    }
}

结果:

YZ[\]^ab  
YZ[\]   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值