江苏住房与城乡建设厅网站,深圳小企业网站建设,公司网站模板源码,三明seo优化十九、流 上一篇 个人整理非商业用途#xff0c;欢迎探讨与指正#xff01;#xff01; 文章目录 十九、流19.1流的概念19.2File类19.2.1File对象的创建19.2.2Java中的路径表示19.2.3File中的常用方法19.2.4FileNameFilter接口 19.3IO流19.3.1流的划分19.3.2字节流[重点]…十九、流
« 上一篇 个人整理非商业用途欢迎探讨与指正 文章目录 十九、流19.1流的概念19.2File类19.2.1File对象的创建19.2.2Java中的路径表示19.2.3File中的常用方法19.2.4FileNameFilter接口 19.3IO流19.3.1流的划分19.3.2字节流[重点]19.3.3IO流使用时的细节19.3.4字节缓冲流19.3.5文件拷贝19.3.6对象流19.3.7数据流19.3.8字符流19.3.9字符缓冲流19.3.10打印流19.3.11转换流19.3.12Properties流操作 19.4NIO 19.1流的概念
在程序中,数据是需要传输的,例如将磁盘上的数据显示到浏览器上,需要将数据进行传输 java中将数据传输的技术称为流
19.2File类
19.2.1File对象的创建
将系统中磁盘上的文件或者文件夹转换为java中的对象
public class Demo01 {public static void main(String[] args) {
// 将D盘下的IO文件夹创建为java对象
// 文件夹File file new File(D:/IO);
// 判断是否存在的方法System.out.println(file.exists());// 文件file new File(D:/IO/a.txt);System.out.println(file.exists());// 分割符可以使用 / 或者 \\(转义字符)file new File(D:\\IO\\a.txt);System.out.println(file.exists());}
}19.2.2Java中的路径表示
相对路径: 相对于当前的项目或者工程位置 绝对路径: 在磁盘上的位置
public class Demo02 {public static void main(String[] args) {
// 绝对路径File file new File(D:/IO/a.txt);System.out.println(file.exists());// 相对路径
// 以/开头(\\) 表示当前项目的所在磁盘(省略了当前项目的所在磁盘)
// 我的项目在D盘: /省略了D:File file2 new File(/IO/a.txt);System.out.println(file2.exists());file2 new File(/02-tools);System.out.println(file2.exists());// 不以/开头 针对当前项目的根路径File file3 new File(a.txt);System.out.println(file3.exists());// 创建IO/c.txt对象file3 new File(IO/c.txt);//当前项目中的IO文件夹下的c.txt文件System.out.println(file3.exists());// 创建com.qf.test01.Demo02文件对象file3 new File(src/com/qf/test01/Demo02.java);System.out.println(file3.exists());}
}19.2.3File中的常用方法
public class Demo03 {
// File的常用方法public static void main(String[] args) {File file new File(IO/d.txt);// 创建一个文件try {
// 创建一个文件file.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}public class Demo04 {// 创建文件夹(目录)
// mkdir:创建一个目录,若父级目录不存在,则不创建(只能创建一层目录)
// mkdirs:创建一个目录,若父级目录不存在,则会一起创建(可以创建多层目录)public static void main(String[] args) {File file new File(java);
//
// file.mkdir();
// file.mkdirs();file new File(se/io);
// file.mkdir();file.mkdirs();}
}public class Demo05 {public static void main(String[] args) {
// 删除方法,删除文件或者空目录File file new File(a.txt);file.delete();file new File(IO);
// 非空目录无法删除file.delete();file new File(java);
// 空的目录可以删除file.delete();file new File(se);file.delete();
// 判断目录或者是文件是否存在System.out.println(file.exists());
// 获取文件或者文件夹的大小System.out.println(file.length());System.out.println(new File(b.txt).length());}
}public class Demo06 {public static void main(String[] args) {File file new File(IO/c.txt);// 绝对路径的String格式System.out.println(file.getAbsolutePath());
// 返回绝对路径 返回值是FileSystem.out.println(file.getAbsoluteFile());
// 获取文件(夹)的名字System.out.println(file.getName());
// 获取父目录System.out.println(file.getParent());
// 判断是否为目录System.out.println(file.isDirectory());
// 判断是否为文件System.out.println(file.isFile());file new File(D:\\01-classin\\14-qf-jy-2305\\01-java\\03-课堂代码\\workspace_JAVASE\\20231221-25-JavaSE-IO流\\IO\\c.txt);
// 获取路径System.out.println(file.getPath());System.out.println(--------------------);
// 获取目录中的所有文件列表 查看D盘下的所有文件和目录file new File(D:/);File[] listFiles file.listFiles();for (File file2 : listFiles) {System.out.println(file2);}System.out.println(--------------------);
// 可以查看src下的所有内容file new File(src/com/qf/test01);listFiles file.listFiles();for (File file2 : listFiles) {System.out.println(file2);}}
}19.2.4FileNameFilter接口
文件过滤器接口
public class Demo07 {public static void main(String[] args) {File file new File(src/com/qf/test01);
// 对文件进行过滤
// file.listFiles(new MyFilter());File[] listFiles file.listFiles(new FilenameFilter() {
// dir当前调用者 name具体的名字Overridepublic boolean accept(File dir, String name) {
// System.out.println(dir,name);
// 帮助我们得到想到的文件return name.startsWith(Test);}});for (File f : listFiles) {System.out.println(f);}}
}
class MyFilter implements FilenameFilter {Overridepublic boolean accept(File dir, String name) {// TODO Auto-generated method stubreturn false;}
}19.3IO流
文件是内存与存储设备之间的数据传输通道载体需要借助流进行传输 所有的IO操作都会抛出IOException异常 所有的IO操作需要将资源关闭
19.3.1流的划分
按照方向划分: 输出流:将内存中的内容写入到磁盘中(存进去) 输入流:将存储设备中的内容读入到内存中(拿出来) 按照单位划分: 字节流:以字节为单位,进行所有文件的读与写 字符流:以字符为单位,只可以对文本数据进行读写(字符流是由字节流转换来的) 按照功能划分: 节点流:具有实际数据传输功能的流(实现类) 过滤流:在上述节点流的基础上进行功能的提升
19.3.2字节流[重点]
字节流: 抽象类: InputStream:字节输入流 读取内容 OutputStream:字节输出流 写入内容 子类: FileInputStream:文件字节输入流 FileOutputStream:文件字节输出流
public class Demo01 {// 字节输出流public static void main(String[] args) throws IOException {
// 创建FileOutputStream对象,可以向参数中的File内去写内容
// 文件可以不存在,第一次写的时候FileOutputStream会创建一个文件
// 若文件存在,直接向对应的文件中写内容FileOutputStream fos new FileOutputStream(new File(IO/hello.txt));// 写入一个int数据
// 一次写入一个字符fos.write(97);fos.write(65);fos.write(90);fos.write(94);// 一次写入一个byte[]byte[] bs {97,98,99,100,101};fos.write(bs);// 重载方法
// 指定的开始的位置,和长度fos.write(bs, 2, 3);System.out.println(写入成功!);
// 所有的流资源都需要关闭fos.close();}
}
public class Demo02 {public static void main(String[] args) throws IOException {
// fos一共有四个构造方法
// 1.通过file创建fos对象
// FileOutputStream fos new FileOutputStream(new File(IO/hello.txt));
// 2.通过文件名(String path)创建fos对象
// FileOutputStream fos new FileOutputStream(IO/hello.txt);
// 3.通过file和string都有一个append重载的构造方法 append的作用是内容的追加
// FileOutputStream fos new FileOutputStream(IO/hello.txt,true);FileOutputStream fos new FileOutputStream(new File(IO/hello.txt),true);fos.write(97);System.out.println(写入成功!);
// 所有的流资源都需要关闭fos.close();}
}public class Demo03 {public static void main(String[] args) throws IOException {
// 读的文件必须是真实存在的FileInputStream fis new FileInputStream(IO/hello.txt);// 读取内容 读取到结尾返回-1
// 一次读取一个字节int read fis.read();/*System.out.println(read);read fis.read();System.out.println(read);read fis.read();System.out.println(read);read fis.read();System.out.println(read);*/while(read ! -1) {System.out.print((char)read);
// 再向下读取一次read fis.read();}fis.close();}
}
public class Demo04 {public static void main(String[] args) throws IOException {
// 读的文件必须是真实存在的FileInputStream fis new FileInputStream(IO/hello.txt);// 读取按照byte[]进行读取
// 从流中将内容存储到byte[]中
// 数组的大小是多少都是ok的,一般情况下每次读取文件的大小1kb~4kb(对于字节流读取文件的速度是最快的)byte[] bs new byte[1024];//1kbSystem.out.println(Arrays.toString(bs));
// 返回值为文件内容的长度
// int a fis.read(bs);int a fis.read(bs, 10, 10);//放到数组中的某个位置 off开始位置,len长度System.out.println(a);// byte[]中是有值的System.out.println(Arrays.toString(bs));System.out.println(new String(bs,0,a));fis.close();}
}19.3.3IO流使用时的细节
在使用输出流时,若文件不存在是会自动创建文件的(必须要保证父目录在),若存在自动想以存在的文件中添加内容 在写文件时,若想向追加内容(不是覆盖),可以使用输出流的重载构造,参数为append,默认为false,设置为true就是追加内容 在读取文件时,文件是必须真实存在,若不存在在抛出异常(FileNotFoundException) 在使用IO流时,无论读写,无论有没有发生异常都需要将流资源进行关闭,关闭资源代码写在finally中 将IO流的创建可以写在try()中,这个的IO流操作是自动关闭资源
public class Demo01 {public static void main(String[] args) {
// 可以创建文件,不能创建文件夹FileOutputStream fos null;try {fos new FileOutputStream(IO/HelloWorld.java);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {
// 无论怎样都需要关闭if(fos ! null) {fos.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
}
public class Demo02 {public static void main(String[] args) {
// 可以将流资源放入到try()中,可以不关闭流,自动为我们关闭的try(FileOutputStream fos new FileOutputStream(IO/HelloWorld.java);FileInputStream fis new FileInputStream(IO/hello.txt);){}catch (Exception e) {// TODO: handle exception} finally {}}
}19.3.4字节缓冲流
BufferedOutputStream/BufferedInputStream 提高IO效率,减少磁盘的访问次数 数据存储在缓冲区中,需要使用flush方法将内容写入文件,close同样有flush的效果
public class Demo01 {public static void main(String[] args) throws Exception {
// 节点流FileOutputStream fos new FileOutputStream(IO/e.txt,true);
// 过滤流BufferedOutputStream bos new BufferedOutputStream(fos);bos.write(97);bos.write(helloworld,你好.getBytes());System.out.println(写完了...);// 内容少,用close可以进行冲刷bos.flush();bos.close();fos.close();}
}public class Demo01 {public static void main(String[] args) throws Exception {
// 节点流FileOutputStream fos new FileOutputStream(IO/e.txt,true);
// 过滤流BufferedOutputStream bos new BufferedOutputStream(fos);bos.write(97);bos.write(helloworld,你好.getBytes());System.out.println(写完了...);// 内容少,用close可以进行冲刷bos.flush();bos.close();fos.close();}
}19.3.5文件拷贝
public class Copy {public static void main(String[] args) throws IOException {copy1();}// 字节流拷贝 IO的次数非常多public static void copy() throws IOException {long start System.currentTimeMillis();FileInputStream fis new FileInputStream(D:/IO/ideaIU-2023.1.2.exe);FileOutputStream fos new FileOutputStream(D:/IO/ideaIU-2023.1.2(1).exe);
// 4kb基本上就是一次拷贝的最大容量 在大的容量就没有速度上的明显提升了byte[] bs new byte[1024*4];int len;while( (len fis.read(bs)) ! -1 ) {fos.write(bs,0,len);}long end System.currentTimeMillis();fos.close();fis.close();System.out.println(拷贝时间: (end - start));}// 缓冲字节流拷贝 IO的次数明显较少public static void copy1() throws IOException {long start System.currentTimeMillis();FileInputStream fis new FileInputStream(D:/IO/ideaIU-2023.1.2.exe);FileOutputStream fos new FileOutputStream(D:/IO/ideaIU-2023.1.2(1).exe);BufferedInputStream bis new BufferedInputStream(fis);BufferedOutputStream bos new BufferedOutputStream(fos);byte[] bs new byte[1024*4];int len;while( (len bis.read(bs)) ! -1 ) {bos.write(bs,0,len);}long end System.currentTimeMillis();bos.close();bis.close();fos.close();fis.close();System.out.println(拷贝时间: (end - start));}
}19.3.6对象流
将对象在磁盘中的写入和读取 写入对象到磁盘中被称为序列化 将磁盘中的对象读取出来被称为反序列化
public class Demo01 {public static void main(String[] args) throws Exception {FileOutputStream fos new FileOutputStream(IO/object.txt);
// 对象流ObjectOutputStream oos new ObjectOutputStream(fos);// 写入文档中对象
// 基本数据类型oos.writeInt(100);oos.writeDouble(100.12);oos.writeUTF(对象流);oos.close();fos.close();System.out.println(------------------------);FileInputStream fis new FileInputStream(IO/object.txt);ObjectInputStream ois new ObjectInputStream(fis);System.out.println(ois.readInt());System.out.println(ois.readDouble());System.out.println(ois.readUTF());ois.close();fis.close();}
}写入对象时,对应的类必须实现Seriablizable接口(可序列化接口)
public class Demo03 {public static void main(String[] args) throws Exception {FileOutputStream fos new FileOutputStream(IO/object.txt);
// 对象流ObjectOutputStream oos new ObjectOutputStream(fos);Dog dog new Dog(小白, 1);oos.writeObject(dog);oos.close();fos.close();}
}
// 可以被序列化的类
class Dog implements Serializable{String name;
// 若int类型的age不想被序列化 transient int age;Overridepublic String toString() {return Dog [name name , age age ];}public Dog(String name, int age) {super();this.name name;this.age age;}
}
public class Demo04 {public static void main(String[] args) throws Exception {FileInputStream fis new FileInputStream(IO/object.txt);ObjectInputStream ois new ObjectInputStream(fis);Object object ois.readObject();System.out.println(object);ois.close();fis.close();}
}19.3.7数据流
DataInputStream/DataOutputStream 也是过滤流,封装了对基本类型和String的读写
public class Demo01 {public static void main(String[] args) throws Exception {FileOutputStream fos new FileOutputStream(IO/f.txt);DataOutputStream dos new DataOutputStream(fos);dos.writeInt(100);dos.close();fos.close();FileInputStream fis new FileInputStream(IO/f.txt);DataInputStream dis new DataInputStream(fis);System.out.println(dis.readInt());dis.close();fis.close();}
}19.3.8字符流
是由字节流转换来的 抽象类:Reader Writer 实现类:FileReader FileWriter 操作字符的
public class Demo01 {public static void main(String[] args) throws Exception {Reader reader new FileReader(IO/hello.txt);int r reader.read();System.out.println(r);r reader.read();System.out.println(r);reader.close();}
}
public class Demo02 {public static void main(String[] args) throws IOException {FileWriter writer new FileWriter(IO/hello.txt,true);// 直接写入一个字符串writer.write(HELLOWORLD);writer.close();}
}19.3.9字符缓冲流
BufferedWriter/BufferedReader 支持读写一行的操作
public class Demo01 {public static void main(String[] args) throws Exception {FileReader fr new FileReader(IO/hello.txt);BufferedReader br new BufferedReader(fr);String readLine;while((readLine br.readLine()) ! null) {System.out.println(readLine);}br.close();fr.close();}
}
public class Demo02 {public static void main(String[] args) throws Exception {FileWriter fw new FileWriter(IO/hello.txt,true);BufferedWriter bw new BufferedWriter(fw);
// 直接写一个换行符bw.newLine();bw.write(21.上课你不听课,在那嘎达整头像,你整他有啥用);bw.close();fw.close();}
}19.3.10打印流
public class Demo01 {public static void main(String[] args) throws IOException {PrintStream ps new PrintStream(IO/g.txt);ps.print(10);ps.println();ps.println(10);ps.print(19.2);ps.print(哈哈哈);ps.close();PrintWriter pw new PrintWriter(IO/h.txt);pw.print(哈哈哈);pw.close();}
}19.3.11转换流
InputStreamReader:将字节输入流转换为字符输入流 OutputStreamWriter:将字节输出流转换为字符输入流
public class Demo01 {public static void main(String[] args) throws Exception {OutputStream os new FileOutputStream(IO/z.txt);
// 从字节转换为字符OutputStreamWriter outputStreamWriter new OutputStreamWriter(os);outputStreamWriter.write(HELLOWORLD);outputStreamWriter.close();os.close();}
}19.3.12Properties流操作
操作属性文件 Properties是Hashtable的子类
usernametom
password123456
hobby\u5B66\u4E60
gender\u7537public class Demo01 {public static void main(String[] args) throws Exception {Properties p new Properties();// 加载属性文件p.load(new FileInputStream(pro/db.properties));
// 通过key获取valueObject value1 p.get(username);System.out.println(value1);value1 p.get(password);System.out.println(value1);value1 p.get(hobby);System.out.println(value1);value1 p.get(gender);System.out.println(value1);}
}19.4NIO
New IO 在JDK1.4后引入的读写机制,提高数据的读写效率
IO,面向流操作,以字节为单位进行读写(BIO) NIO,面向缓冲区操作,以块为单位进行读写
public class Demo01 {public static void main(String[] args) throws Exception {copyFile(pro/db.properties,pro/db1.properties);}/*** * param src 源路径* param dest 目标路径* throws IOException*/public static void copyFile(String src,String dest) throws IOException{
// 判断src是否存在,dest是否文件FileInputStream fis new FileInputStream(src);//读源文件FileOutputStream fos new FileOutputStream(dest);//写入到目标文件中// 获取管道FileChannel fileIn fis.getChannel();//读取时的管道FileChannel fileOut fos.getChannel();//写入时的管道// 申请缓冲区ByteBuffer buffer ByteBuffer.allocate(1024);//字节缓冲区,类似于byte[] bs new byte[1024];while( fileIn.read(buffer) ! -1) {
// 类似于指针的工具,需要它在最前面buffer.flip();
// 写fileOut.write(buffer);
// 类似于flush操作buffer.clear();}// 关闭fileOut.close();fileIn.close();fos.close();fis.close();System.out.println(over);}
}