File类
什么是文件?
文件可认为是相关记录或放在一起的数据的集合。
文件的相关操作
创建文件
File file=new File("src/abc.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
判断文件的属性,都会返回boolean类型的值
file.canExecute();
file.canRead();
file.canWrite();
判断文件是否存在
System.out.println(file.exists());
获取文件的名称
System.out.println(file.getName());
获取当前文件的绝对路径
返回文件绝对路径的规范格式
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
获取文件的父路径名称
System.out.println(file.getParent());
判断文件是否是文件或者目录
System.out.println(file.isDirectory());
System.out.println(file.isFile());
输出所有文件
File file1=new File("d:/");
String[] list=file1.list();
for (String str:list){
System.out.println(list);
}
System.out.println("---------");
File[] files=file1.listFiles();
for (File file2:files){
System.out.println(file2);
}
打印当前文件系统的所有盘符
File[] files1=File.listRoots();
for (int i=0;i<files1.length;i++){
System.out.println(files1[i]);
}
创建单级目录
File file2=new File("d:/a");
file2.mkdir();
创建多级目录
File file1=new File("d:/a/b/c");
file1.mkdirs();
IO流的原理及概念
如何读写文件?
通过流来读写文件,流是指一连串流动的字符,是以先进先出方式发送信息的通道。
输入/输出流与数据源
XXX–>程序–>输入流
程序–>XXX–>输出流
数据源
提供原始数据的原始媒介。常见的:数据库、文件、其他程序、内存、网络连接】IO设备。
IO流的分类
1.按流向分:输入流和输出流。
输入流:
添加缓冲区的方式读取,每次会将数据添加到缓冲区中。
InputStream inputStream=null;
try{
inputStream=new FileInputStream("abc.txt");
int length=0;
//添加缓冲区的方法
byte[] buffer=new byte[1024];
while ((length= inputStream.read(buffer))!=-1){
System.out.println(new String(buffer,0,length));
}
// byte[] buffer=new byte[1024];
// while ((length= inputStream.read(buffer,5,5))!=-1){
// System.out.println(new String(buffer,5,length));
// }
// int read=inputStream.read();
// System.out.println((char)read);
// read=inputStream.read();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
try{
inputStream.close();
}catch (IOException e){
e.printStackTrace();
}
}
输出流:
File file=new File("aaa.txt");
OutputStream outputStream=null;
try {
outputStream = new FileOutputStream(file);
outputStream.write(88);
//注意:\r\n是换行操作
outputStream.write("\r\nmimi".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
IO流的体系
字节流和字符流
字节流可以直接读取中文汉字,而字符流会出现中文乱码。
处理流和节点流
按功能不同:节点流和处理流。
节点流: 可以直接从数据源或目的地读写数据。
处理流(包装流): 不直接连接到数据源或目的地,是其他流进行封装。目的主要是简化操作和提高性能。
节点流和处理流的关系: 节点流处于io操作的第一线,所有操作必须通过他们进行;处理流可以对其他流进行处理(提高效率或操作灵活性)。
文件拷贝
复制文本文件:
//定义源数据文件
File src=new File("abc.txt");
File dest=new File("aaa.txt");
//创建输入输出流对象
InputStream inputStream=null;
OutputStream outputStream=null;
try{
inputStream=new FileInputStream(src);
outputStream=new FileOutputStream(dest);
//带缓冲的输入输出方式
byte[] buffer=new byte[1024];
int length=0;
//完成数据传输的过程
while ((length=inputStream.read(buffer))!=-1){
outputStream.write(buffer);
}
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
复制图片:最好使用字节流
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
try {
fileInputStream =new FileInputStream("1.jpg");
fileOutputStream=new FileOutputStream("2.jpg");
int length=0;
byte[] buffer=new byte[1024];
while ((length=fileInputStream.read(buffer))!=-1){
fileOutputStream.write(buffer);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}