Java基础
IO流
- File类
创建对象时,不能创建文件夹和文件,只是对文件的一个描述。
package com.java.day11;
import java.io.File;
import java.io.IOException;
public class Demo5 {
//1.创建文件对象的三种方式
public static void test1() {
File file1 = new File("E:\\a\\b\\a.txt");
File file2 = new File("E:\\a\\b","a.txt");
File path = new File("E:\\a\\b");
File file3 = new File(path,"a.txt");
System.out.println(file1+","+file2+","+file3);
}
//2.文件目录(文件夹)的创建与删除
public static void test2() {
File file = new File("E:\\a\\b");
if(!file.exists()) { // 判断文件目录有没有
//boolean b = file.mkdir(); // 创建一个文件目录
boolean b = file.mkdirs(); // 创建多个文件目录
System.out.println("创建"+b);
}else {
boolean b = file.delete();//删除一个文件目录
System.out.println("删除"+b);
}
}
//3.文件创建
public static void test3() throws IOException {
File file = new File("a.txt");//不写目录表示文件在当前工程文件夹
if(!file.exists()) {
boolean b = file.createNewFile();//创建文件
System.out.println(b);
}
}
//4.常用方法
public static void test4() {
File file = new File("a.txt");
System.out.println(file.isFile());
System.out.println(file.isDirectory());
System.out.println(file.getName());
System.out.println(file.getParent());
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());//绝对路径
System.out.println(file.length());
}
public static void main(String[] args) throws IOException {
//test1();
//test2();
//test3();
test4();
}
}
- IO流简介
2.1
I-------Input
O-------Output
2.2
按照流的读写方式:字节流和字符流
按照流的方向:输入流和输出流
InputStream abstract
Reader abstract
OutputStream abstract
Writer abstract
2.3
输入流的核心方法:read()
输出流的核心方法:write()
2.4 操作的数组类型
字节流:byte类型
字符流:char类型
2.5 图解
- IO流之字节流
3.1 FileOutputStream和FileInputStream
package com.java.day12;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 1.-选择一个流类(输入流或输出流)
* 2.-选择方法
* 3.-关闭流
* @author ThinPad
*
*/
public class Demo1 {
//1.文件的写操作a.txt 字节
public static void test1() throws IOException {
//输出流,如果数据文件存在,那么使用此数据文件
//如果数据文件不存在,自动创建一个文件,在当前工程文件夹下
FileOutputStream out = new FileOutputStream("a.txt");
out.write("学习".getBytes());
out.close();
}
//2.文件的读操作a.txt 字节
public static void test2() throws IOException {
//判断文件是否存在
File file = new File("a.txt");
//如果文件不存在
if(!file.exists()) {
//创建文件
file.createNewFile();
}
//创建文件输入流对象
FileInputStream in = new FileInputStream(file);
//定义数组
byte[] buf = new byte[(int)file.length()];
//把数据读到数组
in.read(buf);
//把数组转换成字符串并打印
System.out.println(new String(buf));
//关闭流
in.close();
}
//3.读的第二种方式
public static void test3() throws IOException {
//判断文件是否存在
File file = new File("a.txt");
//如果文件不存在
if(!file.exists()) {
//创建文件
file.createNewFile();
}
//创建文件输入流对象
FileInputStream in = new FileInputStream(file);
int n = 0;
//打印字符
//while((n = in.read()) != -1) {//如果read返回-1,表示文件数据读取结束
// System.out.print((char)n);
//}
//打印中文
byte[] b = new byte[2];//gbk是两个字节,utf-8是三个字节
while((n = in.read(b)) != -1){
System.out.print(new String(b));
}
//关闭流
in.close();
}
//4.追加文本内容
public static void test4() throws IOException {
//true表示追加文本内容到文本末尾
FileOutputStream out = new FileOutputStream("a.txt", true);
//\n表示换行
out.write("hello\n".getBytes());
out.close();
}
public static void main(String[] args) throws IOException {
//test1();
//test2();
//test3();
test4();
}
}
3.2 对象的输入输出流
对象的序列化:把对象转换成二进制的流,写到数据文件。
对象的反序列化:把数据文件中二进制的流代表的数据恢复为对象。
按照是否能直接操作数据文件分为:
节点流:直接在构造方法中传入要读写的文件对象或文件名。
处理流:不能直接在构造方法中传入要读写的文件对象或文件名。
代码:
package com.java.day12_2;
import java.io.Serializable;
//Serializable 标识性接口,表示创建的类为序列化的类
//创建的对象就是序列化对象
public class Student implements Serializable{
/**
* serialVersionUID 序列化版本号
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
package com.java.day12_2;
import java.io.File;
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 Demo1 {
//1.写对象到数据文件oos.txt
public static void test1() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt", true));
Student stu1 = new Student("admin", 18);
Student stu2 = new Student("zhou", 18);
oos.writeObject(stu1);
oos.writeObject(stu2);
oos.close();
}
//2.读取对象
public static void test2() throws IOException, ClassNotFoundException {
File file = new File("oos.txt");
if(!file.exists()) {
file.createNewFile();
}
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
Student stu1 = (Student)ois.readObject();
Student stu2 = (Student)ois.readObject();
System.out.println(stu1);
System.out.println(stu2);
ois.close();
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
//test1();
test2();
}
}
- IO流之字符流
4.1 FileReader和FileWriter
package com.java.day12_2;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo2 {
//写
public static void test1() throws IOException {
FileWriter fw = new FileWriter("fw.txt");
fw.write("学习hello");
fw.close();
}
//读
public static void test2() throws IOException {
File file = new File("fw.txt");
if(!file.exists()) {
file.createNewFile();
}
FileReader fr = new FileReader(file);
char[] buf = new char[(int)file.length()];
fr.read(buf);
System.out.println(new String(buf));
fr.close();
}
public static void main(String[] args) throws IOException {
//test1();
test2();
}
}
4.2 缓冲流
有缓冲池,提高读写效率
BufferdReader和BufferdWriter
readLine();//成行读取,返回一行字符串
PrintWriter---------println();//BufferdWriter换行只能\n,所以用PrintWriter更方便
package com.java.day12_2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class Demo4 {
//写
public static void test1() throws FileNotFoundException {
PrintWriter pw = new PrintWriter("pw.txt");
pw.println("周 18 15513888648");
pw.println("张 19 15513998668");
pw.close();
}
//读
public static void test2() throws IOException {
File file = new File("pw.txt");
if(file.exists()) {
file.createNewFile();
}
BufferedReader br = new BufferedReader(new FileReader(file));
String str = "";
while((str=br.readLine()) != null) {
String[] value = str.split("\\s");
String name = value[0];
String age = value[1];
String tel = value[2];
System.out.println(name+","+tel);
}
br.close();
}
public static void main(String[] args) throws IOException {
//test1();
test2();
}
}
- 转换流
InputStreamReader和OutputStreamWriter
把字节流转成字符流
//转换流
public static void test3() throws IOException {
InputStream ins = new FileInputStream("ins.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
}
- 特殊的流
既可以做输入流,又可以做输出流。
RandomAccessFile
参数mode:r--------输入流 、 rw--------输出流