IO流
1.File
实例化-->构造器
构造器1:(path)
-
相对路径-->相对于当前module,绝对路径
构造器2:(parentpath,childpath)
构造器3:(file,childpath)
1.1常用方法
public class FileTest {
@Test
public void test1() throws IOException {
File file1 = new File("hello.txt");//相对于当前Module
File file2 = new File("d:\\hi.txt");
System.out.println(file1.getAbsoluteFile());
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getCanonicalFile());//区别:可以抛出异常
System.out.println(file1.getName());
System.out.println(file1.getParentFile());//这里是Null
System.out.println(file1.length());
System.out.println(file1.lastModified());
}
}
创建删除
@Test
public void test2() throws IOException {
File file = new File("he.txt");
file.createNewFile();
file.mkdir();
file.mkdirs();
}
并未涉及写入等操作,需要流!
2.IO流
字节流8 bit | 字符流16 bit | |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
其他流都是这4个派生
按角色:
节点流:直接作用于文件
处理流:包在节点流外面,对节点流起作用
File-->File流-->具体操作
-
体系图:
2.1FileReader
public class FileReaderTest {
@Test
public void test1() throws IOException {
File file1 = new File("hello.txt");
FileReader fileReader = new FileReader(file1);
//read()-->返回字符,如果达到末尾,返回-1
int read = fileReader.read();
while(read != -1){
System.out.println((char) read);
read = fileReader.read();
}
//流的关闭-->必须
fileReader.close();
}
}
快递小车每次运n个char-->read(char[] cbuf)
注意遍历时候用int len不要用cbuf.length-->每次固定了5,helloworld123输出了helloworld123ld!
也可以用String
public class FileReaderTest {
@Test
public void test1() throws IOException {
FileReader fileReader = null;
try {
File file1 = new File("hello.txt");
//方式一:
fileReader = new FileReader(file1);
// //read()-->返回字符,如果达到末尾,返回-1
// int read = fileReader.read();
// while(read != -1){
// System.out.print((char)read);
// read = fileReader.read();
// }
//流的关闭-->必须
char[] cbuf = new char[5];
int len = 0;
while(len != -1){
len = fileReader.read(cbuf);
// for(int i = 0; i < len ;i++){
// System.out.println(cbuf[i]);
// }
String a = new String(cbuf,0,len);
System.out.print(a);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
fileReader.close();
}
2.2FileWriter
public class FileWriterTest {
@Test
public void test2() throws IOException {
File file2 = new File("hello.txt");
FileWriter fileWriter = new FileWriter(file2);
fileWriter.write("I have a dream");
fileWriter.close();
}
}
怎么把原来的覆盖了?-->对应的file可以不存在,若不存在,先创建再写入,不然直接覆盖
append改成true,就可以在后面加
2.3FileInputStream
public class FileInputStreamTest {
@Test
public void test3() throws IOException {
File file3 = new File("timg.jpg");
File file4 = new File("timgcopy.jpg");
FileInputStream fileInputStream = new FileInputStream(file3);
FileOutputStream fileOutputStream = new FileOutputStream(file4);
byte[] buffer = new byte[5];
int len = 0;
while((len = fileInputStream.read(buffer)) != -1){
fileOutputStream.write(buffer,0,len);
}
fileInputStream.close();
fileOutputStream.close();
}
}
2.4缓冲流
包在节点流外面,提供缓冲区,加快速度
2.5转换流
字节流(如果是文本)-->FileInputStreamReader-->字符流
2.6输入输出流,打印流,数据流
2.7对象流
把对象写入数据源中进行传输-->序列化:ObjectOutputStream(对象冷冻);反序列化:ObjectInuputStream(对象还原)
-
对象的序列化
对象-->二进制流-->实现Serializable接口的,RMI的基础
-
文本读写
@Test
public void ObjectOutTest() throws IOException, ClassNotFoundException {
File file5 = new File("object.txt");
FileOutputStream fos = new FileOutputStream(file5);
FileInputStream fis = new FileInputStream(file5);
ObjectOutputStream oos = new ObjectOutputStream(fos);
ObjectInputStream ois = new ObjectInputStream(fis);
oos.writeObject(new String("woaibeijing"));
String o = (String) ois.readObject();
System.out.println(o);
oos.flush();;
oos.close();
ois.close();
fos.close();
fis.close();
}
-
对实体类进行序列化
实体类需满足:
1.实现Serializable
2.serialVersionUID
3.其内部所有属性可序列化
public class Person implements Serializable {
public static final long serialVersionUID = 12739123172L;
oos.writeObject(new Person("luyao",20));
oos.flush();
Object o2 = ois.readObject();
System.out.println(o2);
2.8随机存取文件流
任意,可读可写
3.NIO2
JDK7之后的升级IO
变成面向缓冲区的IO
Paths工具类,Path-->这些代替File