当前位置: 首页 > news >正文

网站建设教程(任务2签订网站建设合同)题库江苏建筑工程网

网站建设教程(任务2签订网站建设合同)题库,江苏建筑工程网,长沙优化排名推广,宾馆管理系统#xff08;注#xff1a;本篇文章分为两个部分#xff0c;如果想更为深刻的了解IO#xff0c;请关注下一篇#xff1b;欢迎大家学习讨论和批评指正#xff09; IO1 作用 将数据在虚拟机内存和本地磁盘之间进行传输 I#xff1a;input 输入 O#xff1a;output 输…注本篇文章分为两个部分如果想更为深刻的了解IO请关注下一篇欢迎大家学习讨论和批评指正 IO1 作用 将数据在虚拟机内存和本地磁盘之间进行传输 Iinput 输入 Ooutput 输出 流 相当于管道作用为进行数据传输 分类 从传输方向上看 输入流本地磁盘的数据向JVM传输输出流JVM数据向本地磁盘传输 从传输单位上看 字节流以字节为单位进行数据传输。可以传输任意类型的数据如文本、图片、视频、音频等字符流以字符为单位进行数据传输。只能传输文本类型的数据如.txt、.java、.html等 从传输功能上看 节点流具有传输功能和意义的流过滤流没有传输功能用来给节点流增强传输能力或增加附加功能 字节流 输入流InputStream 抽象父类输出流OutputStream 抽象父类 输入流 节点流FileInputStream 创建 FileInputStream fisnew FileInputStream(本地的文件路径);路径 绝对路径以电脑磁盘为基点的完整路径 FileInputStream fis new FileInputStream(D:\\test\\a.txt); FileInputStream fis new FileInputStream(D:/test/a.txt);相对路径以当前项目路径为基点的路径前提是文件必须在项目下 FileInputStream fis new FileInputStream(file\\a.txt); FileInputStream fis new FileInputStream(file/a.txt);路径书写必须截至至文件 文件必须存在否则抛出异常 常用方法 void close()关闭流链接释放相关资源。(每个流中都有)int read(): 读取一个字节返回,读取到达末尾,返回-1int read(byte[] b): 尝试读取数组长度的数据至数组中, 返回实际读取个数.读取到达末尾,返回-1 package com.by.test;import java.io.FileInputStream;public class TestFIS {public static void main(String[] args)throws Exception {// FileInputStream fis new FileInputStream(D:\\test\\a.txt);// FileInputStream fis new FileInputStream(D:/test/a.txt);// FileInputStream fis new FileInputStream(C:\\Users\\Administrator\\IdeaProjects\\Chp16\\file\\a.txt);FileInputStream fis new FileInputStream(file\\a.txt);//FileInputStream fis new FileInputStream(file/a.txt);//读取一个字节//利用循环读取文件所有内容while (true) {//先接收本次读取内容int n fis.read();//判断读取是否到达末尾if (n -1) {break;}//未到达末尾,输出查看System.out.println((char) n);}//利用read(byte[])循环读取文件所有内容while (true) {byte[] bs new byte[3];//接收本次读取结果int n fis.read(bs);//判断读取是否到达末尾if (n -1) {break;}//遍历数组查看本次读取结果for (int i 0; i bs.length; i) {System.out.println(bs[i]);}}//关流fis.close();System.out.println(执行成功);} }输出流 节点流:FileOutputStream 创建 FileOutputStream fosFileOutputStream(本地的文件路径,true|false);如果文件不存在,会自动创建 无法创建文件夹 true表示数据追加,false表示数据覆盖 默认是false 常用方法 void flush(): 刷新缓冲区,所有输出流中都具备该方法void write(int ): 向目标文件写入一个字节void write(byte[] ): 向目标文件写入一个数组的数据 package com.by.test;import java.io.FileOutputStream;public class TestFOS {public static void main(String[] args)throws Exception {FileOutputStream fosnew FileOutputStream(file/b.txt);//写入一个字节fos.write(65);fos.write(66);fos.write(67);//写入一个数组String str abcdefg123456;byte[] bs str.getBytes();fos.write(bs);//关流fos.close();System.out.println(执行成功!);} }标准异常处理 JDK7.0之后,提供了自动关流的语法结构,简化了finally的工作内容 try(//需要自动关流的创建语句 ){}catch(){}原理: JDK7.0之后,所有的流都默认实现了AutoCloseable接口,该接口中提供了自动关流所需的close方法 package com.by.test;import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;public class TestFOS2 {public static void main(String[] args) {try (FileOutputStream fos new FileOutputStream(file/b.txt);){//写入一个字节fos.write(65);fos.write(66);fos.write(67);//写入一个数组String str abcdefg123456;byte[] bs str.getBytes();fos.write(bs);} catch (FileNotFoundException e) {System.out.println(文件路径不正确);} catch (IOException e) {System.out.println(读写失败);} catch (Exception e) {System.out.println(未知异常!);e.printStackTrace();}System.out.println(执行成功!);} }文件复制 原理: 先将文件A中的内容读取到JVM中,再从JVM中将读取内容写入到文件B, 借助JVM来实现A与B之间的数据复制 先读后写 package com.by.test;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;public class TestFileCopy {public static void main(String[] args) {copy1();copy2();}/*** 一次复制一个字节*/public static void copy1(){try (//创建输出节点流-复制到的文件路径FileOutputStream fosnew FileOutputStream(d:/test/2.pdf);//创建输入节点流-被复制的文件路径FileInputStream fisnew FileInputStream(d:/test/1.pdf)) {//先循环读取文件所有内容while (true) {int n fis.read();if (n -1) {break;}//将本次读取的字节写入到目标文件fos.write(n);}System.out.println(复制成功!);} catch (FileNotFoundException e) {System.out.println(文件路径不正确);} catch (IOException e) {System.out.println(读写失败!);} catch (Exception e) {System.out.println(未知异常!);e.printStackTrace();}}/*** 一次复制一个字节数组*/public static void copy2(){try (//创建输出节点流-复制到的文件路径FileOutputStream fosnew FileOutputStream(d:/test/3.pdf);//创建输入节点流-被复制的文件路径FileInputStream fisnew FileInputStream(d:/test/1.pdf)) {//先循环读取文件所有内容while (true) {//创建数组byte[] bs new byte[1024];//读取一个数组的数据int n fis.read(bs);if (n -1) {break;}//将数组中的数据写入到目标文件fos.write(bs);}System.out.println(复制成功!);} catch (FileNotFoundException e) {System.out.println(文件路径不正确);} catch (IOException e) {System.out.println(读写失败!);} catch (Exception e) {System.out.println(未知异常!);e.printStackTrace();}} } 缓冲过滤流 BufferedInputStream: 输入缓冲过滤流BufferedOutputStream: 输出缓冲过滤流 创建 BufferedInputStream bisnew BufferedInputStream(fis对象); BufferedOutputStream bosnew BufferedOutputStream(fos对象);作用 拥有一个内置的数据缓冲区, 文件数据将对接至数据缓冲区中,在缓冲区刷新或关闭时再将内部内容一并的给到目标文件 /*** 一次复制一个字节缓冲过滤流*/public static void copy3(){try (//创建输出节点流-复制到的文件路径FileOutputStream fosnew FileOutputStream(d:/test/4.pdf);//创建输入节点流-被复制的文件路径FileInputStream fisnew FileInputStream(d:/test/1.pdf);//添加缓冲过滤流BufferedOutputStream bosnew BufferedOutputStream(fos);BufferedInputStream bisnew BufferedInputStream(fis)) {//先循环读取文件所有内容while (true) {int n bis.read();if (n -1) {break;}//将本次读取的字节写入到目标文件bos.write(n);}System.out.println(复制成功!);} catch (FileNotFoundException e) {System.out.println(文件路径不正确);} catch (IOException e) {System.out.println(读写失败!);} catch (Exception e) {System.out.println(未知异常!);e.printStackTrace();}}使用 如果先写后读,需要在写入完成后刷新缓冲区才能保证读取的正常进行 调用flush():强刷缓冲区 (推荐)调用close(): 关流之前也会刷新缓冲区 关流时外层过滤流关闭内层节点流会一并关闭 package com.by.test;import java.io.*;public class TestBuffered {public static void main(String[] args) {try(//输出BufferedOutputStream bosnew BufferedOutputStream(new FileOutputStream(file/a.txt));//输入BufferedInputStream bisnew BufferedInputStream(new FileInputStream(file/a.txt))){//先写bos.write(abcd.getBytes());//刷新缓冲区bos.flush(); //bos.close();//再读while (true) {int n bis.read();if (n -1) {break;}System.out.println((char) n);}}catch (FileNotFoundException e) {System.out.println(文件路径不正确);} catch (IOException e) {System.out.println(读写失败!);} catch (Exception e) {System.out.println(未知异常!);e.printStackTrace();}} }对象过滤流 ObjectInputStream 对象输入过滤流ObjectOutputStream 对象输出过滤流附加功能1: 读写基本类型附加功能2: 读写引用类型 读写基本类型 读取: ois.readXxx() 写入: oos.writeXxx(值);注: Xxx对应的为基本类型名,首字母大写由于对象过滤流底层嵌套了缓冲区,所以先写后读操作时,仍然需要在写入完成后刷新缓冲区为了保证数据的安全性,所以在写入时数据会根据魔数机制对其加密,在读取时在进行解密 package com.by.test;import java.io.*;public class TestObject_Double {public static void main(String[] args) {try (//输出流ObjectOutputStream oosnew ObjectOutputStream(new FileOutputStream(file/c.txt));//输入流ObjectInputStream oisnew ObjectInputStream(new FileInputStream(file/c.txt))) {//先写oos.writeDouble(10.5);//强刷缓冲区oos.flush();//读取System.out.println(ois.readDouble());} catch (FileNotFoundException e) {System.out.println(文件路径不正确);} catch (IOException e) {System.out.println(读写失败!);e.printStackTrace();} catch (Exception e) {System.out.println(未知异常!);e.printStackTrace();}} }读写引用类型 读取: Object ois.readObject() 读取到达末尾,抛出EOFException异常 写入: oos.writeObject(对象) 可以自动刷新缓冲区,所以先写后读时无需进行flush读写String package com.by.test;import java.io.*;public class TestObject_String {public static void main(String[] args) {try (//输出流ObjectOutputStream oosnew ObjectOutputStream(new FileOutputStream(file/c.txt));//输入流ObjectInputStream oisnew ObjectInputStream(new FileInputStream(file/c.txt))) {//先写oos.writeObject(床前明月光);oos.writeObject(疑是地上霜);oos.writeObject(举头望明月);oos.writeObject(低头思故乡);//后读while (true) {try {String s (String) ois.readObject();System.out.println(s);} catch (EOFException e) {//读取到达末尾,跳出循环break;}}} catch (FileNotFoundException e) {System.out.println(文件路径不正确);} catch (IOException e) {System.out.println(读写失败!);e.printStackTrace();} catch (Exception e) {System.out.println(未知异常!);e.printStackTrace();}} }读写自定义类型 自定义类必须实现Serializable接口表示允许被序列化否则IO流没有读写权限 序列化拆分对象信息的过程 反序列化通过信息组装对象的过程 将属性通过transient修饰符修饰则可以防止其参与序列化 如果对象中有自定义类型的属性则必须使该属性类型也实现序列化接口或者通过transient修饰符对其修饰 package com.by.entity;import java.io.Serializable;public class Student implements Serializable {private String name;private int age;//防止被序列化private transient double score;// private Teacher tea; //省略getter、setter、构造、toString } package com.by.test;import com.by.entity.Student;import java.io.*;public class TestObject_Student {public static void main(String[] args) {try (//输出流ObjectOutputStream oosnew ObjectOutputStream(new FileOutputStream(file/c.txt));//输入流ObjectInputStream oisnew ObjectInputStream(new FileInputStream(file/c.txt))) {//先写oos.writeObject(new Student(zhangsan, 20, 88.5));//后读System.out.println((Student)ois.readObject());} catch (FileNotFoundException e) {System.out.println(文件路径不正确);} catch (IOException e) {System.out.println(读写失败!);e.printStackTrace();} catch (Exception e) {System.out.println(未知异常!);e.printStackTrace();}} }怎么理解输出语句 System是类库中的工具类out为该工具类中的静态属性类型为标准输出流类型print和println系列方法是该流中提供的写入方法作用为向控制台写入一个内容 今日掌握 流的分类文件复制的源码字节复制缓冲过滤流对象过滤流读写自定义类型
http://www.pierceye.com/news/651808/

