1. RandomAccessFile
能读能写,可以获取文件指针的位置,能够定位指针位置
public class FileDemo{
public static void main(String[] args) throws IOException {
//write();
RandomAccessFile rw = new RandomAccessFile("zqq2.txt", "rw");
rw.writeInt(8);
rw.writeInt(8);
rw.writeBoolean(false);
rw.close();
read1();
}
private static void write() throws IOException {
RandomAccessFile rw = new RandomAccessFile("zqq2.txt", "rw");
rw.writeInt(8);
rw.writeInt(8);
rw.writeBoolean(false);
rw.close();
}
private static void read1() throws IOException {
RandomAccessFile e = new RandomAccessFile("zqq.txt", "rw");
int i = e.readInt();
long filePointer = e.getFilePointer();
System.out.println("指针位置"+filePointer);
int i1 = e.readInt();
long filePointer1 = e.getFilePointer();
System.out.println("指针位置"+filePointer1);
boolean b = e.readBoolean();
long filePointer2 = e.getFilePointer();
System.out.println("指针位置"+filePointer2);
e.seek(0);//定位指针的位置
int and= e.readInt();
System.out.println(and);
e.seek(8);
boolean b1 = e.readBoolean();
System.out.println(b1);
e.close();
}
}
输出结果:
指针位置4
指针位置8
指针位置9
8
false
2
public class IODemo2 {
//作业:断点复制
public static void main(String[] args) throws IOException {
RandomAccessFile r = new RandomAccessFile("yinyue.txt", "rw");
RandomAccessFile w = new RandomAccessFile("yinyue2.txt", "rw");
File file = new File("yinyue2.txt");
if (file.exists()) {
long length = file.length();
//定位字节位置
r.seek(length);
w.seek(length);
} else {
r.seek(0);
w.seek(0);
}
copyFile(r,w);
}
private static void copyFile(RandomAccessFile r, RandomAccessFile w) throws IOException {
int len = 0;
byte[] bytes = new byte[100];
while ((len = r.read(bytes)) != -1) {
long pointer = r.getFilePointer();//获取文件指针位置
System.out.println(pointer);
w.write(bytes, 0, len);
}
r.close();
w.close();
}
}
3.序列化流
public class IODemo7 {
public static void main(String
[] args
) throws IOException
, ClassNotFoundException
{
readObj();
}
private static void readObj() throws IOException
, ClassNotFoundException
{
ObjectInputStream in
= new ObjectInputStream(new FileInputStream("obj.txt"));
Object object
= in
.readObject();
ArrayList
<Student> list
= (ArrayList
<Student>) object
;
Student student
= list
.get(list
.size() - 1);
System
.out
.println(student
.getName());
System
.out
.println(student
.getAge());
}
private static void writeObj() throws IOException
{
Student student
= new Student("张三", 23);
Student student2
= new Student("张三2", 23);
Student student3
= new Student("张三3", 23);
Student student4
= new Student("张三4", 23);
ArrayList
<Student> list
= new ArrayList<>();
list
.add(student
);
list
.add(student2
);
list
.add(student3
);
list
.add(student4
);
ObjectOutputStream obj
= new ObjectOutputStream(new FileOutputStream("obj.txt"));
obj
.writeObject(list
);
}
}
4.
public class IODemo {
public static void main(String
[] args
) {
Properties properties
= new Properties();
properties
.setProperty("key","value");
String property
= properties
.getProperty("key","abc");
String property1
= properties
.getProperty("key2", "abc");
System
.out
.println(property
);
System
.out
.println(property1
);
}
}
输出结果:
value
abc
5.
public class IODemo4 {
public static void main(String
[] args
) throws IOException
{
Properties properties
= new Properties();
properties
.setProperty("zhangsan","123456");
properties
.setProperty("zhangsan2", "123456");
properties
.setProperty("zhangsan3", "123456");
properties
.setProperty("zhangsan4", "123456");
properties
.setProperty("赵六", "123456");
properties
.store(new FileOutputStream("student.properties"),null
);
}
}
实验结果:
student
.properties文件中的内容:
\u8D75\u516D
=123456
zhangsan
=123456
zhangsan4
=123456
zhangsan3
=123456
zhangsan2
=123456