Java.IO基础内容
如何在Java程序中操作这些保存数据的文件呢?Java.IO包提供了一些接口和嘞,对文件进行基本的操作,包括对文件读写的操作,还有一些属性的操作/
创建一个File对象的语法格式如下。
File file = new File(String pathName);
其中pathName表示所指向的文件路径名。
File类的常用方法
方法名称 | 说明 |
---|---|
boolean exists() | 判断文件或者目录是否存在 |
boolean isFile() | 判断是否是文件 |
boolean isDirectory() | 判断是否是文件目录 |
String getPath() | 获取路径 |
String getAbsolutePath() | 获取绝对路径 |
String getName() | 获取文件或者目录的名称 |
boolean createNewFile() | 创建文件 |
long length() | 获取文件的长度,单位为字节,文件不存在,返回OL |
boolean delete() | 删除指定文件、目录 |
public class FileMethods2 {
public static void main(String[] args) {
FileMethods2 fm = new FileMethods2();
File file = new File("D:\\java\\hello.txt");
fm.create(file);
fm.showFileInfo(file);
//fm.delete(file);
}
public void create(File file) {
if (!file.exists()) {
try {
file.createNewFile();
System.out.println("文件已创建");
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
public void delete(File file) {
if (file.exists()) {
file.delete();
System.out.println("文件已删除");
}
}
public void showFileInfo(File file ) {
if (file.exists()) {
if (file.isFile()) {
System.out.println("名称"+file.getName());
System.out.println("相对路径"+file.getPath());
System.out.println("绝对路径"+file.getAbsolutePath());
System.out.println("文件大小"+file.length()+"字节");
}
if (file.isDirectory()) {
System.out.println("此文件是目录");
}
}else {
System.out.println("文件不存在");
}
}
}
在代码showFileInfo(File file )方法中,首先使用exists()方法对文件对象进行判断,如果文件或目录存在继续使用isFile()方法进一步判断是文件还是目录,若是文件,则显示文件名称,路径及大小。再看一下代码create(File file)方法,首先判断文件是否存在,然后使用createNewFile()方法创建一个空文件。
java的流
前面讲述了如何利用File类对文件或目录的属性进行操作,但File类不能访问文件的内容既不能从文件中读取数据或向文件写数据,下面我们来学习对文件的读写.
读文件,是指把文件中的数据读取到内存中,反之写文件是吧内存中的数据写到文件中,那么用什么去读写文件呢?答案就是流
流:一连串流动的字符,是以先进先出的方式发送和接收数据的通道
按照流的流向进行划分,可以分为输入流和输出流:
- 输入流:只能从中读取数据
- 输出流:只能向其中写入数据
按照所操作的数据单元的不同,留有可以分为字符流和字节流:
- 字节流:最先小单元为8个字节;字节输入;字节输出
- 字符流:最先小单元为16个字符;字符输入;字符输出
读写文件文件
使用字节流读取文本文件
字节输入流InputStream类
字节输入流InputStream的作用就是将文件中的数据输入到内部存储器中,它提供了一系列和读取数据有关的方法,常用的方法如表所示:
方法名称 | 说明 |
---|---|
int read() | 读取一个字节数据 |
int read(byte [] b) | 将数据读取到字节数组中 |
int read(byte[] b,int off,int len) | 从输入流中读取最多len长度的字节,保存到字节数组b中,保存的位置从off开始 |
void close() | 关闭输入流 |
int available() | 返回输入流读取的估计字节数 |
字节输入流FileInputStream类
FileInputStream两个构造方法如下:一个是String数据类型 一个是File数据类型
File file =new File("hello.txt");
FileInputStream fis =new FileInputStream(file);
FileInputStream fis =new FileInputStream("hello.txt");
public class FileInputStreamTest {
public static void main(String[] args) throws IOException {
FileInputStream fis = null;
try {
fis = new FileInputStream("D:\\java\\zh.txt");
int data;
System.out.println("可读取的字节数"+fis.available());
System.out.println("文件内容为:");
while ((data = fis.read())!=-1) {
System.out.println((char)data+" ");
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if (fis!= null) {
fis.close();
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
首先创建流对象fis,该对象指向hello文件,然后定义整型变量data,用来接收读取到的数据,利用while循环读取文件的内容,进行输出,最后关闭流;
使用FileInputStream类读文件数据是应注意以下几个方面。
- read()方法返回整数,若读取的是字符串,则需强制类型转换
- 流对象使用完毕后需要关闭
使用字节流写文本文件
字节输出流OutputStream类
字节输入流OutputStream类的作用是吧内存中的数据输出到文件中,它提供了一系列想文件中写数据的有关方法
OutputStream常用方法
方法名称 | 说明 |
---|---|
void write(int c) | 写入一个字节 |
void write(byte [] buf) | 写入数组buf的所有字节 |
void write(byte [] b,int off,int len) | 将字节数组中从off位置开始,长度为len的字节数据输出到输出流中 |
void close() | 关闭输出流 |
字节输出流FileOutputStream类
引入相关类
构造一个文件输出流对象
OutputStream fos = new FileOutputStream("C:\\test.txt");
利用文件输出流的方法把数据写到文本文件中
String str = "好好学习";
byte[] words = str.getBytes();
fos.write(words,0,words.length);
关闭文件流
fos.close();
使用字符流读取文本文件
字符输入流Reader类
方法名称 | 说明 |
---|---|
int read() | 从输入流中读取单个字符 |
int read(byte [] c) | 从输入流中读取c.length长度的字符,保存到字符数组c中,返回实际读取的字符数 |
int read(byte [] c,int off ,in len) | 从输入流中读取最多len长度的字符,保存到字符数组c中,保存的位置从off位置开始,返回实际读取的字符长度 |
void close() | 关闭流 |
public class Test5 {
public static void main(String[] args) {
Reader fr = null;
StringBuffer sbf = null;
try {
fr = new FileReader("D:\\java\\zhasd.txt");
char ch[] = new char[1024];
sbf = new StringBuffer();
int length = fr.read(ch);
while ((length!=-1)) {
sbf.append(ch);
length=fr.read();
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if (fr!= null) {
fr.close();
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
创建字符数组ch作为中转站,存入每次读取的内容,然后调用FileReader对象的read()方法将字符读入数组ch并追加字符串sbf中。
在开发中,我们通常会将FileReader类与BufferedRead类结合使用,可提高读取文件的效率,现在我们就一起来学习BufferedReader类
字符输入流BufferedReader类
使用字符流类BufferedReader和Filereader读取文本文件的具体步骤如下
引入相关的类
创建一个BufferedReader对象
Reader fr =new FileReader("D:\\test.txt");
BufferedReader br = new BufferedReader(fr);
利用BufferedReader类的方法读取文本文件的数据
br.readline();
关闭相关的流对象
br.close();
fr.close();
使用字符流写文本文件
字符输出流Writer类
方法名称 | 说明 |
---|---|
write(String str) | 将str字符串里包含的字符输出到指定的输出流中 |
Write(String str,int off ,int len) | 将str字符串里从off位置开始长度为len的字符输出到输出流中 |
voud flush() | 刷新输出流 |
void close() | 关闭输出流 |
步骤如下
引入相关的类
创建一个FileWriter对象
Writer fw = new FileWriter("D\\test.txt");
利用FileWriter类的方法写文本文件
bw.write("hello");
相关流对象的清空和关闭
bw.flush();
fw.close();
示例:
public class Test7 {
public static void main(String[] args) {
Writer fw = null;
try {
fw = new FileWriter("D:\\java\\zhaassd.txt");
fw.write("我热爱我的团队");
fw.flush();
} catch (IOException e) {
// TODO: handle exception
System.out.println("文件不存在");
}finally {
try {
if (fw!= null) {
fw.close();
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
字符输出流BufferedWriter类
步骤:
引入相关的流
构造一个BufferedWriter对象
FileWriter fw = new FileWriter("D:\\java\\zhaassd.txt");
BufferedWriter bw = new BufferedWriter(fw);
利用BufferedWriter类的方法写文本文件
bw.write("Hello");
相关流的对象的清空和关闭
bw.flush();
fw.close();
二进制文件的读写
使用字节流类DataInputStream读二进制文件
具体步骤如下
引入相关的类
构造一个数据输入流对象
FileInputStream fis = new FileInputStream("D:\\test.txt");
DataInputStream dis = new DataInputStream(fis);
利用数据输入流类的二进制文件的数据
dis.read();
关闭数据输入流
dis.close();
使用字节流类DataInputStream写二进制文件
具体步骤如下
引入相关的类
构造一个数据输入流对象
FileOutputStream outFile = new FileInputStream("D:\\test.txt");
DataOutputStream out = new DataInputStream(outFile);
利用数据输入流类的二进制文件的数据
out.write(1);
关闭数据输入流
out.close();