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

有关网站开发的参考文献秦皇岛在哪

有关网站开发的参考文献,秦皇岛在哪,服务器维护公告,营销培训课程文章目录 Java-IO流文件字节流文件字符流File类缓冲流转换流打印流数据流对象流 Java-IO流 JDK提供了一套用于IO操作的框架#xff0c;为了方便我们开发者使用#xff0c;就定义了一个像水流一样#xff0c;根据流的传输方向和读取单位#xff0c;分为字节流InputStream和… 文章目录 Java-IO流文件字节流文件字符流File类缓冲流转换流打印流数据流对象流 Java-IO流 JDK提供了一套用于IO操作的框架为了方便我们开发者使用就定义了一个像水流一样根据流的传输方向和读取单位分为字节流InputStream和OutputStream以及字符流Reader和Writer的IO框架 这里的流指的是数据流通过流我们就可以一直从流中读取数据直到读取到尽头或是不断向其中写入数据直到我们写入完成 文件字节流 FileInputStream通过它来获取文件的输入流 public static void main(String[] args) {FileInputStream inputStream null; //定义可以先放在try外部try {inputStream new FileInputStream(路径);} catch (FileNotFoundException e) {e.printStackTrace();} finally {try { //建议在finally中进行因为关闭流是任何情况都必须要执行的if(inputStream ! null) inputStream.close();} catch (IOException e) {e.printStackTrace();}} }在使用完成一个流之后必须关闭这个流来完成对资源的释放否则资源会被一直占用 JDK1.7新增了try-with-resource语法用于简化资源释放的写法 public static void main(String[] args) {//注意这种语法只支持实现了AutoCloseable接口的类try(FileInputStream inputStream new FileInputStream(路径)) { //直接在try()中定义要在完成之后释放的资源} catch (IOException e) { //这里变成IOException是因为调用close()可能会出现而FileNotFoundException是继承自IOException的e.printStackTrace();}//无需再编写finally语句块因为在最后自动帮我们调用了close() }使用read方法读取文件内容 public static void main(String[] args) {//test.txtabcdtry(FileInputStream inputStream new FileInputStream(test.txt)) {int tmp;while ((tmp inputStream.read()) ! -1){ //通过while循环来一次性读完内容System.out.println((char)tmp);}}catch (IOException e){e.printStackTrace();} }使用available方法能查看当前可读的剩余字节数量 一个一个读取效率太低了可以预置一个合适容量的byte[]数组来存放 public static void main(String[] args) {//test.txtabcdtry(FileInputStream inputStream new FileInputStream(test.txt)) {byte[] bytes new byte[inputStream.available()]; //我们可以提前准备好合适容量的byte数组来存放System.out.println(inputStream.read(bytes)); //一次性读取全部内容返回值是读取的字节数System.out.println(new String(bytes)); //通过String(byte[])构造方法得到字符串}catch (IOException e){e.printStackTrace();} }控制要读取数量 System.out.println(inputStream.read(bytes, 1, 2)); //第二个参数是从给定数组的哪个位置开始放入内容第三个参数是读取流中的字节数一次性读取同单个读取一样当没有任何数据可读时依然会返回-1 通过skip()方法可以跳过指定数量的字节 FileInputStream是不支持reset()的虽然有这个方法 write()操作向文件里写入内容 public static void main(String[] args) {try(FileOutputStream outputStream new FileOutputStream(output.txt, true)) { //true表示开启追加模式outputStream.write(lb.getBytes()); //现在只会进行追加写入而不是直接替换原文件内容outputStream.flush();}catch (IOException e){e.printStackTrace();} }文件拷贝 public static void main(String[] args) {try(FileOutputStream outputStream new FileOutputStream(output.txt);FileInputStream inputStream new FileInputStream(test.txt)) { //可以写入多个byte[] bytes new byte[10]; //使用长度为10的byte[]做传输媒介int tmp; //存储本地读取字节数while ((tmp inputStream.read(bytes)) ! -1){ //直到读取完成为止outputStream.write(bytes, 0, tmp); //写入对应长度的数据到输出流}}catch (IOException e){e.printStackTrace();} }文件字符流 字符流不同于字节字符流是以一个具体的字符进行读取因此它只适合读纯文本的文件如果是其他类型的文件不适用 public static void main(String[] args) {try(FileReader reader new FileReader(test.txt)){char[] str new char[10];reader.skip(1); //现在跳过的是一个字符reader.read(str);System.out.println(str); //直接读取到char[]中}catch (IOException e){e.printStackTrace();} }Writer public static void main(String[] args) {try(FileWriter writer new FileWriter(output.txt)){writer.getEncoding(); //支持获取编码不同的文本文件可能会有不同的编码类型writer.write(牛);writer.append(牛); //其实功能和write一样writer.flush(); //刷新}catch (IOException e){e.printStackTrace();} }append支持像StringBuilder那样的链式调用返回的是Writer对象本身。 File类 File类是专门用于表示一个文件或文件夹只不过它只是代表这个文件但并不是这个文件本身。 通过File对象可以更好地管理和操作硬盘上的文件。 public static void main(String[] args) {File file new File(test.txt); //直接创建文件对象可以是相对路径也可以是绝对路径System.out.println(file.exists()); //此文件是否存在System.out.println(file.length()); //获取文件的大小System.out.println(file.isDirectory()); //是否为一个文件夹System.out.println(file.canRead()); //是否可读System.out.println(file.canWrite()); //是否可写System.out.println(file.canExecute()); //是否可执行File file new File(/); System.out.println(Arrays.toString(file.list())); //快速获取文件夹下的文件名称列表System.out.println(f.getAbsolutePath()); //获取文件的绝对路径 }直接将File作为参数传入字节流或是字符流读取某个文件的内容 File file new File(test.txt); try (FileInputStream inputStream new FileInputStream(file)){ //直接做参数System.out.println(inputStream.available()); }catch (IOException e){e.printStackTrace(); }缓冲流 普通的文件流读取文件数据非常便捷但是每次都需要从外部I/O设备去获取数据由于外部I/O设备的速度一般都达不到内存的读取速度很有可能造成程序反应迟钝因此性能还不够高。 缓冲流能够提供一个缓冲提前将部分内容存入内存缓冲区在下次读取时如果缓冲区中存在此数据则无需再去请求外部设备。当向外部设备写入数据时也是由缓冲区处理而不是直接向外部设备写入。 创建一个缓冲字节流 public static void main(String[] args) {try (BufferedInputStream bufferedInputStream new BufferedInputStream(new FileInputStream(test.txt))){ //传入FileInputStreamSystem.out.println((char) bufferedInputStream.read()); //操作和原来的流是一样的}catch (IOException e){e.printStackTrace();} }实际上进行I/O操作的并不是BufferedInputStream而是我们传入的FileInputStream而BufferedInputStream虽然有着同样的方法但是进行了一些额外的处理然后再调用FileInputStream的同名方法这样的写法称为装饰者模式 close方法源码 public void close() throws IOException {byte[] buffer;while ( (buffer buf) ! null) {if (bufUpdater.compareAndSet(this, buffer, null)) { //CAS无锁算法并发会用到暂时不需要了解InputStream input in;in null;if (input ! null)input.close();return;}// Else retry in case a new buf was CASed in fill()} }BufferedInputStream中还存在一个专门用于缓存的数组 /*** The internal buffer array where the data is stored. When necessary,* it may be replaced by another array of* a different size.*/ protected volatile byte buf[];缓冲流提供了缓冲机制一部分内容可以被暂时保存BufferedInputStream支持reset()和mark()操作 当调用mark()之后输入流会以某种方式保留之后读取的readlimit数量的内容当读取的内容数量超过readlimit则之后的内容不会被保留当调用reset()之后会使得当前的读取位置回到mark()调用时的位置 其实mark()后保存的读取内容是取readlimit和BufferedInputStream类的缓冲区大小两者中的最大值 public static void main(String[] args) {try (BufferedInputStream bufferedInputStream new BufferedInputStream(new FileInputStream(test.txt))){bufferedInputStream.mark(1); //只保留之后的1个字符System.out.println((char) bufferedInputStream.read());System.out.println((char) bufferedInputStream.read());bufferedInputStream.reset(); //回到mark时的位置System.out.println((char) bufferedInputStream.read());System.out.println((char) bufferedInputStream.read());}catch (IOException e) {e.printStackTrace();} }BufferedReader构造时需要传入一个Reader对象 public static void main(String[] args) {try (BufferedReader reader new BufferedReader(new FileReader(test.txt))){System.out.println((char) reader.read());}catch (IOException e) {e.printStackTrace();} }BufferedReader内部也包含一个缓存数组 private char cb[];BufferedReader支持按行读取 public static void main(String[] args) {try (BufferedReader reader new BufferedReader(new FileReader(test.txt))){System.out.println(reader.readLine()); //按行读取}catch (IOException e) {e.printStackTrace();} }把每一行内容依次转换为集合类提到的Stream流 public static void main(String[] args) {try (BufferedReader reader new BufferedReader(new FileReader(test.txt))){reader.lines().limit(2).distinct().sorted().forEach(System.out::println);}catch (IOException e) {e.printStackTrace();} }BufferedWriter在处理纯文本文件方便 public static void main(String[] args) {try (BufferedWriter reader new BufferedWriter(new FileWriter(output.txt))){reader.newLine(); //使用newLine进行换行reader.write(汉堡做滴彳亍不彳亍); //可以直接写入一个字符串reader.flush(); //清空缓冲区}catch (IOException e) {e.printStackTrace();} }转换流 读取的是一个字符串或是一个个字符但是我只能往一个OutputStream里输出但是OutputStream又只支持byte类型如果要往里面写入内容进行数据转换就会很麻烦那么能否有更加简便的方式来做这样的事情呢 public static void main(String[] args) {try(OutputStreamWriter writer new OutputStreamWriter(new FileOutputStream(test.txt))){ //虽然给定的是FileOutputStream但是现在支持以Writer的方式进行写入writer.write(lbwnb); //以操作Writer的样子写入OutputStream}catch (IOException e){e.printStackTrace();} }只拿到了一个InputStream但是我们希望能够按字符的方式读取我们就可以使用InputStreamReader来帮助我们实现 public static void main(String[] args) {try(InputStreamReader reader new InputStreamReader(new FileInputStream(test.txt))){ //虽然给定的是FileInputStream但是现在支持以Reader的方式进行读取System.out.println((char) reader.read());}catch (IOException e){e.printStackTrace();} }打印流 System.out就是一个PrintStreamPrintStream也继承自FilterOutputStream类因此依然是装饰我们传入的输出流但是它存在自动刷新机制。 PrintStream也永远不会抛出异常而是使用内部检查机制checkError()方法进行错误检查。 它能够格式化任意的类型将它们以字符串的形式写入到输出流。 System.out也是PrintStream不过默认是向控制台打印也可以让它向文件中打印 public static void main(String[] args) {try(PrintStream stream new PrintStream(new FileOutputStream(test.txt))){stream.println(lbwnb); //其实System.out就是一个PrintStream}catch (IOException e){e.printStackTrace();} }println方法就是PrintStream中的方法它会直接打印基本数据类型或是调用对象的toString()方法得到一个字符串并将字符串转换为字符放入缓冲区再经过转换流输出到给定的输出流上。 Scanner使用的是系统提供的输入流也可以使用Scanner来扫描其他的输入流 public static void main(String[] args) throws FileNotFoundException {Scanner scanner new Scanner(new FileInputStream(秘制小汉堡.txt)); //将文件内容作为输入流进行扫描 }数据流 数据流DataInputStream也是FilterInputStream的子类同样采用装饰者模式最大的不同是它支持基本数据类型的直接读取 public static void main(String[] args) {try (DataInputStream dataInputStream new DataInputStream(new FileInputStream(test.txt))){System.out.println(dataInputStream.readBoolean()); //直接将数据读取为任意基本数据类型}catch (IOException e) {e.printStackTrace();} }用于写入基本数据类型 public static void main(String[] args) {try (DataOutputStream dataOutputStream new DataOutputStream(new FileOutputStream(output.txt))){dataOutputStream.writeBoolean(false);}catch (IOException e) {e.printStackTrace();} }写入的是二进制数据并不是写入的字符串使用DataInputStream可以读取一般他们是配合一起使用的。 对象流 ObjectOutputStream不仅支持基本数据类型通过对对象的序列化操作以某种格式保存对象来支持对象类型的IO它不是继承自FilterInputStream的。 public static void main(String[] args) {try (ObjectOutputStream outputStream new ObjectOutputStream(new FileOutputStream(output.txt));ObjectInputStream inputStream new ObjectInputStream(new FileInputStream(output.txt))){People people new People(lbw);outputStream.writeObject(people);outputStream.flush();people (People) inputStream.readObject();System.out.println(people.name);}catch (IOException | ClassNotFoundException e) {e.printStackTrace();} }static class People implements Serializable{ //必须实现Serializable接口才能被序列化String name;public People(String name){this.name name;} }后续的操作中有可能会使得这个类的一些结构发生变化而原来保存的数据只适用于之前版本的这个类因此我们需要一种方法来区分类的不同版本 static class People implements Serializable{private static final long serialVersionUID 123456; //在序列化时会被自动添加这个属性它代表当前类的版本我们也可以手动指定版本。String name;public People(String name){this.name name;} }当发生版本不匹配时会无法反序列化为对象 如果我们不希望某些属性参与到序列化中进行保存我们可以添加transient关键字 static class People implements Serializable{private static final long serialVersionUID 1234567;transient String name;public People(String name){this.name name;} }ialVersionUID 123456; //在序列化时会被自动添加这个属性它代表当前类的版本我们也可以手动指定版本。 String name;public People(String name){this.name name; }} 当发生版本不匹配时会无法反序列化为对象如果我们不希望某些属性参与到序列化中进行保存我们可以添加transient关键字~~~java static class People implements Serializable{private static final long serialVersionUID 1234567;transient String name;public People(String name){this.name name;} }transient关键字使得某些属性不参与序列化取消这些不必要保存的属性可以节省数据空间占用以及减少序列化时间。
http://www.tj-hxxt.cn/news/141275.html

