文章目录
一、file类
在 Java 中,File 类是 java.io 包中唯一代表磁盘文件本身的对象。File 类定义了一些与平台无关的方法来操作文件,File类主要用来获取或处理与磁盘文件相关的信息,像文件名、 文件路径、访问权限和修改日期等,还可以浏览子目录层次结构。
File 类表示处理文件和文件系统的相关信息。也就是说,File 类不具有从文件读取信息和向文件写入信息的功能,它仅描述文件本身的属性。
1.1引入file类
import java.im.File
1.2构造一个文件对象
File file=new File("text.txt");
1.3构造方法
File(String pathname) 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
File(String parent,String child) 根据指定的父路径和文件路径创建一个新File对象实例
File(File parent,String child) 根据指定的父路径对象和文件路径创建一个新的File对象实例
1.4File类创建和删除功能
boolean createNewFile();指定路径不存在该文件时创建文件,返回true 否则false
boolean mkdir() 当指定的单击文件夹不存在时创建文件夹并返回true 否则false
boolean mkdirs() 但指定的多级文件夹在某一级文件夹不存在时,创建多级文件夹并返回true 否则false
boolean delete() 删除文件或者删除单级文件夹
删除文件夹,这个文件夹下面不能有其他的文件和文件夹
演示如下:
import java.io.File;
import java.io.IOException;
/*
file类
创建一个文件/文件夹
删除一个文件/文件夹
获取文件/文件夹
判断文件/文件夹
对文件进行遍历
获取文件大小
file 是一个与操作系统无关的类
记住三个单词:
file:是文件
directory:目录、文件夹
path:路径
绝对路径
相对路径
*/
public class Demo01File {
public static void main(String[] args) throws IOException {
String pathSeparator = File.pathSeparator;//文件路径分割符
// System.out.println(pathSeparator);//win 是分号;Linux中是 :冒号
String separator = File.separator;
// System.out.println(separator);//文件名称分割符 Linux:/root/home/
//绝对路径D:\代码\java
//相对路径 ../../
//构造方法演示
show01();
show02("D:\\代码\\java","test");
show03();
// 常用方法演示
show04();
show05();
show06();
show07();
show08();
show09();
show10();
show11();
show12();
}
private static void show12(){
//如果路径不存在,直接返回false
File f1 = new File("D:\\代码\\java\\hello.java");
boolean b1=f1.delete();
System.out.println(b1);
File f2 = new File("1\\2\\33\\44");//相对路径 但是会自动补齐 D:\代码\java\hello
boolean b2=f2.delete();//直接从硬盘上删除 不会放入回收站
System.out.println(b2);
}
private static void show11(){
File f1 = new File("D:\\代码\\java\\hello.java");
boolean b1=f1.mkdir();//只能创建一个文件夹
System.out.println(b1);
File f2 = new File("D:\\代码\\java\\1\\2\\33\\44\\hello.java");
boolean b2=f1.mkdirs();//递归创建文件夹
System.out.println(b1);
}
private static void show10() throws IOException {
File f1 = new File("D:\\代码\\java\\hello.java");
boolean b1 = f1.createNewFile();
System.out.println(b1);
//第二次执行的时候返回false,因为文件已经存在,他只创建未创建的文件
File f2 = new File("D:\\代码\\java\\新建文件夹");//只能创建文件 不要看文件文字 看类型
boolean b2 = f2.createNewFile();
System.out.println(b2);
}
private static void show09(){
File f1 = new File("D:\\代码\\java");//存在
if(f1.exists()) {//返回TRUE,进入if语句
System.out.println(f1.isDirectory());//true
System.out.println(f1.isFile());//false
}
File f2 = new File("D:\\代码\\day20");//存在
if(f2.exists()) {//不存在返回FALSE,所以不会进入if语句
System.out.println(f2.isDirectory());
System.out.println(f2.isFile());
}
File f3 = new File("D:\\代码\\java\\代码.iml");
if(f3.exists()){ //判断是否为文件夹
System.out.println(f2.isDirectory());//false 是个文件
System.out.println(f2.isFile());//false
}
}
private static void show08(){
File f1 = new File("D:\\代码\\java");
System.out.println(f1.exists());//文件存在打印true,不存在答应false
}
//注意 文件夹没有大小概念,不能获取文件夹的大小
private static void show07() {
File f1 = new File("D:\\代码\\day20");
System.out.println(f1.length());//文件夹的大小0
File f2 = new File("D:\\代码\\day20\\classfive");
System.out.println(f2.length());//不存在的文件夹,打印的大小为0
File f3 = new File("D:\\代码\\java\\代码.iml");//存在
System.out.println(f3.length());
}
private static void show06(){
File f1 = new File("D:\\代码\\day20\\hello.java");
File f2 = new File("D:\\代码\\day20");
//获取的构造方法传递路径结尾部分
System.out.println(f1.getName());
System.out.println(f2.getName());
}
private static void show05() {
File f1 = new File("D:\\代码\\day20\\hello.java");
File f2 = new File("a.txt");
String path1 =f1.getPath();
System.out.println(path1);
System.out.println(f2.getPath());
System.out.println(f1);
System.out.println(f1.toString());
}
private static void show04(){
File f1 = new File("D:\\代码\\java.b.txt");
String absolutepath = f1.getAbsolutePath();//获取绝对路径
System.out.println(absolutepath);
File f2 = new File("hello.java");//相对路径创建,获取绝对路径
//创建的时候,是放在项目路径下面
String absolutepath2 = f2.getAbsolutePath();
System.out.println(absolutepath2);
}
private static void show03(){
File parent = new File("D:\\代码\\java");
File f1 = new File(parent,"hello,java");
System.out.println(f1);
}
private static void show02(String parent,String child) {
File f1 = new File(parent,child);
System.out.println(f1);
}
private static void show01(){
File f1 = new File("D:\\代码\\java");
System.out.println(f1);
//仅仅是创建了File这样的类,并不会去检验他的正确性
File f2 = new File("D:\\代码\\java\\b.txt");
System.out.println(f2);
File f3 = new File("b.txt");
System.out.println(f3);
}
}
运行结果如下:
二、字节流
Java定义了两种类型的输人输出流/字节流和字符流。字节流(byte stream)用以处理字节数据的输人和输出,例如,使用字节流读取或写入二进制文件。
字节流处理字节数据的输人和输出,即以8位二进制为单位进行读/写,更适合图像、声音等二进制文件的读/写。
字节流有两个重要的抽象类: InputStream和OutputStream,即字节输入流和字节输出流。而这两个抽象类都有多个具体的子类,这些子类对不同的数据源和目的地进行处理。
1.输入流
FileInputStrem俗称文件输入流,是字节输人流InputStream类的一个子类,作用是将文件中的数据输人内存中。IputStream类提供了一个重要的方法read(),可以利用该方法读取文本文件中的数据。其具体实现步骤有以下内容。
1.1引人相关的类:
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
1.2构造一个文件输人流对象:
InputStream fileobject = new FileInputStrean(test. txt" );
1.3利用文件输人流类的方法读取文本文件数数据:
fileobject. available();//可读取的字节数
fileobject. read( );//读取文件数据
1.4关闭文件输人流对象:
fileobject. close();//释放资源
演示如下:
import java.io.FileInputStream;
import java.io.IOException;
public class Demo01InputStream {
public static void main(String[] args) throws IOException {
//1、创建FileInputStream 对象,构造方法中绑定要读取的数据
FileInputStream fis= new FileInputStream("D:\\代码\\java\\com\\hg\\day21\\demo01\\a.txt");
//2、使用read方法 读取问
// int len=fis.read();
// System.out.println(len);
// len=fis.read();
// System.out.println(len);
// len=fis.read();
// System.out.println(len);
// len=fis.read();
// System.out.println(len);
int len=0;
while((len=fis.read())!=-1){
System.out.println(len);
}
//释放资源
fis.close();
System.out.println((char)97);
}
}
演示如下:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
public class Demo02InputStream {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("D:\\代码\\java\\com\\hg\\day21\\demo01\\a.txt");
// byte[] bytes=new byte[2];
// int len= fis.read(bytes);
// System.out.println("读取到的长度:"+len);//读取到的长度:2
//
System.out.println(bytes[0]);//取出数组里面的值
System.out.println(bytes[1]);
//
System.out.println(Arrays.toString(bytes));//输出[65,66]
//
// System.out.println(new String(bytes));//输出AB
//
// len= fis.read(bytes);
// System.out.println("读取到的长度:"+len);//读取到的长度:2
// System.out.println(new String(bytes));//C1
//
// len= fis.read(bytes);
// System.out.println("读取到的长度:"+len);//读取到的长度:2
// System.out.println(new String(bytes));//23
//
// len= fis.read(bytes);
// System.out.println("读取到的长度:"+len);//读取到的长度:1
// System.out.println(new String(bytes));//43
byte[] bytes = new byte[10];
int len=0;
while((len= fis.read(bytes))!=-1)
{
System.out.println(len);
System.out.println(new String((bytes)));
}
//释放资源
fis.close();
}
}
2.输出流
File( DutputStrearm俗称文件输出流,是字节输出流OutputStream类的一个子类,作用是把内存中的数据输出到文件中。OutputStream 类提供了一个重要的方法write(),可以利用该方法把内存中的数据写人文本文件中去。其具体实现步骤如下。
2.1引入相关的类:
import java.io.IOException
import java.io.OutputStream;
import java.io.FiletputStream;
2.2构造一个文件输出流对象:
OupuSream fos = new FileOutputStream(“copy. txt”);
2.3利用文件输出流类的方法把数据写人文本文件中:
String str=”好好学习Java";
byte[] words = str. getBytes();
fos. write(words ,0,words. length); //使 用write()方法把数据写入文件中
2.4关闭文件输出流对象:
fos. close();
演示如下:
package com.hg.day20.demo02;
import java.io.FileOutputStream;
import java.io.IOException;
/*
构造方法的作用
1、创建了一个FileOutputStream对象
2、根据构造方法中传递的文件/文件路径,创建一个空的文件
3、会把FileOutputStream对象指向创建的文件
写入过程:
Java程序 -->(java 虚拟机)--> os(操作系统上) --> 会调用对应的驱动 --> 把数据写入文件
*/
public class Demo02OutputStream {
public static void main(String[] args) throws IOException {
//创建一个FileOutputStream 对象 ,构造方法中传入数据目的地
FileOutputStream fos=new FileOutputStream("a.txt");
//调用write方法,将数据写入到文件中
fos.write(97);//
fos.write(98);
fos.write(99);
//释放资源
fos.write(49);
fos.write(48);
fos.write(48);
//100
// byte[] bytes={65,66,67,68};
// byte[] bytes={-65,-66,-67,-68};//中文
byte[] bytes={65,66,67,68,69};
fos.write(bytes,2,3);
fos.close();
}
}