东莞品牌营销型网站建设,wordpress如何做优化,搭建网站要多久,wordpress获取文章内图片文章目录缓冲流转换流标准输入输出流打印流数据流对象流随机存取文件流Java NIO缓冲流
为了提高数据读写的速度#xff0c;Java API提供了带缓冲功能的流类#xff0c;在使用这些流类时#xff0c;会创建一个内部缓冲区数组#xff0c;缺省使用8192个字节(8Kb)的缓冲区。 …
文章目录缓冲流转换流标准输入输出流打印流数据流对象流随机存取文件流Java NIO缓冲流
为了提高数据读写的速度Java API提供了带缓冲功能的流类在使用这些流类时会创建一个内部缓冲区数组缺省使用8192个字节(8Kb)的缓冲区。 缓冲流要“套接”在相应的节点流之上根据数据操作单位可以把缓冲流分为 BufferedInputStream 和 BufferedOutputStream BufferedReader 和 BufferedWriter
当读取数据时数据按块读入缓冲区其后的读操作则直接访问缓冲区。当使用BufferedInputStream读取字节文件时BufferedInputStream会一次性从文件中读取8192个(8Kb)存在缓冲区中直到缓冲区装满了才重新从文件中读取下一个8192个字节数组。
向流中写入字节时不会直接写到文件先写到缓冲区中直到缓冲区写满 BufferedOutputStream才会把缓冲区中的数据一次性写到文件里。使用方法 flush()可以强制将缓冲区的内容全部写入输出流。
关闭流的顺序和打开流的顺序相反。只要关闭最外层流即可关闭最外层流也 会相应关闭内层节点流。
flush()方法的使用手动将buffer中内容写入文件。使用带缓冲区的流对象的close()方法不但会关闭流还会在关闭流之前刷新缓冲区相当于自动调用了flush()方法关闭后不能再写出。 以字节流BufferedInputStream 和 BufferedOutputStream示例具体操作
import java.io.*;/*** Author: Yeman* Date: 2021-09-25-22:36* Description:*/
public class BufferedTest {public static void main(String[] args) {FileInputStream fis null;FileOutputStream fos null;BufferedInputStream bfi null;BufferedOutputStream bfo null;try {//1、实例化File对象指定文件File inFile new File(IO\\input.jpg);File outFile new File(IO\\output.jpg);//2、创建节点流文件流对象fis new FileInputStream(inFile);fos new FileOutputStream(outFile);//3、创建处理节点流的缓冲流对象bfi new BufferedInputStream(fis);bfo new BufferedOutputStream(fos);//4、通过缓冲流进行读写操作byte[] bytes new byte[1024];int length bfi.read(bytes);while (length ! -1){bfo.write(bytes,0,length);length bfi.read(bytes);}} catch (IOException e) {e.printStackTrace();} finally {//5、关闭外层流内存流便自动关闭try {if (bfi ! null) bfi.close();} catch (IOException e) {e.printStackTrace();}try {if (bfo ! null) bfo.close();} catch (IOException e) {e.printStackTrace();}}}
}转换流
转换流提供了在字节流和字符流之间的转换。
InputStreamReader将InputStream转换为Reader字节转为字符输入 OutputStreamWriter将Writer转换为OutputStream字节转为字符输出
字节流中的数据都是字符时转成字符流操作更高效。使用转换流来处理文件乱码问题实现编码和解码的功能。 InputStreamReader 实现将字节的输入流按指定字符集转换为字符的输入流。需要和InputStream“套接”。 构造器 public InputStreamReader(InputStream in) public InputSreamReader(InputStream in,String charsetName) OutputStreamWriter 实现将字符的输出流按指定字符集转换为字节的输出流。需要和OutputStream“套接”。 构造器 public OutputStreamWriter(OutputStream out) public OutputSreamWriter(OutputStream out,String charsetName)
import java.io.*;/*** Author: Yeman* Date: 2021-09-26-20:13* Description:*/
public class Test {public static void main(String[] args) {InputStreamReader isr null;OutputStreamWriter osw null;try {//1、指明输入输出文件File inFile new File(IO\\hi.txt);File outFile new File(IO\\hello.txt);//2、提供字节节点流FileInputStream fis new FileInputStream(inFile);FileOutputStream fos new FileOutputStream(outFile);//3、提供转换流isr new InputStreamReader(fis,gbk);osw new OutputStreamWriter(fos,utf-8);//4、读写操作char[] chars new char[10];int len isr.read(chars);while (len ! -1){osw.write(chars,0,len);len isr.read(chars);}} catch (IOException e) {e.printStackTrace();} finally {//5、关闭外层流try {if (osw ! null) osw.close();} catch (IOException e) {e.printStackTrace();}try {if (isr ! null) isr.close();} catch (IOException e) {e.printStackTrace();}}}
}标准输入输出流
System.in和System.out分别代表了系统标准的输入和输出设备 默认输入设备是键盘输出设备是显示器控制台
System.in的类型是InputStream System.out的类型是PrintStream其是OutputStream的子类
重定向通过System类的setIn()setOut()方法对默认设备进行改变 public static void setIn(InputStream in) public static void setOut(PrintStream out) import java.io.*;/*** Author: Yeman* Date: 2021-09-26-20:13* Description:*/
public class Test {public static void main(String[] args) {BufferedReader bis null;try {//1、提供转换流System.in是字节流将其转换为字符流System.out.println(请输入信息(退出输入e或exit):);InputStreamReader isr new InputStreamReader(System.in);//2、提供缓冲流将输入的一行读取bis new BufferedReader(isr);//3、读操作String s null;while ((s bis.readLine()) ! null){if (e.equalsIgnoreCase(s) || exit.equalsIgnoreCase(s)){System.out.println(程序结束退出程序);break;}System.out.println( s.toUpperCase());System.out.println(继续输入(退出输入e或exit):);}} catch (IOException e) {e.printStackTrace();} finally {//4、关闭外层流try {if (bis ! null) bis.close();} catch (IOException e) {e.printStackTrace();}}}
}打印流
实现将基本数据类型的数据格式转化为字符串输出。
打印流PrintStream和PrintWriter 提供了一系列重载的print()和println()方法用于多种数据类型的输出 PrintStream和PrintWriter的输出不会抛出IOException异常 PrintStream和PrintWriter有自动flush功能 PrintStream打印的所有字符都使用平台的默认字符编码转换为字节 在需要写入字符而不是写入字节的情况下应该使用PrintWriter类。
System.out返回的是PrintStream的实例。
常与System.out搭配使用可以不在控制台输出而是输出到指定位置
import java.io.*;/*** Author: Yeman* Date: 2021-09-26-20:13* Description:*/
public class Test {public static void main(String[] args) {PrintStream ps null;try {FileOutputStream fos new FileOutputStream(new File(IO\\text.txt));
// 创建打印输出流,设置为自动刷新模式(写入换行符或字节 \n 时都会刷新输出缓冲区)ps new PrintStream(fos, true);if (ps ! null) {// 把标准输出流(控制台输出)改成文件System.setOut(ps);}for (int i 0; i 255; i) { // 输出ASCII字符System.out.print((char) i);if (i % 50 0) { // 每50个数据一行System.out.println(); // 换行}}} catch (FileNotFoundException e) {e.printStackTrace();} finally {if (ps ! null) {ps.close();}}}
}数据流
为了方便地操作Java语言的基本数据类型和String的数据可以使用数据流。
数据流有两个类(用于读取和写出基本数据类型、String类的数据 DataInputStream 和 DataOutputStream 分别“套接”在 InputStream 和 OutputStream 子类的流上。 import java.io.*;/*** Author: Yeman* Date: 2021-09-26-20:13* Description:*/
public class Test {public static void main(String[] args) {//写DataOutputStream dos null;try {dos new DataOutputStream(new FileOutputStream(IO\\test.txt));dos.writeUTF(叶绿体);dos.writeInt(22);dos.writeBoolean(true);} catch (IOException e) {e.printStackTrace();} finally {try {if (dos ! null) dos.close();} catch (IOException e) {e.printStackTrace();}}//读DataInputStream dis null;try {dis new DataInputStream(new FileInputStream(IO\\test.txt));//注意读的顺序要和写的顺序一样String name dis.readUTF();int age dis.readInt();boolean isMan dis.readBoolean();System.out.println(name age isMan);} catch (IOException e) {e.printStackTrace();} finally {try {if (dis ! null) dis.close();} catch (IOException e) {e.printStackTrace();}}}
}对象流
ObjectInputStream和OjbectOutputSteam
用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中也能把对象从数据源中还原回来。
序列化用ObjectOutputStream类保存基本类型数据或对象的机制 反序列化用ObjectInputStream类读取基本类型数据或对象的机制
ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量。
实现Serializable或者Externalizable两个接口之一的类的对象才可序列化关于对象序列化详见序列化
可序列化对象
import java.io.Serializable;/*** Author: Yeman* Date: 2021-09-27-8:27* Description:*/class pet implements Serializable {public static final long serialVersionUID 999794470754667999L;private String name;public pet(String name) {this.name name;}Overridepublic String toString() {return pet{ name name \ };}
}public class Person implements Serializable {public static final long serialVersionUID 6849794470754667999L;private String name;private int age;private pet pet;public Person(String name, int age, pet pet) {this.name name;this.age age;this.pet pet;}Overridepublic String toString() {return Person{ name name \ , age age , pet pet };}
}序列化ObjectOutputStream
import java.io.*;/*** Author: Yeman* Date: 2021-09-26-20:13* Description:*/
public class Test {public static void main(String[] args) {ObjectOutputStream oos null;try {oos new ObjectOutputStream(new FileOutputStream(IO\\test.txt));oos.writeUTF(new String(你好世界));oos.flush();oos.writeObject(new Person(Lily,20,new pet(Xinxin)));oos.flush();} catch (IOException e) {e.printStackTrace();} finally {try {if (oos ! null) oos.close();} catch (IOException e) {e.printStackTrace();}}}
}反序列化ObjectInputStream
import java.io.*;/*** Author: Yeman* Date: 2021-09-26-20:13* Description:*/
public class Test {public static void main(String[] args) {ObjectInputStream ois null;try {ois new ObjectInputStream(new FileInputStream(IO\\test.txt));String s ois.readUTF();Person o (Person) ois.readObject();System.out.println(o.toString());} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally {if (ois ! null) {try {ois.close();} catch (IOException e) {e.printStackTrace();}}}}
}随机存取文件流
RandomAccessFile声明在java.io包下但直接继承于java.lang.Object类。并且它实现了DataInput、DataOutput这两个接口也就意味着这个类既可以读也可以写。
RandomAccessFile类支持 “随机访问” 的方式程序可以直接跳到文件的任意地方来读写文件①支持只访问文件的部分内容②可以向已存在的文件后追加内容③若文件不存在则创建④若文件存在则从指针位置开始覆盖内容而不是覆盖文件。
RandomAccessFile对象包含一个记录指针用以标示当前读写处的位置RandomAccessFile类对象可以自由移动记录指针 long getFilePointer()获取文件记录指针的当前位置 void seek(long pos)将文件记录指针定位到 pos 位置
构造器 public RandomAccessFile(File file, String mode) public RandomAccessFile(String name, String mode) 创建 RandomAccessFile 类实例需要指定一个 mode 参数该参数指定 RandomAccessFile 的访问模式 如果模式为只读r。则不会创建文件而是会去读取一个已经存在的文件如果读取的文件不存在则会出现异常。 如果模式为rw读写。如果文件不存在则会去创建文件如果存在则不会创建。
import java.io.*;/*** Author: Yeman* Date: 2021-09-26-20:13* Description:*/
public class Test {public static void main(String[] args) {RandomAccessFile r1 null;RandomAccessFile rw null;try {r1 new RandomAccessFile(IO\\input.jpg, r);rw new RandomAccessFile(IO\\output.jpg, rw);byte[] bytes new byte[1024];int len r1.read(bytes);while (len ! -1){rw.write(bytes,0,len);len r1.read(bytes);}} catch (IOException e) {e.printStackTrace();} finally {try {if (rw ! null) rw.close();} catch (IOException e) {e.printStackTrace();}try {if (r1 ! null) r1.close();} catch (IOException e) {e.printStackTrace();}}}
}Java NIO
Java NIO (New IONon-Blocking IO)是从Java 1.4版本开始引入的一套新的IO API可以替代标准的Java IO API。NIO与原来的IO有同样的作用和目的但是使用的方式完全不同NIO支持面向缓冲区的(IO是面向流的)、基于通道的IO操作NIO将以更加高效的方式进行文件的读写操作。
Java API中提供了两套NIO一套是针对标准输入输出NIO另一套就是网络编程NIO。 随着 JDK 7 的发布Java对NIO进行了极大的扩展增强了对文件处理和文件系统特性的支持以至于我们称他们为 NIO.2。因为 NIO 提供的一些功能NIO已经成为文件处理中越来越重要的部分。
早期的Java只提供了一个File类来访问文件系统但File类的功能比较有限所提供的方法性能也不高。而且大多数方法在出错时仅返回失败并不会提供异常信息。 NIO. 2为了弥补这种不足引入了Path接口代表一个平台无关的平台路径描述了目录结构中文件的位置。Path可以看成是File类的升级版本实际引用的资源也可以不存在。
在以前IO操作都是这样写的
import java.io.File;
File file new File(index.html);但在Java7 中可以这样写
import java.nio.file.Path;
import java.nio.file.Paths;
Path path Paths.get(index.html);同时NIO.2在java.nio.file包下还提供了Files、Paths工具类Files包含了大量静态的工具方法来操作文件Paths则包含了两个返回Path的静态工厂方法。
Paths 类提供的静态 get() 方法用来获取 Path 对象 static Path get(String first, String … more) : 用于将多个字符串串连成路径 static Path get(URI uri): 返回指定uri对应的Path路径