一 File 类
文件和目录路径名的抽象表现形式.
无论是否真实存在,都可以创建File对象.
public class FileDemo01 {
public static void main(String[] args) throws IOException {
/*
* File(File parent, String child)
根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
File(String pathname)
通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
File(String parent, String child)
根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
*/
File file1=new File("D:\\AAA"); //"D:/AAA/haha.txt"
System.out.println(file1);
File file2=new File("D://","AAA//hehe.txt");
System.out.println(file2);
File file3=new File(file1,"haha.txt");
System.out.println(file3);
//1.boolean canWrite() 测试应用程序是否可以修改此抽象路径名表示的文件。 如果文件设置只读,就不可修改
System.out.println("canWrite():"+file3.canWrite());
//2.boolean setReadOnly()
System.out.println("setReadOnly():"+file3.setReadOnly());
System.out.println("canWrite():"+file3.canWrite());
//3.boolean createNewFile()当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
//如果文件所在路径不存在,报错找不到指定的路径,如果路径都存在创建一个新的文件
System.out.println("createNewFile():"+file2.createNewFile());
//4. boolean delete() 删除
System.out.println("delete():"+file2.delete());
System.out.println("delete():"+file1.delete());
//5.boolean exists() 测试此抽象路径名表示的文件或目录是否存在。
System.out.println("exists():"+file3.exists());
System.out.println("exists():"+file2.exists());
/*
* 6.File getAbsoluteFile()
返回此抽象路径名的绝对路径名形式。
String getAbsolutePath()
返回此抽象路径名的绝对路径名字符串。
*/
File file4=new File("hengheng.txt");
System.out.println("getAbsoluteFile():"+file4.getAbsoluteFile());
System.out.println("getAbsolutePath():"+file4.getAbsolutePath());
System.out.println(new File("D:").getTotalSpace());
System.out.println(new File("D:").getFreeSpace());
//7.String getName() 返回由此抽象路径名表示的文件或目录的名称。
System.out.println("getName():"+file3.getName());
System.out.println("getName():"+file1.getName());
/*
* 8.String getParent()
返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。
File getParentFile()
返回此抽象路径名父目录的抽象路径名;如果此路径名没有指定父目录,则返回 null。
*/
System.out.println("getParent():"+file3.getParent());
System.out.println("getParentFile():"+file1.getParentFile());
//9.boolean isAbsolute() 测试此抽象路径名是否为绝对路径名。
System.out.println("isAbsolute():"+file3.isAbsolute());
System.out.println("isAbsolute():"+file4.isAbsolute());
/*
* 10.boolean isDirectory()
测试此抽象路径名表示的文件是否是一个目录。
boolean isFile()
测试此抽象路径名表示的文件是否是一个标准文件。
boolean isHidden()
测试此抽象路径名指定的文件是否是一个隐藏文件。
*/
System.out.println("isDirectory():"+file1.isDirectory());
System.out.println("isFile():"+file1.isFile());
//11.long lastModified() 返回此抽象路径名表示的文件最后一次被修改的时间。
System.out.println("lastModified():"+new SimpleDateFormat().format(new Date(new File("D:/test.txt").lastModified())));
//12.long length() 返回由此抽象路径名表示的文件的长度。
System.out.println("length():"+file3.length());
/*
* 12.String[] list()
File[] listFiles()
*/
String[] strs=file1.list();
System.out.println(Arrays.toString(strs));
System.out.println(Arrays.toString(file1.listFiles()));
/*
* 13.boolean mkdir()
创建此抽象路径名指定的目录。
boolean mkdirs()
创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
*/
File file6=new File("D:/DDD/EEE");
File file7=new File("D:/CCCCC");
File file8=new File("E:/CCCCC");
System.out.println("mkdir():"+file6.mkdir());
System.out.println("mkdir():"+file6.mkdirs());
//boolean renameTo(File dest) 重新命名此抽象路径名表示的文件。
System.out.println("renameTo():"+file7.renameTo(file8));;
}
}
二 字节流(重点)
IO:
* 目的:读写文件中的内容
* 流:一连串流动的数据,先进先出的方式传输信息,管道
* 数据源 目的地-->以程序为中心划分读入写出
*
* 流的分类:
* 按照流向分:
* 输入流
* 输出流
* 按操作单元分:
* 字节流
* 字符流
* 按功能分:
* 节点流
* 功能流
* 分类之间是相辅相成的,互不冲突
*
* 字节流:是万能的 ***** 功能:节点流
* InputStream 字节输入流 此抽象类是表示字节输入流的所有类的超类。
* FileInputStream 从文件系统中的某个文件中获得输入字节。
* OutputStream 字节输出流
*/
文件的拷贝 代码
数据源 -->程序 --> 目的地
关闭流:先打开的后关闭,后打开的先关闭
public class CopyFile06 {
public static void main(String[] args) {
//1.建立联系
File src=new File("D:/test.txt");
File dest=new File("E:/test.txt");
//2.选择流
InputStream is=null;
OutputStream out=null;
try {
is=new FileInputStream(src);
out=new FileOutputStream(dest);
//3.操作
byte[] car=new byte[1024];
int len=-1;
while((len=is.read(car))!=-1){
out.write(car, 0, len);
}
//4.刷出
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
//5.关闭
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
三 字符流
字符流 :只能读写纯文本内容 功能分:节点流
Reader 字符输入流 FileReader 文件字符输入流
Writer 字符输出流 FileWriter 文件字符输出流
public class CharDemo01 {
public static void main(String[] args) throws IOException {
//1.选择流
Reader read=new FileReader("D:/test.txt");
Writer write=new FileWriter("E:/hehe.txt");
//2.读取
char[] car=new char[1024];
int len;
/*int num=read.read();
System.out.println((char)num);
System.out.println((char)read.read());*/
while((len=read.read(car))!=-1){
write.write(car, 0, len);
}
//3.刷出
write.flush();
//3.关闭
write.close();
read.close();
}
}
四 缓冲流
字符流 字节流 -->节点流
* 包裹节点流,在节点流之上-->功能流|处理流
* 处理流:增强功能,提高性能的作用,在节点流之外才能使用,不能代替节点流
缓冲流:提高性能
1.字节缓冲流
字节缓冲流 BufferedInputStream BufferedOutputStream 无新增方法
*/
public class BufferedDemo01 {
public static void main(String[] args) {
//1.建立联系
File src=new File("D:/test.txt");
File dest=new File("E:/test2.txt");
//2.选择流
InputStream is=null;
OutputStream out=null;
try {
is=new BufferedInputStream(new FileInputStream(src));
out=new BufferedOutputStream(new FileOutputStream(dest));
//3.操作
byte[] car=new byte[1024];
int len=-1;
while((len=is.read(car))!=-1){
out.write(car, 0, len);
}
//4.刷出
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
//5.关闭
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.字符缓冲流
字符缓冲流 不能发生多态,因为存在新增方法
* BufferedReader 字符输入缓冲流 新增方法 readLine() 读取一行
* BufferedWriter 字符输出缓冲流 新增方法 newLine() 写出换行符
*/
public class BufferedDemo02 {
public static void main(String[] args) throws IOException {
//1.选择流
BufferedReader read=new BufferedReader(new FileReader("D:/test.txt"));
BufferedWriter write=new BufferedWriter(new FileWriter("E:/hehe.txt"));
//2.读取
// String str=read.readLine();
String s=null;
while((s=read.readLine())!=null){
write.write(s);
write.newLine();
}
//3.刷出
write.flush();
//3.关闭
write.close();
read.close();
}
}
五 Data流 基本数据类型流
Data流 ->基本数据类型流,读入写出带有基本数据类型的数据+字符串数据
* 功能流,必须要包裹节点流使用(字节流)
* DataInputStream 新增方法 readXxx() //Xxx代表数据类型的包装类
* DataOutputStream 新增方法 writeXxx()
*/
public class DataDemo {
public static void main(String[] args) throws IOException {
write("D://test.txt");
read("D://test.txt");
}
public static void write(String dest) throws IOException{
DataOutputStream os=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));
String s="哈哈哈";
double d=3.14;
boolean flag=false;
//写出
os.writeUTF(s); //写出字符串
os.writeDouble(d);
os.writeBoolean(flag);
//刷出
os.flush();
//关闭
os.close();
}
public static void read(String src) throws IOException{
DataInputStream is=new DataInputStream(new BufferedInputStream(new FileInputStream(src)));
//读入带有数据类型的数据 写出什么顺序读入什么顺序
String s=is.readUTF();
double d=is.readDouble();
boolean f=is.readBoolean();
System.out.println(s);
System.out.println(d);
System.out.println(f);
//关闭
is.close();
}
}