Java基础复习-IO流
本文仅对所学java知识的查缺补漏
File
File类的使用
1.File类的一个对象,代表一个文件或一个文件目录(俗称:文件夹)
2.File类声明在java.io包下
3.路径分隔符与操作系统:
window和DOS系统默认使用 \ 表示
UNIX和URL使用 / 表示
4.Java程序支持跨平台运行,因此路径分隔符要慎用
5.为了解决这个隐患,File类提供了一个常量:
public static final String separator:根据操作系统,动态提供分隔符
File file = new File("C:"+File.separator+"test.txt");
6.File类中涉及到关于文件或文件目录的创建、删除、重命名、修改时间、文件大小等方法,
但并未涉及到写入或读取文件内容的操作,如果需要读取或写入文件内容,必须使用IO流完成
创建对象
File(String filePath);
File(String parentPath, String childPath);
File(File parentFile, String childPath);
常用方法
public String getAbsolutePath()
:获取绝对路径public String getPath()
:获取路径public String getName()
:获取名称public String getParent()
:获取上层文件目录路径。若无,返回nullpublic long length()
:获取文件长度(即:字节数)。不能获取目录长度public long lastModified()
:获取最后一次的修改时间,毫秒数public String[] list()
:获取指定目录下的所有文件或者文件目录的名称数组(适用目录)public File[] listFiles()
:获取指定目录下的所有文件或者文件目录的File数组(适用目录)public boolean reanmeTo(File dest)
:把文件重命名为指定的文件路径。- 比如:
file1.renameTo(file2)
,要想保证返回true,需要file1在硬盘中存在,而且file2在硬盘中不存在
- 比如:
public boolean isDirectory()
:判断是否是文件目录public boolean isFile()
:判断是否是文件public boolean exists()
:判断是否存在public boolean canRead()
:判断是否可读public boolean canWrite()
:判断是否可写public boolean isHidden()
:判断是否隐藏public boolean createNewFile()
:创建文件,若文件存在,则不创建,返回falsepublic boolean mkdir()
:创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层不存在,则创建失败public boolean mkdirs()
:创建文件目录。如果上层目录不存在,一并创建public boolean delete()
:删除文件或者文件夹(注意:Java中的删除不走回收站)
IO流
流的分类
- 按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
- 按数据流的流向不同分为:输入流,输出流
- 按流的角色的不同分为:节点流,处理流(节点流的包装)
抽象基类 | 字节流 | 字符流 |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
流的操作步骤(固定)
- 实例化
File
对象 - 实例化流对象
- 要进行流的操作
- 关闭流
例子:文本复制(字符流)
public class FileReaderWriterTest {
@Test
public void test(){
FileReader fileReader = null;
try {
//1.实例化File类对象,指明要操作的文件
File file = new File("src\\javabasic\\day26","hello.txt");
System.out.println(file.getAbsolutePath());
//2.提供具体的流
fileReader = new FileReader(file);
//3.数据的读入
//read():返回读入的一个字符。如果到达文件末尾,则返回-1
//read(char[] cbuf):返回每次读入cbuf数组中的字符个数。如果到达文件末尾,返回-1。这种方式,如果读入的个数比char[]数组的长度小,那么char[]数组中还会保留部分上一次读入的内容
int data;
while((data = fileReader.read()) != -1){
System.out.print((char)data);
}
}catch (IOException e){
e.printStackTrace();
}finally {
//4.关闭流,因为对于物理连接,比如:数据库连接,IO流,Socket的连接,JVM是关闭不了的,需要手动关闭
try {
if (fileReader != null)
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
例子:图片复制(字节流)
/**
* 图片复制:使用FileInputStream和FileOutputStream
*/
@Test
public void test3(){
File file = new File("src\\javabasic\\day26", "hello.jpg");
File file1 = new File("src\\javabasic\\day26", "hello1.jpg");
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = new FileInputStream(file);
outputStream = new FileOutputStream(file1);
byte[] bytes = new byte[5];
int len;
while((len = inputStream.read(bytes)) != -1){
outputStream.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意
- 对于文本文件(
.txt,.java,.c,.cpp
),使用**字符流(16bit)**处理 - 对于非文本文件(
.jpg,.mp3,.mp4,.avi,.doc,.ppt......
),使用**字节流(8bit)**处理 - 使用字节流处理文本文件可能会出现乱码(复制文件可以用字节流,相当于数据的搬运工)
缓冲流
BufferedInput / OutputStream
提高了字节流的读取和写入速度,
原因:底层有一个8k大小的byte
缓冲区:即byte[8192]
/**
* 处理流之一:缓冲流的使用
* @Author: fxx
* @Date: 2020/12/28 14:26
*/
public class BufferedTest {
@Test
public void test(){
//1.实例化File
File file1 = new File("src\\javabasic\\day26", "hello.jpg");
File file2 = new File("src\\javabasic\\day26", "hello1.jpg");
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
//2.实例化节点流
inputStream = new FileInputStream(file1);
outputStream = new FileOutputStream(file2);
//3.实例化处理流:包装节点流的流
bufferedInputStream = new BufferedInputStream(inputStream);
bufferedOutputStream = new BufferedOutputStream(outputStream);
//4.操作流
byte[] bytes = new byte[5]; //每次读进来的流字节,一般文件比较大的话会设置为1024
int len;
long start = System.currentTimeMillis();
while((len = bufferedInputStream.read(bytes)) != -1){
bufferedOutputStream.write(bytes, 0, len);
}
long end = System.currentTimeMillis();
System.out.println("耗时:"+(end - start));
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.关闭流
//关闭流的操作是:先关闭外层处理流,再关闭内层节点流
//说明:关闭外层流的同时,内层流也会自动关闭,所以这里只需要关闭外层流
try {
if(bufferedInputStream != null)
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedOutputStream != null)
bufferedOutputStream.close();
} catch (IOException e