使用Java中的文件操作函数来读写文件
在Java中,文件操作是一项非常基础且必要的功能。通过Java提供的文件操作函数,可以实现文件的读写、创建、复制、删除等操作,为我们开发实用的程序提供了极大的便利。本文将介绍在Java中如何读写文件,并举例说明常用的文件操作函数的使用方法。
一、读取文件
Java中读取文件最常用的方法是将文件转换成输入流,然后从输入流中读取数据。Java提供了多种输入流,如FileInputStream、DataInputStream等等,这里以FileInputStream为例来介绍怎样读取文件。
1. 以字节流的方式读取文件:
public static void readFileByBytes(String filePath) {
File file = new File(filePath);
InputStream in = null;
try {
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
以上代码首先创建一个File对象,然后创建一个FileInputStream对象,接着使用while循环从FileInputStream中读取数据,直到读取完毕。如果在读取过程中出现异常则打印异常信息并返回。这个方法可以读取任意类型的文件,但是读取到的数据都是二进制的。如果要读取文本文件,则需要将这些二进制数据转换成相应的字符串。
2. 以字符流的方式读取文件:
public static void readFileByChars(String filePath) {
File file = new File(filePath);
Reader reader = null;
try {
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
以上代码首先创建一个File对象,使用InputStreamReader将FileInputStream对象转换成字符流,再使用while循环从Reader中读取数据,直到读取完毕。如果在读取过程中出现异常则打印异常信息。这个方法可以读取文本文件,并且可以保留原文件的格式。
二、写入文件
与读取文件类似,Java中写入文件也是将数据转换成输出流,然后将数据写入输出流。Java中提供的输出流有多种,如FileOutputStream、DataOutputStream等等,这里以FileOutputStream为例来介绍怎样写入文件。
1. 以字节流的方式写入文件:
public static void writeFileByBytes(String filePath, String content) {
File file = new File(filePath);
OutputStream out = null;
try {
out = new FileOutputStream(file);
byte[] bytes = content.getBytes();
out.write(bytes);
out.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
以上代码首先创建一个File对象,然后创建一个FileOutputStream对象,接着将要写入的数据转换成字节数据,最后将字节数据写入到FileOutputStream中。如果在写入过程中出现异常则打印异常信息并返回。这个方法可以写入任意类型的文件,但是写入的数据都是二进制的。
2. 以字符流的方式写入文件:
public static void writeFileByChars(String filePath, String content) {
File file = new File(filePath);
Writer writer = null;
try {
writer = new FileWriter(file);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
以上代码首先创建一个File对象,然后创建一个FileWriter对象,接着将要写入的数据转换成字符串数据,最后将字符串数据写入到FileWriter中。如果在写入过程中出现异常则打印异常信息。这个方法可以写入文本文件,并且可以保留原文件的格式。
三、常用的文件操作函数
Java中提供了许多的文件操作函数,包括创建文件、删除文件、复制文件等等。这里介绍一些常用的函数。
1. 创建文件:
public static void createFile(String filePath) {
File file = new File(filePath);
try {
if (file.createNewFile()) {
System.out.println("File is created!");
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
以上代码首先创建一个File对象,然后使用createNewFile函数创建新文件,如果文件已经存在则会返回false。如果在创建过程中出现异常则打印异常信息。
2. 删除文件:
public static void deleteFile(String filePath) {
File file = new File(filePath);
if (file.exists()) {
file.delete();
System.out.println("File is deleted.");
} else {
System.out.println("File not exists.");
}
}
以上代码首先创建一个File对象,然后使用exists函数判断文件是否存在,如果存在则使用delete函数删除文件。如果文件不存在则直接返回。如果在删除过程中出现异常则打印异常信息。
3. 复制文件:
public static void copyFile(String sourceFilePath, String targetFilePath) {
File source = new File(sourceFilePath);
File target = new File(targetFilePath);
try {
InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(target);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File is copied!");
} catch (IOException e) {
e.printStackTrace();
}
}
以上代码首先创建两个File对象,一个是要复制的文件,一个是目标文件,然后使用FileInputStream读取源文件,使用FileOutputStream将数据写入目标文件。如果在复制过程中出现异常则打印异常信息。
四、总结
本文主要介绍了Java中文件操作函数的使用方法,并举例说明了常用的文件操作函数。熟练掌握这些函数可以使我们更加灵活地读写和处理文件,为我们开发实用的程序提供了极大的便利。同时,在文件读写的过程中,我们还要注意一些细节问题,如文件路径的写法、文件的编码格式等等,这些都会影响到文件操作的正确性。
