在Java中把不同的输入输出源(键盘、文件、网路连接)抽象表述为“流”。

1.输入流、输出流

.字节输入流通过FileInputStream和来操作

字节输出流通过FileOutputStream来操作

2.字节流、字符流

字节流通过InputStream和OutputStream来操作,数据单元是8位的字节

字符流通过Reader和Writer来操作,数据单元是16位的字符

3.节点流、处理流

处理流可以包装节点流进行数据传输,通过处理流,Java程序无需理会输入输出节点是磁盘、网络还是其他输入输出设备,程序只要将节点包装成处理流,就可以使用相同的输入、输出代码来读不通的输入输出设备的数据。


文件字节输出(写)

方法一:

public class 文件输入输出流 {
 public static void main(String[] args) throws Exception {

//1、确定目标文件
  File file = new File("D:\\ccc.txt");
  FileOutputStream fos = null;
  //2、获取流
  try {
   fos = new FileOutputStream(file,true);
 //3、获取目标数据
  String str = "hello world!!!";
  //4、通过流提供的一些列方法 将目标数据 写入目标文件
  fos.write(str.getBytes());
  
 int num = 1/0;//除0异常
 } catch (Exception e) {
   // TODO Auto-generated catch block
 e.printStackTrace();
}finally{
   //finally无论是否发生异常 finally中的代码 都会执行

//5、关闭流
    try {
    fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
  e.printStackTrace();
    }
  }
  
  /**
   *方法二:
   */
  //1、确定目标文件
  File file = new File("D:\\ccc.txt");
  //2、确定流
  FileOutputStream fos = new FileOutputStream(file);
  //3、确定数据
  fos.write(97);

//4、关闭流
  fos.close();
  
  /**
   *方法三:
   */
//1、确定目标文件
 File file = new File("D:\\ccc.txt");
 //2、确定流
  FileOutputStream fos = new FileOutputStream(file);
//3、确定数据
 String str = "hello world!!!";
 //4、写入文件
//  /*参数一:目标数组
//   * 参数二:从哪里开始写
//   * 参数三:一共写入多少个
//   * write(b, off, len);
//   */
  fos.write(str.getBytes(), 2, 5);

//5、关闭流
  fos.close();


从文件中将数据读取出来

方法一:

public class 文件输入输出流 {  public static void main(String[] args) throws Exception {

//1、确定目标文件

File file = new File("D:\\ccc.txt");
  //2、获取流对象
 try {
  FileInputStream fis = new FileInputStream(file);
 //3、读取数据
 byte[] b = new byte[1024];//用于接受数据
  fis.read(b);
 //4、打印数据
//   /**
//    * 参数一:目标构造数组
//    * 参数二:从哪里开始构造
//    * 参数三:构造的长度
//    * String(byte[] bytes, int offset, int length)
//    * */
  System.out.println(new String(b,0,(int)file.length()));
  } catch (Exception e) {
   // TODO Auto-generated catch block
  e.printStackTrace();
  }
  
  /**
   * 方法二:
   */
 //1、确定目标文件
  File file = new File("D:\\ccc.txt");
  //2、确定流
  FileInputStream fis = new FileInputStream(file);
  //3、开始读
  byte[] b = new byte[1024];
  fis.read(b, 0, 3);

//4、关闭流
 fis.close();
  
 System.out.println(new String(b));
  
  /**
   * 方法三:
   */
//1、确定目标文件
 File file = new File("D:\\ccc.txt");
  //2、确定流
  FileInputStream fis = new FileInputStream(file);
//3、开始读数据
  /*
   * 注意:之前的读取方法 我们都是将数据 直接读到一个byte数组中
   * read():从文件中一个一个的读出来 
   * 如果有数据 读出来的是 ,每个字母对应的ascll码值
   * 如果读到最后 返回-1
   * 利用循环读出
   */
  StringBuffer sb = new StringBuffer();//用于拼接
  int temp = 0;//用于临时存储每个字节
  
  while((temp = fis.read())!= -1){
   sb.append((char)temp);
  }

//4、关闭流
  fis.close();
  
  System.out.println(sb.toString());

最新文章

  1. SOAPUI使用教程-测试JDBC数据库
  2. WPF-开机自启
  3. Java日志框架:SLF4J,Common-Logging,Log4J,Logback说明
  4. PHP_Bibel阅读笔记(二)——脸黑的一天(?一年)
  5. bzoj1503
  6. phpcms如何判断用户是否登录
  7. ajax使用jquery的实现方式
  8. OC 和 swift 小结
  9. 【转】Python中执行cmd的三种方式
  10. java 静态方法和单例模式的区别
  11. vue初探
  12. struts2.1.6教程六、使用标签
  13. 开发vue但不使用vue-cli和webpack相关注意事项
  14. IMDB-二分类问题
  15. Swift ios应用开发实践
  16. Apache Sling
  17. flutter环境配置
  18. JS高级-虚拟DOM
  19. iOS连续上传多张图片
  20. 9大行为导致Java程序员薪资过低, 你有几个?

热门文章

  1. 2016/2/18 html 图片热点,网页划区,拼接,表单
  2. (三)Java 开发环境配置
  3. UVA-10163 Storage Keepers DP
  4. POJ 2629:Common permutation
  5. Cordova 开发 App
  6. 2-4 Vue中的属性绑定和双向数据绑定
  7. gitlab&Jenkins 详细介绍及其应用
  8. SQL 经典语句大全
  9. 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil
  10. 转 DOS(CMD)下批处理换行问题/命令行参数换行 arg ms-dos