day8IO流
File类的常用方法
public static void main(String[] args) {
/*
1.如何创建File类的实现
2.相对路径:相较于某个路径下,指明的路径
3.绝对路径:包含盼复在内的文件或文件目录的路径
*/
?
//构造器1
File file = new File("Hello.txt"); //相对于当前module
File file1 = new File("C:\\Users\\86137\\Desktop\\作业\\JavaSE\\基础语法\\src\\com\\file\\Demo01");
System.out.println(file);
System.out.println(file1);
//构造器2
File file2 = new File("C:\\Users\\86137\\Desktop\\作业\\JavaSE\\基础语法\\src","Hello");
System.out.println(file2);
//构造器3
File file3 = new File(file2,"Hi.txt");
System.out.println(file3);
}
}
-
public String getAbsoLutePath():获取绝对路径
-
public String getPath() :获取路径
-
public String getName() :获取名称
-
public String getParent():获取上层文件目录路径。若无,返回null
-
public long length() :获取文件长度(即:字节数)。不能获取目录的长度。
-
public Long lastModified() :获取最后一次的修改时间,毫秒值
-
public string[ ] list():获取指定目录下的所有文件或者文件目录的名称数组public
-
File[ ] listFiles() :获取指定目录下的所有文件或者文件目录的File数组
-
public boolean renameTo(File dest):把文件重命名为指定的文件路径
-
需要保证重命名成功要保证file1存在,file2不存在
-
boolean rename To = file1.renameTo(file2)
-
public static void main(String[] args) {
File file1 = new File("F:\\soft");
String[] s = file1.list();
for (String s1 : s) {
System.out.println(s1);
}
System.out.println("===========");
File[] list = file1.listFiles();
for (File file : list) {
System.out.println(file);
}
?
}
?
CREF.EXE
debug.exe
DEBUG32.EXE
===========
F:\soft\CREF.EXE
F:\soft\debug.exe
F:\soft\DEBUG32.EXE
?
-
public booLean isDirectory():判断是否是文件目录
-
public booLean isFile() :判断是否是文件
-
public booLean exists() :判断是否存在
-
public boolean canRead() :判断是否可读
-
public boolean canwrite() :判断是否可写
-
public boolean isHidden() :判断是否隐藏
-
public boolean createNewFile() :创建文件。若文件存在,则不创建,返回false
-
public boolean mkdir() :创建文件目录。如果此文件目录存在,就不创建了。如果此文件不存在,就创建目录。
-
public boolean mkdirs() :创建文件目录。如果上层文件目录不存在,一并创建
-
删除磁盘中的文件或文件目录
-
public booLean deLete( ):删除文件或者文件夹,删除注意事项:java中的删除不走回收站。
-
IO流
-
按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
-
按数据流的流向不同分为:输入流,输出流
-
按流的角色的不同分为:节点流,处理流
| 抽象基类 | 字节流 | 字符流 |
|---|---|---|
| 输入流 | InputStream | Reader |
| 输出流 | OutputStream | Writer |
FileReader fr = null;
try {
//实例化要操作的文件
File file = new File("Hello.txt"); //相较于当前工程
System.out.println(file.getAbsoluteFile());
//提供具体的流
fr = new FileReader(file);
?
//数据的读入
//read()返回读入的一个字符,如果达到文件末尾,返回-1
//方式1
// int data = fr.read();
// while ((data != -1)){
// System.out.print((char)data);
// data = fr.read();
// }
?
//方式二
int data;
while ((data = fr.read()) != -1){
System.out.print((char)data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//流的关闭操作
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
FileReader fr = null;
try {
//1.File类的实例化
File file = new File("Hello.txt");
//2.FIleReader流的实例化
fr = new FileReader("Hello.txt");
//3.读入的操作
//read(char[] cbuf)返回每次读入cbuf数组中的字符的个数
//如果达到文件末尾返回-1
char[] cbuf = new char[5];
int len;
//方式一
while((len= fr.read(cbuf)) != -1) {
for (int i = 0; i < len; i++) {
System.out.print(cbuf[i]);
}
//错误的写法
// for(int i = 0;i< cbuf.length;i++)
// System.out.println(cbuf[i]);
?
//方式二
//错误写法,对应方式一的错误写法,将数组里有啥取啥
// String str = new String(cbuf);
// System.out.println(str);
//正确写法
String str = new String(cbuf, 0, len);
System.out.print(str);
?
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fr !=null)
//4.资源的关闭
{
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
?
}