相关文章:

  • 织梦手机网站模板删除不了宁波网站建设服务商
  • 深圳网站建设最专业网站seo技术
  • 定制网站开发一般多少钱郑州哪家公司做网站好
  • jsp做的网站效果给你一个网站你怎么做的吗
  • 怎样做网站海报wordpress最近怎么又是5.0更新
  • 淡水网站建设哪家便宜设计网页分析
  • 长沙多迪php网站开发培训学校小说榜单首页百度搜索风云榜
  • 门户网站建设计入什么科目网站备案 时间更新
  • 企业建网站租用服务器好还是买一个好wordpress 预订插件
  • 电气建设网站下载的asp网站怎么打开
  • 南阳网站建设icp备手机应用商店免费下载
  • 网站开发测量像素工具网站模板包含哪些内容
  • 南昌网站排名优化费用湖北公众号定制开发
  • 个人主页自助建站凡科网干嘛的
  • 网站后台上传图片不显示品牌营销咨询公司
  • 卖房网站母亲节做什麽活动从传播的角度
  • 永久免费的cad软件seo咨询
  • 网站邮件功能设计理论网站排名软件包年
  • wordpress语言文件编辑专业的企业网站优化公司
  • 正定网站建设制作公司wordpress去掉模板登录
  • 定制开发一个网站多少钱网站开发项目的心得体会
  • 网站被做跳转怎么办个人网站开发软件
  • 湛江网站制作费用南昌建站系统外包
  • 杭州市住房和城乡建设厅网站网页设计个人网站作业
  • 钦州建站哪家好杭州网站建站平台
  • 程序员做笔记的网站在线简历制作系统
  • 有一个网站自己做链接获取朋友位置wordpress504
  • 设计感 网站wordpress企业内网主题
  • 金塔精神文明建设网站上线了小程序制作平台
  • 东莞阳光网站建设成效网站内容营销