FileInputStream与FileOutputstream

本文演示了如何使用Java的FileOutputStream和FileInputStream类进行文件读写操作,包括创建文件、写入文本内容和读取文件内容。
 package cn.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileTest {
 public static void main(String[] args) {
  File file = new File("f://word.text");
  try {
   FileOutputStream out = new FileOutputStream(file);
   byte[] buy = "我有一只小毛驴,我从来也不骑。".getBytes();
   out.write(buy);
   out.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  try {
   FileInputStream in = new FileInputStream(file);
   byte byt[] = new byte[1024];
   int len = in.read(byt);
   System.out.println("文本中的信息是:" + new String(byt, 0, len));
   in.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  /*
   * if(file.exists()){ String name=file.getName(); Long
   * length=file.length(); boolean hidden=file.isHidden();
   * System.out.println("文件名:"+name); System.out.println("文件内容长:"+length);
   * System.out.println("文件是否隐藏:"+hidden); /*file.delete();
   * System.out.println("文件已删除");
   * 
   * }else{ try { file.createNewFile(); System.out.println("文件已经创建"); }
   * catch (IOException e) { // TODO Auto-generated catch block
   * e.printStackTrace(); } }
   */
 }/*
   * else{ System.out.println("文件不存在"); } }
   */
}

转载于:https://my.oschina.net/dtz/blog/634345

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值