上一章节:JavaSE多线程(JavaSE大全)
I/O流介绍
将网络或磁盘中的数据读取到程序中,称为输入流。
将内存中的数据写入磁盘中,称为输出流。
文件读写的操作称为I/O操作,java提供了针对不同情况的处理流的类,这些操作类在java的IO包之中。
Java中“流”表示任何有能力产生数据源或有能力接收数据源的对象。
流的分类:
(1)输入流:程序可以从磁盘或网络中读取数据的流
(2)输出流:程序能向其中写入磁盘数据的流
按数据传输单位分:
(1)字节流:以字节为单位传输数据的流
(2)字符流:以字符为单位传输数据的流
Java中I/O流类型结构:
文件相关操作
文件是一系列数据的集合。
Java.io.File类是专门对文件进行操作的类,具体操作如:创建,删除文件和重命名等。(只能对文件本身进行操作,不能对文件内容操作)
在操作一个文件前需要先创建一个File对象。
File file=new File(String path);
访问File对象的属性:
(1)public boolean canRead() //检查应用程序是否可以读取文件的抽象路径名记。
(2)public boolean canWrite() //检查应用程序是否可以修改文件的抽象路径名记
(3)public boolean exists() //检查文件或目录是否存在这种抽象路径名记。
(4)public boolean isDirectory() //测试文件是否通过这种抽象路径名表示的是一种正常的文件。
(5)public boolean isFile() //测试文件是否通过这种抽象路径名表示是一个目录。
(6)public boolean isHidden() //测试文件是否通过这种抽象路径名的命名是隐藏文件。
(7)public long lastModified() //毫秒值
(8)public long length() //以字节为单位
(9)public String getName() //获取文件名
(10)public String getPath() //路径名
(11)public String getAbsolutePath() //返回此File对象的绝对路径名
(12)public File getAbsoluteFile() //返回此抽象路径名的绝对形式。
(13)public String getCanonicalPath() //返回此File对象的规范路径名字符串
(14)public File getCanonicalFile() //返回此File对象的规范形式
(15)public String getParent() //返回父目彔的路径名字符串
public static void main(String[] args) throws IOException {
//创建文件
File file=new File("E:\\file\\aa.txt");
file.createNewFile();
//创建文件夹
File file1=new File("E:\\file\\aa");
file1.mkdir();
File file2=new File("E\\file");
//判断文件是否存在
System.out.println(file.exists());
//判断是否为文件
System.out.println(file.isFile());
//判断是否为目录
System.out.println(file.isDirectory());
//获取文件列表
System.out.println(Arrays.toString(file2.list()));
//获取文件绝对路径
System.out.println(file.getAbsolutePath());
//文件删除
File file3=new File("E\\file\\a2.txt");
System.out.println(file.delete());
//重命名
file.renameTo(new File("D:\\file\\aaaa.txt"));
}
}
字节输入流
常用实现类:
(1)字节输入流:FileInputStream
(2)字节输出流:FileOutputStream
(3)字符输入流:FileReader
(4)字符输出流:FileWriter
(5)缓冲字符输入流:BufferedReader
(6)缓冲字符输出流:BufferedWriter
(7)打印流:PrintWriter—数据的显示和打印
(8)对象输入流:ObjectOutputStream–将本地文件中的对象读出来
(9)对象输出流:ObjectOutputStream–将对象信息写入到本地文件
public static void main(String[] args) {
File file=new File("E:\\file\\1.txt");
FileInputStream fis=null;
try {
fis=new FileInputStream(file);
//获取文件长度
System.out.println(fis.available());
//读取文件并返回读取到的位置,读取完毕,返回-1
System.out.println(fis.read());
//准备一个byte数组存放读取到的文件
byte[] b=new byte[1024];
//读取一部分内容放进数组
int len=fis.read(b);
while(len!=-1) {
//将byte类型数组转换成字符串
String data=new String(b);
System.out.println(data);
len=fis.read(b);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭文件资源
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
字节输出流
//字节输出流 FileOutputStream
public static void main(String[] args) {
String s="你啊好啊,🐖";
FileOutputStream fops=null;
try {
//true表示追加写,不加默认覆盖写入
fops=new FileOutputStream("E:\\file\\hello.txt",true);
//将要写入的字符串转换成字节数组
byte[] b=s.getBytes();
//写入文件
fops.write(b, 0, b.length);
//清空缓冲区数据,并强制写入
fops.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fops.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
字符输入流
//字符输入流 filereader
public static void main(String[] args) {
File file=new File("E:\\file\\1.txt");
FileReader fr=null;
try {
fr=new FileReader(file);
//使用字符数组保存读取内容
char[] c=new char[1024];
//将读取的内容存放到字符数组中
int len=fr.read(c);
while(len!=-1) {
String s=new String(c);
len=fr.read();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
缓冲流
//缓冲字符输入流 Buffer
public static void main(String[] args) {
File file=new File("E:\\file\\1.txt");
BufferedReader br=null;
BufferedWriter bw=null;
try {
br=new BufferedReader(new FileReader(file));
bw=new BufferedWriter(new FileWriter("E:\\file\\aa\\1.txt"));
//逐行读取字符串
String str=br.readLine();
while(str!=null) {
System.out.println(str);
bw.write(str+"\r\n");
bw.flush();
str=br.readLine();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bw.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
打印流
public static void main(String[] args) {
//打印到控制台
String s="hhhhh";
PrintWriter pw=new PrintWriter(System.out);
pw.print(s);
pw.close();
//打印到文件
PrintWriter pw1=null;
try {
pw1=new PrintWriter(new FileWriter("E:\\file\\5.txt",true));
pw1.print("hhhhh");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
pw1.close();
}
}
对象输出流:
对象输入流:
public class Student implements Comparable<Student>,Serializable{
private static final long seriaVersionUID=1L;
public String name;
public int age;
public String hobby;
public Student(String name, int age, String hobby) {
this.name = name;
this.age = age;
this.hobby = hobby;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
return 0;
}
}
public static void main(String[] args) {
Student st=new Student("zz",18, "xx");
Student st1=new Student("zzx",19, "xxx");
Student st2=new Student("zzz",20, "xxz");
ArrayList<Student> al=new ArrayList<Student>();
al.add(st);
al.add(st1);
al.add(st2);
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new FileOutputStream("E:\\file\\123.txt"));
//写入对象集合
oos.writeObject(al);
oos.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
ObjectInputStream ois=null;
try {
ois=new ObjectInputStream(new FileInputStream("E:\\file\\123.txt"));
ArrayList<Student> stuArry=(ArrayList<Student>) ois.readObject();
for (Student stu : stuArry) {
System.out.println("姓名:"+stu.name+"年龄:"+stu.age+"爱好:"+stu.hobby);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}