File类的使用
1.File类的一个对象,代表一个文件或一个文件目录(文件夹)。
2.File类声明在java.io包下。
3.File类中涉及到关于文件和文件目录的创建、删除、重命名、修改时间、文件大小等方法,并未涉及到写入或读取文件内容的操作,如果要读取或写入文件内容,必须使用IO流来完成。
File类的实例化
2.1常用的构造器:
- public File(String pathname)
以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果 pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。 - public File(String parent,String child)
以parent为父路径,child为子路径创建File对象。 - public File(File parent,String child)
根据一个父File对象和子文件路径创建File对象。
示例代码:
@Test
public void test(){
//构造器1:
File file1 = new File("hello.txt" );//相对路径,在idea中相对于当前module
//绝对路径
File file2 = new File("E:\\ideaCode\\JavaSenior\\day26\\hei.txt");
System.out.println(file1);
System.out.println(file2);
//构造器2:
File file3 = new File("E:\\ideaCode","JavaSenior");
System.out.println(file3);
//构造器3
File file4 = new File(file3,"hhh.txt");
System.out.println(file4);
}
2.2 路径的分类:
相对路径:相较于某个路径下,指明的路径。
绝对路径:包含盘符在内的文件或文件目录的路径。
在idea中
如果使用JUit中的单元测试方法测试,相对路径为当前Module下。
如果使用main方法进行测试,相对路径为当前的Project(工程)下。
在Eclipse中
因为可以有多个工程,所以不论是使用单元测试还是使用main方法进行测试,始终都是相对于工程下的,各个工程之间不会相互影响。
2.3 路径分隔符:
- 路径中的每级目录之间用一个路径分隔符隔开。
- 路径分隔符和系统有关:
windows和DOS系统默认使用“”来表示
UNIX和URL使用“/”来表示 - Java程序支持跨平台运行,因此路径分隔符要慎用。
为了解决这个隐患,File类提供了一个常量:
public static final String separator。根据操作系统,动态的提供分隔符。File类的常用方法:
3.1 File类的获取功能
public String getAbsolutePath():获取绝对路径
public String getPath() :获取路径
public String getName() :获取名称
public String getParent():获取上层文件目录路径。若无,则返回null。
public long length() :获取文件长度(即:字节数)。不能获取文件目录的长度。
public long lastModified() :获取最后一次的修改时间,毫秒值。
如下的两个方法适用于文件目录,如果文件目录不存在会报异常
public String[] list() :获取指定目录下的所有文件或者文件目录的名称数组。
public File[] listFiles() :获取指定目录下的所有文件或者文件目录的File数组。
3.2 File类的重命名功能
public boolean renameTo(File dest):把文件重命名为指定的文件路径。
3.3 File类的判断功能
public boolean isDirectory():判断是否是文件目录
public boolean isFile() :判断是否是文件
public boolean exists() :判断是否存在
public boolean canRead() :判断是否可读
public boolean canWrite() :判断是否可写
public boolean isHidden() :判断是否隐藏
3.4 File类的创建功能
public boolean createNewFile() :创建文件。若文件存在,则不创建,返回false
public boolean mkdir() :创建文件目录。如果此文件目录存在,就不创建了。 如果此文件目录的上层目录不存在,也不创建。
public boolean mkdirs() :创建文件目录。如果上层文件目录不存在,一并创建。
3.5 File类的删除功能
public boolean delete():删除文件或者文件目录 。
删除文件时的注意事项:
1. Java中删除的文件不会存放在回收站。
2. 要删除一个文件目录,该文件目录内不能包含文件或者文件目录。
示例代码如下:
@Test
public void test2(){
File file1 = new File("haha.txt");
File file2 = new File("E:\\io\\hi.txt");
System.out.println(file1.getAbsoluteFile());
System.out.println(file1.getPath());
System.out.println(file1.getName());
System.out.println(file1.getParent());
System.out.println(file1.length());
System.out.println(new Date(file1.lastModified()));
System.out.println(file2.getAbsoluteFile());
System.out.println(file2.getPath());
System.out.println(file2.getName());
System.out.println(file2.getParent());
System.out.println(file2.length());
System.out.println(file2.lastModified());
}
@Test
public void test3(){
File file = new File("E:\\ideaCode\\JavaSenior");
String[] list = file.list();
for (String str :list){
System.out.println(str);
}
File[] files = file.listFiles();
for(File fi :files){
System.out.println(fi);
}
}
@Test
public void test4(){
File file1 =new File("haha.txt");
File file2 =new File("E:\\io\\hi.txt");
boolean b = file1.renameTo(file2);
System.out.println(b);
}
@Test
public void test5(){
File file1 =new File("haha.txt");
System.out.println(file1.isDirectory());
System.out.println(file1.isFile());
System.out.println(file1.exists());
System.out.println(file1.canRead());
System.out.println(file1.canWrite());
System.out.println(file1.isHidden());
File file2 =new File("E:\\io");
System.out.println(file2.isDirectory());
System.out.println(file2.isFile());
System.out.println(file2.exists());
System.out.println(file2.canRead());
System.out.println(file2.canWrite());
System.out.println(file2.isHidden());
}
@Test
public void test6() throws IOException {
File file1 = new File("hi.txt");
if (!file1.exists()){
//文件的创建
file1.createNewFile();
System.out.println("创建成功!");
}else {
//文件的删除
file1.delete();
System.out.println("删除成功!");
}
}
@Test
public void test7() throws IOException {
//文件目录的创建
File file1 = new File("E:\\io\\io1\\io3");
boolean mkdir = file1.mkdir();
if (mkdir){
System.out.println("创建成功11!");
}
File file2 = new File("E:\\io\\io2\\io4");
boolean mkdir2 = file2.mkdirs();
if (mkdir2){
System.out.println("创建成功22!");
}
}
IO流概述:
I/O是Input/Output的缩写,用于处理设备之间的数据传输。如读/写文件,网洛通讯等。
流是用来处理数据的。
处理数据时,一定要先明确数据源,与数据目的地。
数据源可以是文件,可以是键盘。
数据目的地可以是文件、显示器或者其他设备。
流只是在帮助数据进行传输,并对传输的数据进行处理,比如过滤处理、 转换处理等。
1.流的分类:
1.按照操作数据单位的不同分为:字节流(8bit),字符流(16bit)。
2.按照数据流的流向的不同分为:输入流,输出流。
3.按照流的角色的不同分为:节点流,处理流。
2.输入、输出的标准化过程
2.1输入过程
①创建File类的对象,指明读取的数据的来源。(要求此文件一定要存在)。
②创建相应的输入流,将File类的对象作为参数,传入流的构造器中。
③具体的读入过程:
创建相应的byte[]或char[]
④关闭流资源。
说明,程序中出现的异常需要使用try-catch-finally处理(idea快捷键为shift+alt+z)
2.2输出过程
①创建File类的对象,指明写出的数据的来源。(并不要求此文件一定要存在)。
②创建相应的输出流,将File类的对象作为参数,传入流的构造器中。
③具体的写出过程:
write(char[]/byte[] buffer,0,len)
④关闭流资源。
FileReader / FileWriter的使用:
1. FileReader的使用
1.read()的理解:返回读入的一个字符,如果达到文件的末尾,返回-1。
2.异常的处理:为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally处理。
3.读入的文件一定要存在,否则就会报异常java.io.FileNotFoundException。
示例代码如下:
练习:将haha.txt文件内容读入程序中,并输出到控制台。
@Test
public void test2() {
FileReader fr = null;
try {
//1.File类的实例化
File file =new File("haha.txt");
//2.FileReader流的实例化
fr = new FileReader(file);
//3.读入的操作
//read(char[] cbuf):返回每次读入cbuf数组中的字符的个数,如果达到文件末尾,返回-1。
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) !=-1 ){
//方式一:
//错误的写法
// for (int i = 0;i < cbuf.length;i++){
// System.out.print(cbuf[i]);
// }
//正确的写法:
// for (int i = 0;i <len;i++){
// System.out.print(cbuf[i]);
// }
//方式二:
//错误的写法,对应着方式一的错误的写法
// String str = new String (cbuf);
// System.out.println(str);
//正确的写法
String str = new String (cbuf,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.流资源的关闭操作
try {
if (fr != null){
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. FileWriter的使用
从内存中写出数据到硬盘的文件里。
说明:
- 输出操作对应的File是可以不存在的,并不会报异常。
- File对应的硬盘中的文件如果不存在,再输出的过程中,会自动创建此文件。
File对应的硬盘中的文件如果存在:
如果流使用的构造器是:FileWriter(file,false)或者FileWriter(file):对原有文件的覆盖。
如果流使用的构造器是:FileWriter(file,true):不会对原有文件的覆盖,而是在原有文件的基础上追加内容。3. 文本文件的复制
示例代码:
@Test public void test4() { FileReader fr = null; FileWriter fw = null; try { //1.创建File的对象,指明读入和写出的文件 File file1 = new File("haha.txt"); File file2 = new File("haha2.txt"); //不能使用字符流来处理图片等字节数据 // File file1 = new File("1.jpg"); // File file2 = new File("2.jpg"); //2.创建输入流和输出流对象 fr = new FileReader(file1); fw = new FileWriter(file2); //3.数据的读入和写出操作 char[] cbuf =new char[5]; int len; //记录每次读入到cbuf数组中的字符的个数 while ((len = fr.read(cbuf)) != -1){ //每次写出len个字符 fw.write(cbuf,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { //4.关闭流资源 try { if (fw != null ) { fw.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (fr != null ) { fr.close(); } } catch (IOException e) { e.printStackTrace(); } } }
FileInputStream / FileOutputStream的使用
对于文本文件(.txt,.java,.c,.cpp等),使用字符流处理。
如果使用字节流来处理,在控制台查看结果时可能会出现中文乱码。
如果只是想把文件复制,不在控制台查看文件的内容,也可以使用字节流来处理。
对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt等),使用字节流处理。
示例代码如下:
//使用字节流FileInputStream处理非文本文件
//实现对图片的复制
@Test
public void test2() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//1.
File file1 = new File("1.jpg");
File file2 = new File("3.jpg");
//2.
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
//3.
byte[] buff = new byte[5];
int len;
while ((len = fis.read(buff)) != -1){
fos.write(buff,0,len);
}
System.out.println("复制成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.
try {
if (fos != null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
缓冲流
1.缓冲流涉及到的类:
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
2.作用
提升流的读取、写入的速度。
原因:内部提供了一个缓冲区,默认情况下是8192,即8kb。
3使用BufferedInputStream和BufferedOutputStream:处理非文本文件
//实现文件复制的方法
public void copyFileWithBuffered(String srcPath,String destPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.
File srcfile = new File(srcPath);
File destfile = new File(destPath);
//2.
//2.1造节点流
fis = new FileInputStream(srcfile);
fos = new FileOutputStream(destfile);
//2.2造缓冲流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1){
bos.write(buffer,0,len);
// bos.flush();//刷新缓冲区
}
System.out.println("复制成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.
//要求:先关闭外层的流,再关闭内层的流
if (bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//说明:关闭外层流的同时,内层流也会自动的进行关闭,关于内层流的关闭,我们可以省略
// fos.close();
// fis.close();
}
对上述方法进行测试:
@Test
public void testCopyBuffererd(){
long start = System.currentTimeMillis();
String srcPath = "1.jpg";
String destPath = "6.jpg";
copyFileWithBuffered(srcPath,destPath);
long end = System.currentTimeMillis();
System.out.println("复制所需要的毫秒数为:"+ (end - start));
}
4.使用BufferedReader和BufferedWriter:处理文本文件
//使用BufferedReader和BufferedWriter实现文本文件的复制
@Test
public void test6(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
//使用匿名方式,创建文件和相应的流
br = new BufferedReader(new FileReader(new File("dbcp.txt")));
bw = new BufferedWriter(new FileWriter(new File("dbcp1.txt")));
//读写操作
//方式一:使用char[]数组
// char[] cbuf = new char[1024];
// int len;
// while ((len = br.read(cbuf)) != -1){
// bw.write(cbuf,0,len);
// bw.flush();//刷新操作
// }
//方式二:使用String
String data;
while ((data = br.readLine()) != null){
bw.write(data);
bw.newLine();//提供换行的操作
}
System.out.println("复制成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
try {
if (bw != null){
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
转换流
1.涉及到的类:
InputStreamReader:将InputStream转换为Reader
OutputStreamWriter:将Writer转换为OutputStream
2.构造器
public OutputStreamWriter(OutputStream out)
public OutputSreamWriter(OutputStream out,String charsetName)
3.作用:
提供字节流和字符流之间的转换
4.编码表:
常见的编码表:
ASCII:美国标准信息交换码。用一个字节的7位可以表示。
ISO8859-1:拉丁码表。欧洲码表 。用一个字节的8位表示。
GB2312:中国的中文编码表。最多两个字节编码所有字符。
GBK:中国的中文编码表升级,融合了更多的中文文字符号。最多两个字节编码。
Unicode:国际标准码,融合了目前人类使用的所有字符。为每个字符分配唯一的字符码。所有的文字都用两个字节来表示。
UTF-8:变长的编码方式,可用1-4个字节来表示一个字符。
示例:
@Test
public void test2(){
//1.造文件
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
File file1 = new File("dbcp.txt");
File file2 = new File("dbcp_gbk.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
isr = new InputStreamReader(fis,"utf-8");
osw = new OutputStreamWriter(fos,"gbk");
//2.读写过程
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1){
osw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//3.关闭资源
try {
if (isr != null){
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (osw != null){
osw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
对象流:
1.涉及到的类:
ObjectInputStream 和 ObjectOutputStream
用于存储和读取基本数据类型数据或对象的处理流。
2.作用:
ObjectOutputStream:内存中的对象 ----->存储中的文件、通过网络传输出去 序列化过程
ObjectInputStream:存储中的文件、通过网络接收过来 ---->内存中的对象 反序列化过程
用于存储和读取基本数据类型数据或对象的处理流。可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。
3.对象的序列化机制:
对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象。
示例代码如下:
/*
序列化过程:将内存中的java对象保存到磁盘中或通过网络传输出去
使用ObjectOutputStream实现
*/
@Test
public void test(){
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
oos.writeObject(new String("你真是太帅了"));
oos.flush();//刷新操作
oos.writeObject(new Person("小子",21));
oos.flush();
oos.writeObject(new Person("小妞",20,1001,new Account(500)));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos !=null){
oos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
反序列化:将磁盘文件中的对象还原为内存中的一个java对象
使用ObjectInputStream来实现
*/
@Test
public void test2(){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("object.dat"));
Object obj = ois.readObject();
String str = (String) obj;
Person p = (Person) ois.readObject();
Person p1 = (Person) ois.readObject();
System.out.println(str);
System.out.println(p);
System.out.println(p1);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (ois !=null){
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.实现序列化的类需要满足:
实现序列化时Javabean需要满足如下的要求,方可序列化:
- 需要实现接口:Serializable
- 当前类提供一个全局常量:serialVersionUID
- 除了当前Person类需要实现Serializable接口之外,还必须保证其内部的所有属性也必须是可序列化的。(默认情况下,基本数据类型可序列化)
- ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量。
示例代码如下:
public class Person implements Serializable {
public static final long serialVersionUID = 457987412388296L;
private String name;
private int age;
private int id;
private Account acct;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name, int age, int id) {
this.name = name;
this.age = age;
this.id = id;
}
public Person(String name,int age,int id,Account acct){
this.name = name;
this.age = age;
this.id = id;
this.acct = acct;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Account getAcct() {
return acct;
}
public void setAcct(Account acct) {
this.acct = acct;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", id=" + id +
", acct=" + acct +
'}';
}
}
class Account implements Serializable{
public static final long serialVersionUID = 48446849848932L;
private double balance;
@Override
public String toString() {
return "Account{" +
"balance=" + balance +
'}';
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Account(double balance) {
this.balance = balance;
}
}
标准的输入输出流:
System .in:标准的输入流,默认从键盘输入。
System.out:标准的输出流,默认从控制台输出。
修改默认的输入和输出行为:
System类的setIn(InputStream is) / setOut(PrintStream ps)方式重新指定输入和输出的流。
练习:
题目描述:从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。
方法一:使用Scanner实现。
方法二:使用System.in实现。
示例代码:
public static void main(String[] args) {
BufferedReader br = null;
try {
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
while (true){
System.out.println("请输入字符串:");
String data =br.readLine() ;
if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)){
System.out.println("程序结束");
break;
}
String s = data.toUpperCase();
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
打印流:
实现将基本数据类型的数据格式转化为字符串输出。
打印流:PrintStream 和PrintWriter
说明:
1.提供了一系列重载的print()和println()方法,用于多种数据类型的输出。
2.PrintStream和PrintWriter的输出不会抛出IOException异常。
3.PrintStream和PrintWriter有自动flush功能。
4.PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节。 在需要写入字符而不是写入字节的情况下,应该使用 PrintWriter 类。
5.System.out返回的是PrintStream的实例。
示例代码:
@Test
public void test2() {
PrintStream ps = null;
try {
FileOutputStream fos = new FileOutputStream(new File("E:\\io\\text.txt"));
// 创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
ps = new PrintStream(fos, true);
if (ps != null) {// 把标准输出流(控制台输出)改成文件
System.setOut(ps);
}
for (int i = 0; i <= 255; i++) { // 输出ASCII字符
System.out.print((char) i);
if (i % 50 == 0) { // 每50个数据一行
System.out.println(); // 换行
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (ps != null) {
ps.close();
}
}
}
数据流:
DataInputStream 和 DataOutputStream
作用:
用于读取或写出基本数据类型的变量或字符串。
示例代码:
@Test
public void test3() {
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeUTF("哈哈哈哈");
dos.flush();//刷新操作,将内存中的数据写入文件
dos.writeInt(21);
dos.flush();
dos.writeBoolean(true);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dos != null){
dos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中。
读取不同类型的数据的顺序要和当初写入文件时保存数据的顺序一致,如果顺序不一致会报异常。
*/
@Test
public void test4() {
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("data.txt"));
String name = dis.readUTF();
int age = dis.readInt();
boolean isMale = dis.readBoolean();
System.out.println("name = " + name );
System.out.println("age = " + age );
System.out.println("isMale = " + isMale );
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis !=null){
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
随机存取文件流:
RandomAccessFile
RandomAccessFile 声明在java.io包下,但直接继承于java.lang.Object类。并且它实现了DataInput、DataOutput这两个接口,也就意味着这个类既可以读也可以写。
RandomAccessFile 类支持 “随机访问” 的方式,程序可以直接跳到文件的任意地方来读、写文件。
支持只访问文件的部分内容。
可以向已存在的文件后追加内容。
RandomAccessFile 对象包含一个记录指针,用以标示当前读写处的位置。
RandomAccessFile 类对象可以自由移动记录指针:
long getFilePointer():获取文件记录指针的当前位置
void seek(long pos):将文件记录指针定位到 pos 位置
2.使用说明:
- RandomAccessFile直接继承于java.lang.Object类,实现了DataInput和DataOutput接口。
- RandomAccessFile既可以作为一个输入流,又可以作为一个输出流。
- 如果RandomAccessFile作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建。如果写出到的文件存在,则会对原有文件内容进行覆盖。(默认情况下,从最开始的地方覆盖)
- 可以通过相关的操作,实现RandomAccessFile“插入”数据的效果。
3.构造器
public RandomAccessFile(File file, String mode)
public RandomAccessFile(String name, String mode)
创建 RandomAccessFile 类实例需要指定一个 mode 参数,该参数指定 RandomAccessFile 的访问模式:
r: 以只读方式打开
rw:打开以便读取和写入
rwd:打开以便读取和写入;同步文件内容的更新
rws:打开以便读取和写入;同步文件内容和元数据的更新
如果模式为只读r。则不会创建文件,而是去读取一个已经存在的文件;如果读取的文件不存在则会出现异常。如果模式为rw读写。如果文件不 存在则会去创建文件,如果存在则不会创建。
示例代码一:
@Test
public void test1() {
RandomAccessFile raf1 = null;
RandomAccessFile raf2 = null;
try {
raf1 = new RandomAccessFile(new File("1.jpg"),"r");
raf2 = new RandomAccessFile(new File("2.jpg"),"rw");
byte[] buffer = new byte[1024];
int len;
while ((len = raf1.read(buffer)) != -1){
raf2.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (raf1 != null){
raf1.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (raf2 != null){
raf2.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例代码二:
//使用RandomAccessFile实现数据的插入效果
@Test
public void test3() {
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile("haha.txt","rw");
raf.seek(3);//将指针调到角标为3的位置
//保存指针3后面的所有数据到StringBuilder中
StringBuilder stubu = new StringBuilder((int) new File("haha.txt").length());
byte[] buffer = new byte[20];
int len;
while ((len = raf.read(buffer)) != -1){
stubu.append(new String(buffer,0,len));
}
//调回指针,写入“xyz”
raf.seek(3);
raf.write("123".getBytes());
//将StringBuilder中的数据写入到文件中
raf.write(stubu.toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (raf !=null){
raf.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}