相关文章:

  • 网站优化前景重庆营销型网站建设多少钱
  • 怎么自己给自己的网站做推广北京网站建设定制
  • 苏州h5网站建设价格page 编辑 wordpress
  • 推广优化网站排名高新公司网站建设哪家好
  • 用什么建设网站洋气的传媒公司名字
  • 上海网站建设-中国互联长沙市有限公司
  • 与电子商务网站建设有关实训报告系统优化助手
  • 国外有没有网站是做潘多拉的市场调研报告万能模板
  • 国外网站搭建平台京网站建设
  • 巴中市建设局网站博客网站登录入口
  • 优秀网站网页设计v9做的网站被攻击链接吧跳转
  • 山东专业的网站建设wordpress手机自动跳转二级
  • 网站备案名称填写规则工业企业网络推广
  • 潍坊作风建设网站asp网站源码
  • 旅游网站设计完整代码好的企业管理网站
  • 网站建设的公司前景大型网站开发 书籍
  • 购物网站建设网页推广php网站开发前景
  • 建设集团网站报告书阿里巴巴网站分类板块做全屏
  • 网站开发职业工资如何开发app软件平台
  • 建设网站投标标书范本手机如何制作app
  • 怎么样做手机网站做零售网站
  • 建站网址导航hao123连山网站建设
  • 呼和浩特网站建设费用html编辑器怎么用
  • 销售网站的技巧镇江网友之家手机版
  • 纪检监察工作 网站建设抖音代运营合同范标准版
  • 做服装外单的网站有哪些网站logo代码
  • 珠海门户网站建设山东莱芜金点子信息港
  • 做暧暧视频免费视频中国网站上海网站建设seo1888
  • 自学摄影教程的网站有哪些中国建设银行个人登录
  • 郑州网站建设公司如何公众号开发专业