字节流与字符流的常用简单读写

本文提供了使用Java进行文件操作的实例代码,包括FileInputStream、FileOutputStream、BufferedReader及BufferedWriter的基本用法。

 

FileIputStream的实例

public void Input() throws IOException{

  FileInputStream fis=new FileInputStream("D:\\22\\myDoc\\space.txt");
  byte[] b=new byte[fis.available()];
  while((fis.read(b))!=-1){
    System.out.println(new String(b));
  }
  fis.close();

  }

FileOutputStream的简单实例

public void Output() throws IOException{


  FileOutputStream fos=new FileOutputStream("D:\\22\\myDoc\\space.txt");
  File file=new File("D:\\22\\myDoc\\space.txt");
  if(!file.exists()){
    file.mkdir();
    System.out.println("创建成功!");
  }
  String string="hahahhahaha";
  fos.write(string.getBytes());
  fos.flush();
  fos.close();
}

BufferedWriter的简单实例

public void Writer() throws IOException{


  FileOutputStream fos=null;//字节流
  OutputStreamWriter osw=null;//字节转字符流
  BufferedWriter bw=null;//字符流
  fos=new FileOutputStream("D:\\text\\text.txt");
  osw=new OutputStreamWriter(fos);
  bw=new BufferedWriter(osw);
  bw.write("无敌是多么寂寞!");
  bw.flush();
  osw.flush();
  fos.flush();
  bw.close();
  osw.close();
  fos.close();
}

BufferedReader的简单实例

public void Reader() throws IOException{


  FileInputStream fis=null;
  InputStreamReader isw=null;
  BufferedReader br=null;
  fis=new FileInputStream("D:\\text\\text.txt");
  isw=new InputStreamReader(fis);
  br=new BufferedReader(isw);
  String cstring="";
  while((cstring=br.readLine())!=null){
    System.out.println(c);
  }
  br.close();
  isw.close();
  fis.close();
}

 

转载于:https://www.cnblogs.com/BLJworld1994/p/5787173.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值