目录
一、文件
文件就是保存数据的地方
1.创建文件
①方式1
//方式1:new File(String pathname) 根据路径构建一个File对象
public static void create01() {
//创建文件对象
File file = new File("D:\\Javatest\\news1.txt");
//根据文件对象创建新文件方法,注意这里会出异常,需要处理
try {
file.createNewFile();
System.out.println("news1.txt创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
②方式2
//方式2:new File(File parent,String child) 根据父目录文件+子路径构建
public static void create02(){
//首先创建父目录文件对象
File f1 = new File("D:\\Javatest");
File f2 = new File(f1, "news2.txt");
//根据文件对象创建新文件方法,注意这里会出异常,需要处理
try {
f2.createNewFile();
System.out.println("news2.txt创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
③方式3
//方式3:new File(String parent,String child) 根据父目录路径和子路径构建
public static void create03(){
File f1 = new File("D:\\Javatest","news3.txt");
try {
f1.createNewFile();
System.out.println("news3.txt创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
2.获取文件相关信息
/**
* @author lei_abc
* @version 1.0
*/
public class FileInformation {
public static void main(String[] args) throws IOException {
info();
}
//获取文件的信息
public static void info() throws IOException {
//先创建文件对象
File f1 = new File("d:\\Javatest\\news1.txt");
//调用相应方法得到对应信息即可
//1.创建文件
f1.createNewFile();
//2.获取文件名
System.out.println("文件名:"+f1.getName());
//3.获取文件绝对路径
System.out.println("文件绝对路径:" + f1.getAbsolutePath());
//4.获取文件父目录
System.out.println("文件父级目录:" + f1.getParent());
//5.获取文件大小(字节)
System.out.println("文件大小:" + f1.length());
//6.判断文件是否存在
System.out.println("文件是否存在:" + f1.exists()); //true,因为上面已调勇创建文件方法
//7.判断是否为文件
System.out.println("是不是一个文件:" + f1.isFile()); //true
//8.判断是否为目录
System.out.println("是不是一个目录" + f1.isDirectory()); //false
}
}
3.目录操作
①删除
//1.删除文件与目录操作,目录可看成特殊的文件,exists方法也可判断
public static void m1() {
String filePath = "d:\\Javatest\\news1.txt";
File file = new File(filePath);
//判断file文件是否存在,存在则删除
if (file.exists()) {
//根据delete返回值判断是否成功,成功则输出删除成功
if (file.delete()) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
} else {
System.out.println("该文件/目录不存在");
}
}
②创建
mkdir() 创建一级目录
mkdirs() 创建多级目录
//3.创建目录,先判断目录是否存在,不存在则创建
public static void m3() {
String filePath = "d:\\Javatest\\new";
File file = new File(filePath);
if (file.exists()) {
System.out.println("该目录存在");
} else {
//创建目录方法
if (file.mkdir()) {
System.out.println("目录创建成功");
} else {
System.out.println("目录创建失败");
}
}
}
二、IO流原理及流的分类
1.IO流原理
① I/O流是Input/Output的缩写,I/O技术是非常实用的技术,用于处理数据传输。如读/写文件,网络通讯等。
②Java程序中,对于数据的输入/输出操作以“流(Stream)”的方式进行
③java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过方法输入或输出数据
④输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中
⑤输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中
2.流的分类
① 按操作数据单位不同分为:字节流(8bit)、字符流(按字符),读取二进制文件只能使用字节流,否则可能文件损毁
②按数据流的流向不同分为:输入流、输出流
③按流的角色不同分为:节点流、处理流/包装流
节点流是底层流/低级流,直接跟数据源相连接
处理流(包装流)包装节点流,既可以消除不同节点流的实现差异,也可以提供更方便的方法来完成输入输出,使用了修饰器模式,不会直接与数据源相连,以增加缓冲的方式来提高输入输出的效率
三、字节流
InputStream抽象类是所有类字节输入流的超类
InputStream常用的子类:
1.FileInputStream
①构造方法
//构造文件输入流时会有编译异常,必须处理
FileInputStream(File file)//通过一个文件对象来构造文件输入流
FileInputStream(FileDescriptor fdObj)//
FileInputStream(String name)//通过文件的绝对路径来构造文件输入流
②常用方法
1.read() //从此输入流中读取一个数据字节
2.read(byte[] b) //从此输入流中读取最多b.length个数据字节
3.close() //关闭此文件输入流并释放与此流相关的所有系统资源
public static void readFile01(){
String filePath = "D:\\Javatest\\news1.txt";
FileInputStream fts1 = null;
int readData = 0;
try {
//抛出异常需要处理
fts1 = new FileInputStream(filePath);
//通过read方法返回值是否为-1判断是否读取完成,直至读取完所有字节后结束循环
//返回-1代表读取完毕
while((readData = fts1.read()) != -1){
System.out.print((char) readData);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭文件流,因为JVM不会把流当垃圾清理,所以需要手动清理释放资源,需要处理异常
try {
fts1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void readFile02(){
String filePath = "D:\\Javatest\\news1.txt";
FileInputStream fts1 = null;
byte[] b1 = new byte[3];
int readData = 0;
try {
//抛出异常需要处理
fts1 = new FileInputStream(filePath);
//通过read方法返回值是否为-1判断是否读取完成,直至读取完所有字节后结束循环
//返回-1代表读取完毕
while((readData = fts1.read(b1)) != -1){
//参1代表读取的数组,参2代表从索引几开始读取,参3代表读取多少个,因为read(byte[] b)方法如果读取正常返回的是读取的个数
System.out.print(new String(b1,0,readData));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭文件流,因为JVM不会把流当垃圾清理,所以需要手动清理释放资源,需要处理异常
try {
fts1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.BufferedInputStream:缓冲字节输入流
3.ObjectInputStream:对象字节输入流
2.FileOutputStream
①构造方法
FileOutputStream(File file) //通过File对象创建文件输出流对象,默认每次写入覆盖之前内容
FileOutputStream(File file, boolean append) //append为true时,每次写入为追加,不覆盖
FileOutputStream(String name) //通过绝对路径创建文件输出流对象,默认每次写入覆盖之前内容
FileOutputStream(String name, boolean append) //append为true时,每次写入为追加,不覆盖
②常用方法
1.write(int b) 将指定字节数数据写入文件输出流
2.write(byte[] b) 将b.length字节数据写入文件输出流
3.write(byte[] b, int off, int len) 从b数组写入len个字节数据文件输出流,从off索引开始写入
4.close() 关闭此文件输出流并释放与此流有关的所有系统资源。
public static void writeFile(){
String filePath = "D:\\Javatest\\news1.txt";
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filePath,true);
System.out.println("创建字节输出流成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String s1 = "hello,lei_abc";
try {
fileOutputStream.write(s1.getBytes());
System.out.println("写入成功");
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.close();
System.out.println("字节输出流关闭成功");
} catch (IOException e) {
e.printStackTrace();
}
}
3.BufferedInputStream
字节处理流,与字符处理流的构造方法与常用方法相同
4.BufferedInputStream
字节处理流,与字符处理流的构造方法与常用方法相同
四、字符流
1.FileReader
① 构造方法
new FileReader(File/String) //通过File对象或者文件路径来构造FileReader对象
② 常用方法
1.read() //每次读取单个字符,返回该字符,如果到文件末尾返回-1
2.read(char[]) //批量读取多个字符到数组,返回读取到的字符数,如果到文件末尾返回-1
3.close() //关闭流
相关API:
1.new String(char[]) 将char[]转换成String
2.new String(char[],off,len) 将char[]的指定部分转换成String
2.FileWriter
① 构造方法
new FileWriter(File/String) //覆盖模式
new FileWriter(File/String,true) //追加模式
② 常用方法
1.write(int) //写入单个字符
2.write(char[]) //写入指定数组
3.write(char[],off,len) //写入指定数组的指定部分
4.write(String) //写入整个字符串
5.write(String,off,len) //写入字符串指定部分
6.close() //关闭流
相关API:
String类:toCharArray将String转换为char[]
注意:FileWriter使用后,必须要关闭(close)或刷新(flush),否则写入不到指定文件,还在内存中
3.BufferedReadr
BufferedReadr属于处理流(包装流)
①构造方法
new BufferedReader(FileReader) //括号内可放任意节点流
②常用方法
read() //读取单个字符
read(char[] cbuf, int off, int len) // 将字符读入数组的某一部分
readLine() //读取一个文本行
4.BufferedWriter
BufferedWriter属于处理流(包装流)
①构造方法
new BufferedWriter(FileReader) //括号内可放任意节点流
②常用方法
close() //关闭此流
flush() //刷新该流的缓冲
newLine() //写入一个行分隔符。
write(char[] cbuf, int off, int len) //写入字符数组的某一部分
write(int c) //写入单个字符
write(String s, int off, int len) //写入字符串的某一部分
五、序列化
当我们需要保存或读取的是一些值或对象(例如int类型的100、dog对象等)时,必须使用对象处理流,保存称为序列化,读取称为反序列化
如果想要让一个类可序列化,需要实现Serializable接口或Externalizable,推荐前者,因为前者没有方法需要实现,只是个标记接口,后者需要实现一些方法
注意:
①读写顺序要一致
②序列化的类中建议添加SerialVersionUID,为了提高版本兼容性
③序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员
④序列化对象时,要求里面属性的类型也需要实现序列化接口
⑤序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实现了序列化
1.ObjectOutputStream
public class ObjectOutStream_ {
public static void main(String[] args) throws IOException {
String path = "D:\\Javatest\\data.dat";
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(path));
objectOutputStream.writeInt(100);
objectOutputStream.writeBoolean(true);
objectOutputStream.writeChar('a');
objectOutputStream.writeUTF("hello");
objectOutputStream.writeObject(new Dog("小白",1));
System.out.println("序列化成功...");
}
}
class Dog implements Serializable{
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
2..ObjectInputStream
public class ObjectInputStream_ {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String path = "D:\\Javatest\\data.dat";
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(path));
System.out.println(objectInputStream.readInt());
System.out.println(objectInputStream.readBoolean());
System.out.println(objectInputStream.readChar());
System.out.println(objectInputStream.readUTF());
System.out.println(objectInputStream.readObject());
System.out.println("反序列化成功");
}
}
六、标准输入输出流
1.System.in:标准输入流
从键盘读取数据输入
2.System.out:标准输出流
将数据输出到显示器上
七、转换流
当采用不同的编码方式时,读取可能出现乱码,此时通过转换流就可以解决
1.InputStreamReader
①构造方法
//参1为字节流,参2为编码方式
InputStreamReader(InputStream in, String charsetName)
②常用方法
1.read() //读取字符
2.getEncoding() //返回此流使用的编码方式名称
3.close() //关闭此流
2.OutputStreamWriter
①构造方法
//参1为字节流,参2为编码方式
InputStreamReader(InputStream in, String charsetName)
②常用方法
1.write() //读取字符
2.getEncoding() //返回此流使用的编码方式名称
3.close() //关闭此流
4.flush() //刷新此流缓冲
八、打印流
1.PrintStream
属于字节流
①构造方法
new PrintStream(System.out); //创建新的打印流,默认输出到屏幕
new PrintStream(File file) //创建新的打印流,将内容输出到文件中
new PrintStream(String fileName) //创建新的打印流,根据路径找到文件输入到文件中
②常用方法
1.print() //打印内容
2.write() //输出内容,等价print()
3.close() //关闭此流
4.flush() //刷新此流缓冲
2.PrintWriter
属于字符流
①构造方法
new PrintWriter(System.out); //创建新的打印流,默认输出到屏幕
new PrintWriter(File file) //创建新的打印流,将内容输出到文件中
new PrintWriter(String fileName) //创建新的打印流,根据路径找到文件输入到文件中
②常用方法
1.print() //打印内容
2.write() //输出内容,等价print()
3.close() //关闭此流
4.flush() //刷新此流缓冲
九、Properties类
专门用于读写配置文件的集合类
配置文件的格式:
键=值
键=值
注意:键值对不需要有空格,值不需要用引号引起来,默认类型是String
1.读/改文件
①构造方法
直接new
②常用方法
1.load(InputStream inStream) //加载配置文件的键值对到Properties对象
2.list(PrintWriter out) //讲数据显示到指定设备
3.getProperty(key) //根据键获取值
4.setProperty(key,value) //设置键值对到Properties对象
5.store(Writer writer, String comments) //将Properties中的键值对存储到配置文件,在idea中,
保存信息到配置文件,如果含有中文,会存储为
uniccode码,参2为注释,会在文件上方生成一行注释