低价刷粉网站推广,wp网站打开太慢怎么做优化,wordpress 文章版权 插件,wordpress模板旅游JavaIO流 一、概念二、File类三、File类的使用1、File文件/文件夹类的创建2、File类的获取操作3、File类判断操作 - boolean4、File类对文件/文件夹的增删改5、File类的获取子文件夹以及子文件的方法 四、Java中IO流多种维度的维度1、按照流向 - Java程序2、按照流的大小分类3、… JavaIO流 一、概念二、File类三、File类的使用1、File文件/文件夹类的创建2、File类的获取操作3、File类判断操作 - boolean4、File类对文件/文件夹的增删改5、File类的获取子文件夹以及子文件的方法 四、Java中IO流多种维度的维度1、按照流向 - Java程序2、按照流的大小分类3、按照流的功能分类 五、JavaIO流的四大基类1、InputStream2、OutputStream3、Reader4、Writer5、补充知识点——方法递归 六、JavaIO流中的常用实现类1、节点流2、处理流 一、概念
Java Input/Output一般情况指的是Java操作一些外部数据时使用IO流的形式进行操作外部数据主要包括文件、网络等等。
二、File类
JavaIO既可以操作文件外部数据还可以操作网络端口这种外部数据如果Java要操作文件外部数据必须要借助一个类File文件对象类。
File类是Java中对文件/文件夹的抽象表示通过这个File类我们可以将操作系统本地的一个文件/文件夹加载到Java程序当中随后通过File对象可以对文件进行增删改查等操作。
File类只能操作文件的外部内容、而文件当中有哪些数据这个操作File类做不到。
三、File类的使用
1、File文件/文件夹类的创建
根据全路径创建
File file new File(d: File.separator 桌面 File.separator a);根据父子路径创建
File file new File(d:\\桌面 ,a);根据父子路径创建只不过父路径也是File对象
File file new File(new File(d:\\桌面) ,a);2、File类的获取操作
//在eclipse中因为创建的时Java项目Java项目中所有的相对路径指的都是项目名下的某个路径 而非Java源文件的同级路径
File file1 new File(a/a.txt);
//获取文件名 路径最后一个文件/文件夹的名字
String fileName file.getName();
System.out.println(fileName);
//获取文件的父路径 取决于你再构建File对象时有没有传入父路径
String parent file.getParent();
System.out.println(parent);
//获取文件的路径 ---传入的路径
String path file.getPath();
System.out.println(path);
//获取文件的绝对的路径--传入路径没有关系的
String absolutePath file1.getAbsolutePath();
System.out.println(absolutePath);3、File类判断操作 - boolean
System.out.println(file1.exists());//判断路径是否存在
System.out.println(file1.isFile());//判断是否是文件
System.out.println(file1.isDirectory());//判断是否是目录
System.out.println(file1.isHidden());//判断是否是隐藏文件System.out.println(file1.canRead());//可读
System.out.println(file1.canWrite());//可写
System.out.println(file1.canExecute());//可执行4、File类对文件/文件夹的增删改
创建 创建文件createNewFile()要求父目录必须存在 创建文件夹mkdir()创建单层目录/mkdirs()创建多层目录
删除
delete()–如果是目录目录必须空目录
修改
renameTo(File):boolean
boolean mkdir file1.mkdirs();//创建文件夹
System.out.println(mkdir);boolean creatNewFile file1.createNewFile();//创建文件
System.out.println(creatNewFile);boolean delete file1.delete();
System.out.println(delete);
//重命名要求两个路径必须在同一个路径下
boolean result file1.renameTo(new File(KK));
System.out.println(result);5、File类的获取子文件夹以及子文件的方法
listFiles()
list():返回指定目录下的下一级的文件或者文件夹
四、Java中IO流多种维度的维度
1、按照流向 - Java程序
输入流 Input/Reader
输出流 Output/Writer
2、按照流的大小分类
字节流Stream——什么样的数据都可以处理
字符流Reader/Writer——只能处理纯文本类型的数据
3、按照流的功能分类
节点流直接对接到数据源上的流 常用的节点流文件流、数组流、网络流
处理流无法直接对接到数据源上而是包装了节点流在节点流基础之上提供了更加强大的功能
五、JavaIO流的四大基类
Javaio流中所有的流都有四个顶尖父类四个顶尖父类是四个抽象类四个抽象类当中封装了和流有关的很多的公用方法。
1、InputStream
Java中所有字节输入流的顶尖父类 —— 一个字节一个字节的读取文件中的数据
readint——读取的字节全部读取完成后返回-1
availableint——可利用的字节数还能读取的字节数
close()——关闭IO流任何IO流都需要手动关闭
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;/**
* InputStream是一个字节输入流
*/
public class Demo01 {public static void main(String[] args) {InputStream is null;try {is new FileInputStream(new File(kl/a.txt));//读取数据源的一个字节,read每一次读取完成下一次再进行读取基于上一次的结果向后读取int read is.read();System.out.println(read);//返回值如果是read()方法代表的是每一次读取完成的字节的值read(byte[])的话返回值不做研究//不管什么情况下read的返回值一旦为-1那么代表数据与没数据了int read2 is.read();System.out.println(read2);byte[] array new byte[12];int read3 is.read(array);String string new String(array,UTF-8);System.out.println(string);//字节流中可以利用的字节数有多少int available is.available();System.out.println(available);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(is!null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
}2、OutputStream
Java中所有字节输出流的顶尖父类
write写出的字节
close()
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;/*** OutputStream基类提供的常用方法* write(int字节)*/
public class Demo02 {public static void main(String[] args) {OutputStream os null;try {os new FileOutputStream(new File(kl/a.txt));os.write(97);//a 覆盖写os.write(中国加油.getBytes(UTF-8));//中国加油//os.write(中国加油.getBytes(UTF-8),0,6);//中国} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(os ! null) {try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
}复制照片到指定路径
package com.nuc.kl.file.io;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/**
*
* author 冰霜中的独舞
* version 2023年7月7日 上午9:15:11
*
*/
public class FileCopy {public static void main(String[] args) {InputStream is null;OutputStream os null;try {is new FileInputStream(new File(D:\\2023PracticalTraining\\software\\workspace\\eclipseworkspace\\java-study-619\\picture\\c.png));os new FileOutputStream(new File(D:\\2023PracticalTraining\\software\\workspace\\eclipseworkspace\\java-study-619\\picture\\d.png));//byte[] buf new byte[1024 * 1024];int read;while((read is.read()) ! -1) {os.write(read^5);}
// while((read is.read(buf)) ! -1) {
// os.write(buf);
// }} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if(os ! null) {try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(is ! null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
}import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/**
* 循环读取加密文件的1024个字节然后对下一个字节进行解密操作^5
* 输入流加密文件 输出流解密文件路径
* 每隔1024个字节对下一个字节进行^5运算进行加密
*/
public class Homework {public static void main(String[] args) {InputStream is null;OutputStream os null;try {is new FileInputStream(new File(D:\\Desktop\\a.png));os new FileOutputStream(new File(D:\\Desktop\\b.png));//循环读取is中的数据一次读取1024个字节byte[] by new byte[1024];while(is.read(by) ! -1) {os.write(by);int r is.read()^5;os.write(r);}System.out.println(解密完成);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(os ! null) {try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(is ! null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
}3、Reader
Java中所有字符输入流的顶尖父类
因为要根据编码集进行数据的读取一次要读取一个字符而一个字符对应了多个字节。编码集只有纯文本才有编码集。
read()close()
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Arrays;/**
* Read的常用方法
* close()
*/
public class Demo03 {public static void main(String[] args) throws IOException {Reader r new FileReader(new File(kl/a.txt));int read r.read();System.out.println(read);int read1 r.read();System.out.println(read1);char c (char)read1;System.out.println(c);char[] buf new char[10];r.read(buf);System.out.println(Arrays.toString(buf));System.out.println(buf);r.close();}
}a中国加油4、Writer
Java中所有字符输出流的顶尖父类
writeclose —— 底层调用了flushflush —— 字符流一次输出多个字节先把多个字节缓存起来直到遇到flush方法才会把缓存起来的结果输出
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Demo04 {public static void main(String[] args) throws IOException {Writer w new FileWriter(new File(kl/a.txt));w.append(a);w.write(zs12333);w.write(ls12333);w.flush();w.close();}
}5、补充知识点——方法递归
方法迭代其实就是一种循环只不过这个循环比较特殊循环我们只知道循环的终止条件而循环的次数我们是不太清楚的
使用方法
1、将需要重复性执行的代码抽取到一个方法中
2、方法中需要对重复性执行的代码进行改造有一个递归入口自己调用自己的一个逻辑、递归出口
/**
* 使用方法递归计算 ∑100
*/
public class RecursionStudy {public static void main(String[] args) {int num 0;//for循环for (int i 0; i 100; i) {num i num;}System.out.println(num);//迭代System.out.println(sum(100));}public static int sum(int num) {if(num 1) {return 1;}else {return num sum(num-1);}}
}六、JavaIO流中的常用实现类
1、节点流
直接连接数据源的流 数组流连接的数据源是一个数组 字节数组流 ByteArrayInputStream——字节数组输入流ByteArrayOutputStream——字节数组输出流-----输出的目的地是一个字节数组只不过这个字节数组不需要我们创建传递因为这个流底层封装了一个字节数组用于接收字节数据的输出 import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;public class Dmeo01 {public static void main(String[] args) throws IOException, InterruptedException {ByteArrayInputStream basi new ByteArrayInputStream(中国加油.getBytes(UTF-8));byte[] by new byte[12];basi.read(by);System.out.println(new String(by,UTF-8));ByteArrayOutputStream baos new ByteArrayOutputStream();System.out.println(baos);baos.write(中国加油.getBytes(UTF-8));System.out.println(baos);String string baos.toString(UTF-8);System.out.println(string);}
}字符数组流 CharArrayReader——字符数组输入流CharArrayWriter——字符数组输出流 文件流连接的数据源是一个文件 字节流——什么文件都可以处理 FileInputStream new FileInputStream(File)new FileInputStream(String path)【注意】文件必须存在不存在报错 FileOutputStream new FileOutputStream(File|String)——默认是覆盖写因为boolean append默认为falsenew FileOutputStream(File|String,boolean append)——追加写【注意】文件可以不用提前存在如果文件不存在会自动创建 字符流——只能处理纯文本文件 FileReader new FileReader(String|File) FileWriter new FileWriter(File|String)—默认是覆盖写new FileWriter(File|String,boolean append)写出数据之后必须flush()刷新 网络流
2、处理流
处理流对节点流进行包装提供一些更加强大的功能——处理速度快。
缓冲流缓冲流包装另外一个流缓冲流内部维护一个缓冲池使用了缓冲流一次读取多个字节、多个字符到缓冲流的内部的缓冲池当中。—— 不管是字节的还是字符的缓冲流一定要flush。 字节缓冲流 —— 缓冲了一个8192的字节数组 BufferedInputStreamBufferedOutputStream 字符缓冲流 BufferedReader——提供了一个特殊的方法readLine()BufferedWriter——提供了一个特殊的方法newLine()
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;public class Demo01 {public static void main(String[] args) throws Exception {BufferedReader br new BufferedReader(new FileReader(wc.txt));BufferedWriter bw new BufferedWriter(new FileWriter(wc_n.txt));String line null;while((line br.readLine()) ! null) {line line.toUpperCase();bw.write(line);bw.newLine();bw.flush();}bw.close();br.close();System.out.println(输出完成);}
}import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;public class Demo01 {public static void main(String[] args) throws Exception {BufferedReader br new BufferedReader(new FileReader(wc.txt));BufferedWriter bw new BufferedWriter(new FileWriter(wc_n.txt));String line null;while((line br.readLine()) ! null) {line line.toUpperCase();bw.write(line);bw.newLine();bw.flush();}bw.close();br.close();System.out.println(输出完成);}
}字节转字符流以指定的编码集将一个字节流转换为字符流进行处理加快我们的处理速度—纯文本类型的IO流有效 InputStreamReader(InputStream,charset)OutputStreamWriter(OutputStream,charset) 对象流——只有字节流 Java中的序列化Java对象转换成二进制编码和反序列化Java二进制编码转换为Java对象机制的问题 序列化和反序列化需要用到Java的两个IO流 ObjectInputStreamObjectOutputStream import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;public class Demo01 {public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {/*** 序列化的代码*/
// Student s new Student(zs,20,男);
// ObjectOutputStream oos new ObjectOutputStream(new FileOutputStream(student.txt));
// oos.writeObject(s);
// oos.flush();
// oos.close();
// System.out.println(对象序列化完成);/*** 反序列化的代码*/ObjectInputStream ois new ObjectInputStream(new FileInputStream(student.txt));Object object ois.readObject();System.out.println(object);}
}我们的Java对象如果想要序列化那么必须得声明它能序列化Java中对象默认不具备序列化的能力。如果想要具备那么必须实现两个接口其中一个即可序列化就是把对象的属性值转换成为二进制 Serializablestatic或者transient修饰的属性不会序列化 import java.io.Externalizable;
import java.io.Serializable;
import java.util.Objects;/**
* JavaBean:只要私有化的属性和公开的get和set方法还有hashCode、equals、toString以及构造器
*/
public class Student implements Serializable {private String name;private transient Integer student_age;private String student_sex;public Student() {super();}public Student(String name, Integer student_age, String student_sex) {super();this.name name;this.student_age student_age;this.student_sex student_sex;}public String getName() {return name;}public void setName(String name) {this.name name;}public Integer getStudent_age() {return student_age;}public void setStudent_age(Integer student_age) {this.student_age student_age;}public String getStudent_sex() {return student_sex;}public void setStudent_sex(String student_sex) {this.student_sex student_sex;}Overridepublic int hashCode() {return Objects.hash(name, student_age, student_sex);}Overridepublic boolean equals(Object obj) {if (this obj)return true;if (obj null)return false;if (getClass() ! obj.getClass())return false;Student other (Student) obj;return Objects.equals(name, other.name) Objects.equals(student_age, other.student_age) Objects.equals(student_sex, other.student_sex);}Overridepublic String toString() {return Student [name name , student_age student_age , student_sex student_sex ];}
}Externalizable必须重写两个方法一个序列化写出的方法一个反序列化读取的方法 import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.util.Objects;/**
* JavaBean:只要私有化的属性和公开的get和set方法还有hashCode、equals、toString以及构造器
*/
public class Student implements Externalizable {private static final long serialVersionUID 1L;private String name;private transient Integer student_age;private String student_sex;public Student() {super();}public Student(String name, Integer student_age, String student_sex) {super();this.name name;this.student_age student_age;this.student_sex student_sex;}public String getName() {return name;}public void setName(String name) {this.name name;}public Integer getStudent_age() {return student_age;}public void setStudent_age(Integer student_age) {this.student_age student_age;}public String getStudent_sex() {return student_sex;}public void setStudent_sex(String student_sex) {this.student_sex student_sex;}Overridepublic int hashCode() {return Objects.hash(name, student_age, student_sex);}Overridepublic boolean equals(Object obj) {if (this obj)return true;if (obj null)return false;if (getClass() ! obj.getClass())return false;Student other (Student) obj;return Objects.equals(name, other.name) Objects.equals(student_age, other.student_age) Objects.equals(student_sex, other.student_sex);}Overridepublic String toString() {return Student [name name , student_age student_age , student_sex student_sex ];}Overridepublic void writeExternal(ObjectOutput out) throws IOException {out.writeUTF(name);out.writeInt(student_age);}Overridepublic void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {name in.readUTF();student_age in.readInt();}
}序列化版本id是为了告诉编译器序列化的二进制和我们当前项目中的类是否为同一个版本如果是不同的版本那么报错。 打印流——只有输出流——提供了一系列重载的print和println方法用于输出数据 PrintStreamPrintWriter Java中标准输入和标准输出流 System.inSystem.outSystem.setIn(InputStream)System.setOut(OutputStream)