极品wordpress素材教程网站,福安网站开发,2022年最火的关键词,做app网站的公司名称目录
一、文件
1、文件系统和路径
2、File类
3、FilenameFilter接口
二、IO流
1、流的分类
2、流的体系结构
三、字节流
1、InputStream
2、OutputStream
四、字符流
1、Reader
2、Writer
五、过滤流和转换流
1、过滤流
2、转换流
六、序列化
1、对象序列化…目录
一、文件
1、文件系统和路径
2、File类
3、FilenameFilter接口
二、IO流
1、流的分类
2、流的体系结构
三、字节流
1、InputStream
2、OutputStream
四、字符流
1、Reader
2、Writer
五、过滤流和转换流
1、过滤流
2、转换流
六、序列化
1、对象序列化与反序列化
2、ObjectInputStream和ObjectOutputStream
七、NIO
1、Buffer
2、Channel 一、文件
1、文件系统和路径 一个文件系统包含三类对象文件、目录和符号链接。 文件系统中的对象用路径来作为唯一的识别路径有绝对路径根目录为参照和相对路径当前目录为基础两种。
2、File类 java.io包提供了一系列用于文件处理的接口和类如下图。 代码实例
1File变量不一定初始化一个文件名也可以是一个路径“.”表示当前路径在vscode环境下代表当前项目的根目录而不是当前包的路径。
2try...catch语句当File初始化的file变量路径不存在是生成一个该文件夹或文件如果File初始化的是一个文件那么如果该文件不存在则在他应在的路径位置上生成一个该文件若文件夹不存在则生成一个该文件夹。
3对于vscode编译环境使用file.list()生成“.”路径下的文件列表输出是.vscode,bin,src这一类的项目根目录下的文件夹。file.listroots()生成的绝对路径文件列表输出为C:/,D:/的系统根路径下目录。
import java.io.File;
import java.io.IOException;public class Demo {public static void main(String[] args){File filenew File(.); //.是指当前路径System.out.println(file.getName()); //获取文件名System.out.println(file.getParent()); //父路径System.out.println(file.getAbsolutePath()); //绝对路径System.out.println(file.exists()); //file是否存在try{file.createNewFile(); //如果file不存在则在当前路径下创建 }catch(IOException e){e.printStackTrace();}String[] listfile.list(); //当前路径的文件列表for(String name:list)System.out.println(name);File[] listrootsfile.listRoots(); //电脑根目录下的文件列表for(File name:listroots)System.out.println(name);}
}3、FilenameFilter接口 FilenameFilter接口是一个文件过滤器接口可以将符合条件的文件筛选出来FilenameFilter接口只有一个acceptFile dirString name方法用于筛选符合条件的文件返回true。 上面File类提到的list方法也可以接受FilenameFilter类型的参数。 下面实例返回后缀为.zip和.txt的符合条件的文件列表。
String[] filterFileNamesfile.list(new FilenameFilter() {public boolean accept(File dir,String name){return(name.endsWith(.zip)||name.endsWith(.txt)); //过滤特定后缀}}
);
for(String name:filterFileNames)System.out.println(name);
二、IO流 IO流是实现数据输入和输出的基础流的优势在于使用统一的方式对数据进行操作或传递简化代码操作。
1、流的分类
按流的流向来分类 输入流只能从输入流中读取数据 输出流只能从输出流中写入数据
按流所操作的基本数据单元来分类 字节流所操作的基本数据单元是8位的字节byte 字符流所操作的基本数据单元是16位的字符unicode
按流的角色来分类 节点流用于从/向一个特定IO设备中读/写数据的流 处理流对一个已经存在的流进行连接或封锁通过封锁后的流来实现数据的读/写功能 2、流的体系结构 在使用IO流时如果内容是文本内容则使用字符流若内容是二进制内容如图像则使用字节流。 Java的IO流体系如下 三、字节流 字节流的最基本的两个抽象类是InputStream和OutputStream这两个都是抽象类不能进行实例化所以必须用子类如FileInputStream和FileOutputStream来进行实例化。
1、InputStream InputStream字节输入流的方法 InputStream子类 读取文件的实例注意缓冲区的设置
public class stream {public static void main(String[] args){FileInputStream finnull;try{fin new FileInputStream(src/io/stream.java);byte[] bbufnew byte[1024]; //缓冲区 int hasRead 0; while((hasReadfin.read(bbuf))0) //循环读取文件数据read输出当前字节数System.out.println(new String(bbuf,0,hasRead)); //String构造函数三个参数byte[],offset,length}catch(IOException e){e.printStackTrace();}finally{try{fin.close();}catch(IOException e){e.printStackTrace();}}}
}
2、OutputStream OutputStream字节输出流的方法 OutputStream的子类 写入文件的实例
import java.io.*;
import java.util.Scanner;public class stream {public static void main(String[] args){File filenew File(src/io/1.txt);try{file.createNewFile(); //创建一个文件}catch(IOException e){e.printStackTrace();}Scanner scnew Scanner(System.in); FileOutputStream foutnull;try{foutnew FileOutputStream(src/io/1.txt); //输出字节流System.out.println(请输入内容);String strsc.nextLine(); //输入为Stringfout.write(str.getBytes()); //转换为byte数组}catch(IOException e){e.printStackTrace();}finally{try{fout.close();sc.close(); //关闭扫描流很新颖}catch(IOException e){e.printStackTrace();}}}
}四、字符流 字符流处理数据基本单元是字符也就是char[]数组而不是字节流的byte[]数组。 字符流的两个基本流是字符输入流Reader和字符输出流Writer这两个都是抽象类不能进行实例化可以使用FileReader和FileWriter来创建实例。
1、Reader Reader的子类 Reader方法 Reader实例 由于FileReader接口可以读文件BufferedReader接口参数为ReaderFileReader只有read()方法每次读一个字符而BufferedReader存在新方法ReadLine可以逐行读所以构成了下面的嵌套格式。
public class stream2 {public static void main(String []args){BufferedReader brnull;try{brnew BufferedReader(new FileReader(src/io/stream3.java)); //会引发FileNotFoundException但该异常蕴含与IOExceptionString resultnull;while((resultbr.readLine())!null){System.out.println(result);}}catch(IOException e){e.printStackTrace();}finally{try{br.close(); //关闭字符流}catch(Exception e){e.printStackTrace();}}}
}
2、Writer Writer子类 Writer方法 Writer实例 文件写入字符流可以直接用write写入键盘输入的String变量不需要嵌套。
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;public class stream2 {public static void main(String []args){Scanner scnew Scanner(System.in);FileWriter fwnull;try{fwnew FileWriter(src/io/1.txt);System.out.println(请输入内容 );String strsc.nextLine();fw.write(str);}catch(IOException e){e.printStackTrace();}finally{try{fw.close();sc.close();}catch(IOException e){e.printStackTrace();}}}
}五、过滤流和转换流
1、过滤流 过滤流用于对已有流进行连接和封装处理分为过滤输入流FilterInputStream和过滤输出流FilterOutputStream分别继承于InputStream和OutputStream。 过滤流可以参见字节流处理中的BufferedInputStream类。
2、转换流 转换流就是将字节流和字符流之间相互转换。 InputStreamReader将字节输入流转换成字符输入流 OutputStreamWriter将字符输出流转换成字节输出流
六、序列化
1、对象序列化与反序列化 对象序列化将对象数据写入一个输出流的过程。将代码转换成二进制格式 反序列化从一个输入流中读取一个对象。从二进制格式中读代码 对象序列化的两个特点
1可以在分布式应用中使用参数和返回值必须序列化
2可以循环保存每一个对象的数据。 序列化接口java.lang.Serializable接口接口中没有任何方法当一个类implements该接口则该类为可序列化类可序列化类可以进行对象序列化。
2、ObjectInputStream和ObjectOutputStream ObjectInputStream和ObjectOutputStream分别是InputStream和OutputStream的子类通过对象输出流通过writeObject来写入序列化对象对象输入流通过readObject来读取序列化对象。 下面对象序列化实例 try括号内建立回收机制当try...catch结束后自动对其执行close。
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;public class filter {public static void main(String [] args){try(ObjectOutputStream obsnew ObjectOutputStream(new FileOutputStream(src/io/1.txt))){Person personnew Person(张三,25);obs.writeObject(person);obs.flush();}catch(Exception e){e.printStackTrace();}}
}class Person implements Serializable{private int age;private String name;public Person(String name,int age){this.namename;this.ageage;}public String toString(){return 姓名this.name年龄this.age;}
}七、NIO NIO类将文件或文件的一段区域映射到内存中可以像访问内存一样访问文件。 NIO中两个核心类Buffer和ChannelBuffer本质上是一个数组Channel类似于流传输相比于输入输出流提供了一个map方法将一块数据直接映射到内存。
1、Buffer Buffer类没有构造方法而是通过静态方法获得Buffer对象。 static xxxBuffer allocate(int capacity) : 创建一个指令容量的xxxBuffer对象。xxx代表数据类型如char Buffer的四个重要方法capacity容量limit界限position位置mark标记。 capacity最开始allocate设置的长度 limit初始化为capacity大小当执行flip方法后变为当前position大小当执行clear方法后再回到capacity大小 position初始化为0当添加元素时为添加元素所占总长度不会因为后续获取靠前的数据而发生变化因为Buffer类本质上是一个数组当获取值后不会影响数组变化 mark设置mark为当前position 其他常用方法
clear()将position设置为0limit设置为capacity丢弃标记flip()将limit设置为当前positionposition设置为0rewind()将position设置为0丢弃markreset()将position设置为以前的markget(2)将索引为2的元素获取若为添加参数则传入当前position的值put(a)添加元素a 测试代码
import java.nio.CharBuffer;public class nioDemo {public static void main(String[] args){CharBuffer buffCharBuffer.allocate(8);System.out.println(buff.capacity()); //输出8System.out.println(buff.limit()); //输出0System.out.println(buff.position()); //输出0buff.put(a);buff.put(b);buff.put(c);System.out.println(buff.limit()); //输出8System.out.println(buff.position()); //输出3System.out.println(buff.get(0)); //输出abuff.flip(); //此时limit3position0System.out.println(buff.get()); //输出abuff.clear(); //position0,limit8System.out.println(buff.limit()); //输出8}
}
2、Channel Channel与Buffer的区别
1Channel可以直接将指定文件部分或全部映射成Buffer
2程序不能直接访问Channel数据Channel只能与Buffer交互。 参考书籍《Java 8 基础应用与开发》QST青软实训编