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

个人建站步骤赣州网站网站建设

个人建站步骤,赣州网站网站建设,环保科技东莞网站建设,陕西建设网网博主#xff1a;_LJaXi 专栏#xff1a; Java | 从跨平台到跨行业 开发工具#xff1a;IntelliJ IDEA Community Edition 2022.3.3 Java IO流 中秋特供啦 #x1f96e;Java Io #x1f354;什么是流流的分类文件字节输入流1. 条件循环解决1 (2) 读取特性 2. 数组存储解决 … 博主_LJaXi 专栏 Java | 从跨平台到跨行业 开发工具IntelliJ IDEA Community Edition 2022.3.3 Java IO流 中秋特供啦 Java Io 什么是流流的分类文件字节输入流1. 条件循环解决1 (2) 读取特性 2. 数组存储解决 文件字节输出流输出流构造方法 字节流复制文件字节缓冲流BufferedInputStreamBufferedInputStream 自定义读取部分数据BufferedOutputStream 对象流Student 类ObjectOutStreamObjectInputSteam序列化和反序列化注意点 中秋特供啦 提前祝大家 中秋 | 国庆 快乐 Java Io 什么是流 I: Input | 输入 O: Output | 输出 流的分类 按照数据的流向 输入流读数据输出流写数据 按照数据类型来分 字节流 字节输入流字节输出流 字符流 字符输入流字符输出流 输入流,输出流 字节流,字符流 文件字节输入流 FileInputStream package FileInput;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;/*** Author: _LJaXi* author ASUS*/ class FileInput {public static void main(String[] args) throws IOException {try {FileInputStream fis new FileInputStream(d:\\aaa.txt);// 一次读取多个字节,存到数组中// 创建一个长度为3的数组 / 字节类型byte[] buf new byte[3];int count 0;// 条件判断循环while ((count fis.read(buf)) ! -1) {System.out.print(new String(buf,0,count));}// 3. 关闭fis.close();} catch (FileNotFoundException e) {e.printStackTrace();}} }1. 条件循环解决 package main.java.com.mycode;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;/*** Author: _LJaXi* author ASUS*/ class MyIo {public static void main(String[] args) throws IOException {try {FileInputStream fis new FileInputStream(d:\\aaa.txt);// 读取System.out.println(fis.read());int data 0;// 循环打印字节while ((data fis.read()) ! -1) {System.out.print((char) data);}// 3. 关闭fis.close();} catch (FileNotFoundException e) {e.printStackTrace();}} }1 (2) 读取特性 package main.java.com.mycode;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;/*** Author: _LJaXi* author ASUS*/ class MyIo {public static void main(String[] args) throws IOException {try {FileInputStream fis new FileInputStream(d:\\aaa.txt);// 一次读取多个字节,存到数组中// 创建一个长度为3的数组 / 字节类型byte[] buf new byte[3];// 返回实例读取的个数int count fis.read(buf);// 打印字符串 bufSystem.out.println(new String(buf));System.out.println(count);// 多次读取int count2 fis.read(buf);System.out.println(new String(buf));System.out.println(count2);int count3 fis.read(buf);System.out.println(new String(buf));System.out.println(count3);// 若文件内字符太短可能会出现读取数组you多余元素问题那是你的上次buf数组没有清空导致的// 可以使用// System.out.println(new String(buf, index(索引), count3)); 来清理多余的元素// new String(buf)参数// 1. param1 是一个字节数组用于构建新的字符串对象// 2. param2 是偏移量offset表示从 param1 的索引为 param2 的位置开始构建字符串// 3. param3 是长度length表示构建字符串的长度// 3. 关闭fis.close();} catch (FileNotFoundException e) {e.printStackTrace();}} }2. 数组存储解决 package main.java.com.mycode;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;/*** Author: _LJaXi* author ASUS*/ class MyIo {public static void main(String[] args) throws IOException {try {FileInputStream fis new FileInputStream(d:\\aaa.txt);// 一次读取多个字节,存到数组中// 创建一个长度为3的数组 / 字节类型byte[] buf new byte[3];int count 0;// 条件判断循环while ((count fis.read(buf)) ! -1) {System.out.print(new String(buf,0,count));}// 3. 关闭fis.close();} catch (FileNotFoundException e) {e.printStackTrace();}} }文件字节输出流 FileOutputStream package main.java.com.mycode;import java.io.FileOutputStream;/*** Author: _LJaXi* author ASUS*/public class FileOutput {public static void main (String[] args) throws Exception {// 1. 创建文件字节输出流对象// 若不想覆盖原本内容将构造方法append设置为trueFileOutputStream fos new FileOutputStream(d://aaa.txt, true);// 2. 写入文件// fos.write(97);// fos.write(b);// fos.write(c);String str helloworld;// 获取字符串所对应的字节数组fos.write(str.getBytes());// 3. 关闭fos.close();System.out.println(执行完毕);} }输出流构造方法 // 某个构造方法有个形参 append这个构造方法的 append 若为true 表明不覆盖原本内容 public FileOutputStream(String name, boolean append)throws FileNotFoundException{this(name ! null ? new File(name) : null, append);}字节流复制文件 package CopyFile;import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;/* * 使用文件字节流实现文件的复制 * */ public class CopyFile {public static void main(String[] args) throws IOException {// 1. 先用文件字节输入流读到内存中FileInputStream fis new FileInputStream(C:\\Users\\ASUS\\Desktop\\凌波.png);// 2. 再用一个文件字节输出流写入到硬盘FileOutputStream fos new FileOutputStream(C:\\Users\\ASUS\\Desktop\\凌波丽.jpg);// 3. 一边读 一边写byte[] buf new byte[1024];int count 0;while((countfis.read(buf)) ! -1) {fos.write(buf,0,count);}// 4. 关闭流fis.close();fos.close();System.out.print(复制完毕);} }字节缓冲流 BufferedInputStream / Buffered0utputStream 提高IO效率减少访问磁盘的次数; 数据存储在缓冲区中flush是将缓存区的内容写入文件中也可以直接close; BufferedInputStream package Buffered;import java.io.FileInputStream; import java.io.BufferedInputStream; import java.io.IOException;/* * 使用字节缓冲流读取 * BufferedInputStream * Tip 我想和 符韬_(pinegg) 一起玩 * */ public class BufferedStream {public static void main(String[] args) throws IOException {// 1. 创建BufferedInputStreamFileInputStream fis new FileInputStream(d:\\aaa.txt);// 增强底层流BufferedInputStream bis new BufferedInputStream(fis);// 2. 读取 bis 读文件会放在缓冲区提高效率int count 0;// bis.read() 并不是读一个字节先把一部分数据读到bis缓冲区内调用已经读了8k了// private static int DEFAULT_BUFFER_SIZE 8192;while ((countbis.read()) ! -1) {System.out.print((char) count);}// 3. 关闭bis.close(); // 缓冲流close之后, 会自动帮你关闭输入流 fis.close()} }BufferedInputStream 自定义读取部分数据 package Buffered;import java.io.FileInputStream; import java.io.BufferedInputStream; import java.io.IOException;/* * 使用字节缓冲流读取 * BufferedInputStream * author 符韬是谁 * */ public class BufferedStream {public static void main(String[] args) throws IOException {// 1. 创建BufferedInputStreamFileInputStream fis new FileInputStream(d:\\aaa.txt);// 增强底层流BufferedInputStream bis new BufferedInputStream(fis);// 2. 读取 bis 读文件会放在缓冲区提高效率int count 0;// bis.read() 并不是读一个字节先把一部分数据读到bis缓冲区内调用已经读了8k了// private static int DEFAULT_BUFFER_SIZE 8192;// 当然你也可以自己定义把一部分数据读取到缓冲区, 下方注释写法byte[] buf new byte[6000];while ((countbis.read(buf)) ! -1) {System.out.print(new String(buf, 0, count));}// 3. 关闭bis.close(); // 缓冲流close之后, 会自动帮你关闭输入流 fis.close()} }BufferedOutputStream package Buffered; import java.io.*;/** 使用字节缓冲流写入* BufferedOutputStream* time 2023年9月11日14:19:47* author _LJaXi* tip 想秀阿卡丽* */public class OutputStream {public static void main(String[] args) {try {FileOutputStream fos new FileOutputStream(d:\\aaa.txt, true);BufferedOutputStream bos new BufferedOutputStream(fos);String data LOL;bos.write(data.getBytes()); // 写入 8k 到缓冲区fos.flush(); // 刷新到硬盘/*---------------------------------------------------------------* bos.flush()的作用是将缓冲区中的数据立即刷新写入到硬盘中* 当使用BufferedOutputStream写入数据时数据通常会先被存储在内部的缓冲区中直到缓冲区达到一定的容量或者手动调用flush()方法* ---------------------------------------------------------------* flush()方法的使用是为了确保数据被及时写入硬盘而不是一直停留在缓冲区* 在调用flush()方法后任何未写入的数据将被立即写入到硬盘中这样可以避免数据丢失的风险* ---------------------------------------------------------------* 在流关闭之前或发生异常时也会自动调用flush()方法确保所有数据都被写入硬盘* ---------------------------------------------------------------*/bos.close(); // 内部也会调用 flush() 方法} catch (IOException e) {e.printStackTrace();}} }对象流 增强了缓冲区功能增强了读写8种基本数据类型和字符串功能增强了读写对象的功能 { ​ readObject() 从流中读取一个对象 ​ writeObject(Object obj) 向流中写入一个对象 } 使用流传输对象的过程称为序列化(写入)、反序列化(读取) Student 类 package ObjectStream;import java.io.Serializable;// 要想序列化类必须实现一个接口为标记接口 public class Student implements Serializable {private String name;private int age;public Student() {}public Student(String name, int age) {super();this.name name;this.age age;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}// this is 重写 toString 方法Overridepublic String toString() {return Student{ name name \ , age age };} }ObjectOutStream package ObjectStream;import java.io.*; /* * 序列化类必须要实现 Serializable 接口(标记接口) * ObjectOutputStream * time 2023年9月14日13:49:52 * author _LJaXi * tip 阿卡丽5连败 * */ public class OutStream {public static void main(String[] args)throws IOException {// 1. 创建对象流FileOutputStream fos new FileOutputStream(d:\\aaa.bin);ObjectOutputStream oos new ObjectOutputStream(fos);// 2. 序列化写入操作Student zs new Student(张三, 20);oos.writeObject(zs);// 3. 关闭oos.close();System.out.println(序列化完毕);} }ObjectInputSteam package ObjectStream;import java.io.*;/* * ObjectInputStream 反序列化(读取重构成对象) * time 2023年9月14日23:31:23 * author _LJaXi * tip 阿卡丽排位3连胜 * */ public class InputStream {public static void main(String[] args) throws IOException, ClassNotFoundException {// 1. 创建对象流FileInputStream fis new FileInputStream(d:\\aaa.bin);ObjectInputStream ois new ObjectInputStream(fis);// 2. 读取文件(反序列化)Student s (Student)ois.readObject();// Student s2 (Student)ois.readObject(); // 不能读取多次// 3. 关闭ois.close();System.out.println(执行完毕);System.out.println(s.toString());} }序列化和反序列化注意点 序列化类必须实现 Serializable 接口 序列化类中对象属性要求实现 Serializable 接口 序列化类中对象属性要求实现 Serializable 接口意思为 public class Student implements Serializable {private String name;private int age;private Class class; // Class类要实现 Serializable 接口// ... 其他内容 }正在更新 ING…
http://www.pierceye.com/news/292174/

