文件流的应用

InputStream输入流 文件从硬盘读取到内存  
OutputStream输出流 文件从内存写入到硬盘

一批代码:
File file=new File("f://a.txt");
InputStream inputStream=new FileInputStream(file);
byte b[]=new byte[1024];
inputStream.read(b);
inputStram.close();
syso(new String(b));//1024个字节多余空字符代替

优化:
File file=new File("f://a.txt");
InputStream inputStream=new FileInputStream(file);
byte b[]=new byte[1024];
int len= inputStream.read(b);
inputStram.close();
syso(new String(b,0,len));//截取0-len个

优化:
File file=new File("f://a.txt");
InputStream inputStream=new FileInputStream(file);
int filelength=(int)file.length();//强制类型转换
byte b[]=new byte[filelength];
inputStream.read(b);
inputStram.close();
syso(new String(b));

一个字节读取代码:
File file=new File("f://a.txt");
InputStream inputStream=new FileInputStream(file);
int filelength=(int)file.length();//强制类型转换
byte b[]=new byte[filelength];
int len=0;
int temp=0;
while((temp=inputStram.read())!=-1){
   b[len++]=(byte)temp;
 }
inputStram.close();
syso(new String(b));

===============================
一次性写入代码:
File file=new File("c://a.txt");
OutputStream out=new FileOutputStream(file);
String str="aaaaa";
byte b[]=str.getBytes();
out.write(b);
out.close();

追加写入代码:
File file=new File("c://a.txt");
OutputStream out=new FileOutputStream(file,true);
String str="aaaaa";
byte b[]=str.getBytes();
out.write(b);
out.close();

BufferedInputStream

代码:
public static void stream() throws Exception{
  InputStream inputStream=new FileInputStream("c://a.doc");
  OutputStream outputStream=new FileOutputStream("c://复制的b.doc");
  int b=0;
  long startTime=System.currentTimeMillis();//获取当前时间
  while((b=inputStream.read())!=-1){
    outputStream.write(b);
  }
  inputStream.close();
  outputStream.close();
  long endTime=System.currentTimeMillis();//获取当前时间
  syso(endTime-startTime);
}

public static void main(String []args) throws Exception{
  stream();
}


BufferedOutputStream
代码:
public static void bufferStream() throws Exception{
  BufferedInputStream inputStream=new BufferedInputStream(new FileInputStream("c://a.doc"));
  BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutStream("c://复制的b.doc"));
  int b=0;
  long startTime=System.currentTimeMillis();//获取当前时间
  while((b=inputStream.read())!=-1){
    outputStream.write(b);
  }
  inputStream.close();
  outputStream.close();
  long endTime=System.currentTimeMillis();//获取当前时间
  syso(endTime-startTime);
}

public static void main(String []args) throws Exception{
  bufferStream();
}


Reader
一批读取代码 
public  static void main(String []args){
 File file=new File("c://a.txt");
 Reader reader=new FileReader(file);
 char c[]=new char[1024];
 reader.read(c);
 reader.close();
 syso(new String(new String(c)));
}

优化:

public  static void main(String []args){
 File file=new File("c://a.txt");
 Reader reader=new FileReader(file);
 char c[]=new char[1024];
 int len=reader.read(c);
 reader.close();
 syso(new String(new String(c,0,len)));
}

一个字节读取
public  static void main(String []args){
 File file=new File("c://a.txt");
 Reader reader=new FileReader(file);
 char c[]=new char[1024];
 int len=0;
 int temp=0;
 while((temp=reader.read())!=-1){
  c[len++]=(char)temp;
 }
 reader.close();
 syso(new String(new String(c,0,len)));
}

Writer
代码
public static void main(String []args){
 File file=new File("c://a.txt");
 Writer out=new FileWriter(file,true);
 out.writer;
 out.close;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

py编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值