OutputStream和InputStream实例

OutputStream和InputStream实例

 

  import java.io.File;

 

  import java.io.OutputStream;

 

  import java.io.FileOutputStream;

 

  public class OutputStreamDemo03

 

  {

 

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

 

  File file = new File( "H:"+File.separator+"programTest"+File.separator+"IO"+File.separator+"字符流与字符流"+File.separator+"test.txt");

 

  OutputStream out = null;

 

  out = new FileOutputStream(file,true); //是否追加

 

  String str = " 新增非覆盖!";

 

  byte[] b = str.getBytes();

 

  for(byte temp:b){

 

  out.write(temp);

 

  }

 

  //out.write(b);

 

  //out.flush();

 

  out.close();

 

  out = new FileOutputStream(file,true); //是否追加

 

  str = " \r\n我是换行的哦!"; // \r\n换行

 

  b = str.getBytes();

 

  for(byte temp:b){

 

  out.write(temp);

 

  }

 

  //out.write(b);

 

  //out.flush();

 

  out.close();

 

  }

 

  };

 

  import java.io.File;

 

  import java.io.InputStream;

 

  import java.io.FileInputStream;

 

  public class InputStreamDemo01

 

  {

 

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

 

  File file = new File( "H:"+File.separator+"programTest"+File.separator+"IO"+File.separator+"字符流与字符流"+File.separator+"test.txt");

 

  InputStream input = null;

 

  input = new FileInputStream(file); //是否追加

 

  String str = " 新增非覆盖!";

 

  byte[] b = new byte[1024]; //所有内容读到此数组中

 

  input.read(b);

 

  input.close();

 

  System.out.println("读取到的字符串为:"+new String(b));

 

  }

 

  };

 

  //此时内容已读取过来,不过有很多空格,因为b的大小为1024

 

  修改为

 

  import java.io.File;

 

  import java.io.InputStream;

 

  import java.io.FileInputStream;

 

  public class InputStreamDemo01

 

  {

 

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

 

  File file = new File( "H:"+File.separator+"programTest"+File.separator+"IO"+File.separator+"字符流与字符流"+File.separator+"test.txt");

 

  InputStream input = null;

 

  input = new FileInputStream(file); //是否追加

 

  String str = " 新增非覆盖!";

 

  byte[] b = new byte[1024]; //所有内容读到此数组中

 

  int len = input.read(b);

 

  input.close();

 

  System.out.println("读取到的字符串为:"+new String(b,0,len));

 

  }

 

  };

 

  高效方式

 

  byte[] b = new byte[(int)f.length()]; //开辟文件大小的空间

 

  input.read(b);

 

  input.close();

 

  System.out.println("读取到的字符串为:"+new String(b));

 

  byte[] b = new byte[(int)file.length()]; //所有内容读到此数组中

 

  for(int i=0;i<b.length;i++){

 

  b[i] = (byte)input.read();

 

  }

 

  input.close();

 

  System.out.println("读取到的字符串为:"+new String(b));

 

  带背景字体部分适用于知道文件大小时可用,input.read()读到文件末尾时值为-1,

 

  byte[] b = new byte[(int)f.length()]; //开辟文件大小的空间

 

  int len = 0;

 

  int temp = 0;

 

  while((temp = input.read()) != -1)

 

  {

 

  b[len] = (byte)temp;

 

  len++;

 

  }

 

  input.close();

 

  System.out.println("读取到的字符串为:"+new String(b,0,len));

 

  带横线部分适合于不知道所读内容大小时,通过input.read()读到文件末尾时值为-1进行判断输出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值