相关文章:

  • 中国室内设计师联盟网站浙江手机版建站系统开发
  • 网站开源代码模版广州公司注册核名查询系统官网
  • 海外网站seo丹阳市住房建设管理局网站
  • 定制公司网站沙市做网站weisword
  • 平湖模板网站建设公司网站建设项目报告书
  • 校园门户网站解决方案手机与电脑网站制作
  • 济南网站建设 伍际网络网站域名备案授权书
  • 网站开发销售提成网站建设的内部风险分析
  • 网站建设框架都有哪些内容公司名字大全参考2022
  • 成功备案的网站增加域名黄金网站
  • 学习网站开发多少钱北京网页设计公司兴田德润可以吗
  • 如何加强门户网站建设上海好的设计公司
  • h5企业模板网站模板营销推广的渠道方式
  • 怎么学做网站PHP百度搜索风云榜总榜
  • 网站风格模板公司建设官方网站
  • 做站群一个网站多少钱网络服务器的分类
  • 专业的常州做网站营销推广48个方法
  • 开奖网站怎么做wordpress4.9.8中文版
  • 国外做任务的网站网推公司
  • 国外有在线做设计方案的网站吗为什么用Vue做网站的很少
  • 网站一定要备案网站建设与维护工作
  • 锦江区建设和交通局网站怎样在网上建立平台
  • 网站维护升级访问中六安论坛网站
  • ppt模板网站哪个免费重庆手机版建站系统哪家好
  • 35岁学设计晚不晚北京网站快速排名优化
  • 网站建设三合一 500元阜阳网站建设公司
  • 那些公司需要网站开发工程师网页开发与网站开发
  • 手机端网站如何做排名wordpress no7
  • 搭建网站什么意思o2o的典型电子商务平台
  • vs2013网站开发教程wordpress站内搜索框