终于到第二阶段了,听说I/O是比较重要的部分,所以这部分打算花多点时间打基础。磨刀不误砍柴~~
1.File类
(1)是IO中唯一代表磁盘文件本身信息的类,而不是文件中的内容
(2)定义了一些与平台无关的方法来操作文件,例如创建、删除、重命名
(3)java中的目录被当做一种特殊的文件被使用,list方法可以返回目录中的所有子目录和文件
(4)在UNIX下的路径分割符为(/),在doc下的分割符为(\),java可以正确处理Unix和doc下的路径分隔符
另外还有以deleteOnExit 是推出后删除
实例: 判断某个文件是否存在,存在侧删除,不存在则创建.
import java.io.*;
public class File {
public static void main(String[] args) {
File f = new File("1.txt");
if(f.exists()){
f.delete();
}else{
try {
f.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
//文件各种信息
System.out.println("File name:"+f.getName());
System.out.println("File Path:"+f.getPath());
System.out.println("File abs Path:"+f.getAbsolutePath());
System.out.println("File Parent:"+f.getParent());
System.out.println(f.exists()?"exist":"not exist");
System.out.println(f.canRead()?"read":"not read");
System.out.println(f.isDirectory()?"directory":"not directory");
System.out.println("File last Modified:"+new Date(f.lastModified()));
}
}
2. RandomAccessFile类
提供了众多文件访问方法(操作文件),支持随机访问,在随机(相对顺序而言)读写等长记录格式的文件时有很大的优势
2中构造方法 一:只读 二:可读可写
练习:往文件中写入三名员工的信息,每个员工含有姓名和年龄两个字段,然后按照第二名,第一名,第三名的先后顺序读出员工信息。
Employee.java
p lic class Employee {
public String name =null;
public int age =0;
public static final int LEN = 8;
public Employee(String name,int age){
if(name.length()>LEN){
this.name = name.s string(0, 8);
}
else{
while(name.length()<LEN){
name+="\";
}
this.name = name;
this.age = age;
}
}
}
RandomFileTest.java
import java.io.RandomAccessFile;
public class RandomFileTest {
public static void main(String[] args) throws Exception{
Employee e1 = new Employee("zhangsan",23);
Employee e2 = new Employee("lisi",24);
Employee e3 = new Employee("wangwu",25);
RandomAccessFile ra = new RandomAccessFile("employee.txt","rw");
ra.writeChars(e1.name);
ra.writeInt(e1.age);
ra.writeChars(e2.name);
ra.writeInt(e2.age);
ra.writeChars(e3.name);
ra.writeInt(e3.age);
ra.close();
String strName = null;
raf.skipBytes(Employee.LEN*2+4);
for(int i=0;i<Employee.LEN;i++){
strName+=raf.readChar();
}
System.out.println(strName.trim()+":"+raf.readInt());
raf.seek(0);
strName="";
for(int i=0;i<Employee.LEN;i++){
strName+=raf.readChar();
}
System.out.println(strName.trim()+":"+raf.readInt());
raf.skipBytes(Employee.LEN*2+4);
strName="";
for(int i=0;i<Employee.LEN;i++){
strName+=raf.readChar();
}
System.out.println(strName.trim()+":"+raf.readInt());
raf.close();
}
}
3. 节点流:InputStream与OutputStream类:
FileInputStream指定的文件必须是存在与可读,OutputStream 不存在就创建,存在则覆盖,但不能指定一个已经被其他程序打开的文件。
创建FileInputStream实例对象时,指定的文件应当是存在和可读的。创建FileOutputStream实例对象时,如果指定的文件已经存在,这个文件中的原来内容将被覆盖清除
Java分节点流和过滤流两大类
4. Reader和Writer
package 优快云.IOtest;
import java.io.*;
public class FileSream2 {
public static void main(String[] args) throws IOException {
FileWriter fileWriter = new FileWriter("hello.txt");
fileWriter.write("www.itheima.com");
fileWriter.close();
char [] buf = new char[1024];
FileReader fileReader = new FileReader("hello.txt");
int len = fileReader.read(buf);
System.out.println(new String(buf,0,len));
fileReader.close();
}
}
5、PipedInputSteam和PipedOutputSteam类
用于在应用程序中创建管道通讯,主要用来线程之间的通讯
PipedInputStream 类与PipedOutputStream类用于在应用程序中的创建管道通信。
使用管道流类,可以实现各个程序模块之间的松耦合通信
ByteArrayInputStream 和ByteArrayOutputStream,用于以IO流的方式来完成对字节数组内容的读写,来支持类似内存虚拟文件或者内存映像文件的功能。
StringReader类和 StringWriter类来以字符IO流的方式处理字符串
这个知识点看的不是太明白,多做练习