////////window 系统下文件夹和文件确实不能同名,以前文件总带着后缀名竟然么有发现。。。
////////这个file是不分文件和目录的,不论创建的时候是不是以/结尾。因为不需要区分,创建文件和创建目录方法分开了,不用识别,如果你不用创建,是本身已经存在的东西,那直接读取识别就知道是文件还是目录。
public static void main(String[] args) throws IOException, URISyntaxException {
//四种构造方式。
File file = new File("a.txt");
file = new File("a/b/c.txt"); //这里写路径或者文件名都可以。
file = new File("a/b", "c.txt"); //左边路径,右边文件名。
//file = new File(new URI("F:\\JAVA\\code\\myCode\\src\\a\\b\\c\\c.txt")); //要用绝对路径,用了绝对路径也抛异常???
//创建父目录的file对象
File fu = new File("a/b/c");
System.out.println("fu.mkdirs() = " + fu.mkdirs());
System.out.println("fu.createNewFile() = " + fu.createNewFile()); //创建文件时,有同名文件或目录存在会失败。。。 对,文件和目录有名字冲突。
System.out.println("fu.isDirectory() = " + fu.isDirectory());//如果本地不存在叫c的东西返回false,如果有叫c的目录或文件那就根据实际来
file = new File(fu, "c.txt"); //这种在剪切前使用,方便创建文件夹。
//创建
System.out.println("fu.mkdirs() = " + fu.mkdirs());//mkdir()只创建一个文件夹,如果父文件夹不存在的话就会出错。
System.out.println("file.createNewFile() = " + file.createNewFile());//路径存在才能创建
System.out.println("fu.isDirectory()+fu.isFile()+fu.isAbsolute()+fu.exists() = " + fu.isDirectory() + fu.isFile() + fu.isAbsolute() + fu.exists());
file.delete(); //删除表示的这一个文件或目录,一个路径只表示一个文件!! 目录为空时才能删除目录。
file.createNewFile();
System.out.println("file.length() = " + file.length()); //返回文件内容长度,一个英文长为1,GBK编码下中文长2,UTF-8下中文长3. //如果此路径名表示一个目录,则返回值是不确定的。
//file.renameTo(new File("a/a.txt"));//剪切操作,不会自动创建目录,所以目录先创建好,配合用File的那个构造方法很得劲
System.out.println("---------------------------用相对路径定义:" + file.toString());
System.out.println("file.getName()" + file.getName());
System.out.println("file.getPath() = " + file.getPath());
System.out.println("file.getAbsolutePath() = " + file.getAbsolutePath());
System.out.println("file.getParent() = " + file.getParent());
System.out.println("file.getParentFile() = " + file.getParentFile());
file = new File("F:\\JAVA\\code\\myCode\\src\\a\\b\\c\\c.txt");
//file.createNewFile();
System.out.println("---------------------------用绝对路径定义:" + file.toString());
System.out.println("file.getName()" + file.getName());
System.out.println("file.getPath() = " + file.getPath());
System.out.println("file.getAbsolutePath() = " + file.getAbsolutePath());
System.out.println("file.getParent() = " + file.getParent());
System.out.println("file.getParentFile() = " + file.getParentFile());
new File("a/a.db").createNewFile();
new File("a/b.html").createNewFile();
new File("a/a.png").createNewFile();
new File("a/a.txt").createNewFile();
file = new File("a/"); //这里a后面加不加/都会识别成目录。
File[] files = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
String temp = pathname.getName();
if (temp.contains(".")) {
String[] strs = temp.split("\\.");
temp = strs[strs.length - 1].toLowerCase();
switch (temp) {
case "txt":
case "html":
case "css":
case "js":
return true;
default:
return false;
}
}
return false;
}
});
System.out.println("(files) = " + Arrays.toString(files));
delDirectory(new File("a"));
}
public static boolean delDirectory(File dir) { //删除目录,强力删除。
if (dir.isDirectory()) {
File[] files = dir.listFiles();
if (files.length == 0) {
dir.delete();
return true;
} else {
for (File file : files) {
if (file.isFile())
file.delete();
else
delDirectory(file);
}
}
dir.delete();//忘写这个了。只删文件,不删目录了就。
} else if (dir.isFile()) {
dir.delete();
return true;
} else
return false;
return true;
}
输出
fu.mkdirs() = true
fu.createNewFile() = false
fu.isDirectory() = true
fu.mkdirs() = false
file.createNewFile() = true
fu.isDirectory()+fu.isFile()+fu.isAbsolute()+fu.exists() = truefalsefalsetrue
file.length() = 0
---------------------------用相对路径定义:a\b\c\c.txt
file.getName()c.txt
file.getPath() = a\b\c\c.txt
file.getAbsolutePath() = D:\JAVA\code\myCode\a\b\c\c.txt
file.getParent() = a\b\c
file.getParentFile() = a\b\c
---------------------------用绝对路径定义:F:\JAVA\code\myCode\src\a\b\c\c.txt
file.getName()c.txt
file.getPath() = F:\JAVA\code\myCode\src\a\b\c\c.txt
file.getAbsolutePath() = F:\JAVA\code\myCode\src\a\b\c\c.txt
file.getParent() = F:\JAVA\code\myCode\src\a\b\c
file.getParentFile() = F:\JAVA\code\myCode\src\a\b\c
(files) = [a\a.txt, a\b.html]
读取内容
•
字符流
•
Reader:字符输入流
•
FileReader:文件字符输入流
BufferedReader:高效字符输入流
* Writer:字符输出流
* FileWriter:文件字符输出流
* BufferedWriter:高效字符输出流
* * * 字节流
* InputStream:字节输入流
* FileInputStream:文件字节输入流
* BufferedInputStream:高效字节输入流
* OutputStream:字节输出流
* FileOutputStream:文件字节输出流
* BufferedOutputStream:高效字节输出流
字符和字节的区别,一个英文占一个字节也占一个字符。一个中文在GBK编码下占2个字节,1个字符,在utf-8编码下占3个字节,1个字符。
所以处理中文文本文件的话,最好用字符流。
字符流没有getChannel()方法,因为getChannel()是针对字节的。
中文不能用‘’往byte里放。但是可以用两位byte来放一个中文。先将中文转成byte,然后把数字放到byte里。out.write(new byte[]{-50, -46, 'c', '\''});资源要及时关闭。读取之前数组不清空,有一个记录的读取位置。
这是Buffer* 的函数,不用Buffer的没这个函数
flush()是立刻将东西输出。也就是清空缓存
close() 的同时会执行默认flush(),所以中间不怎么需要写flush()。
后面带stream的是字节流,前面带buffer的是带缓存器的。
字符流的就是把中文也当成一个字符处理,处理中文十分方便,并多了几个直接操作String的函数。其它都和字节流的一样。还有byte[]换成了char[].
带不带缓存的类包含的方法都一样。就是读取效率有差别。
byte[] bytes = new byte[]{1,2,3}
bytes.toString()
new String(bytes)
Arrays.toString(bytes)
* OutputStream:字节输出流 此抽象类是表示输出字节流的所有类的超类* * 子类:FileOutputStream* * 文件输出流是用于将数据写出 File * * 构造方法:public FileOutputStream(File file)* 创建一个向指定 File 对象表示的文件中写入数据的文件输出流* public FileOutputStream(File file, boolean append):append为true,代表追加* * * public abstract void write(int b):将指定的一个字节写入此输出流 (不能写汉字)* * public void write(byte[] b):一次写一个字节数组 * 写汉字 先将字符串转为字节数组(getBytes())---->write(by);* public void write(byte[] b, int off, int len):写字节数组中的一部分 * * public void close():关闭此输出流并释放与此流有关的所有系统资源
* InputStream:表示字节输入流的所有类的超类* * FileInputStream 从文件系统中的某个文件中获得输入字节* * FileInputStream(File file) * FileInputStream(String name)* * * 方法* abstract int read() :从输入流中读取数据的下一个字节 如果到达流的末尾,则返回 -1。* * public int read(byte[] b):一次读取一个数组:返回值代表真正读取的字节个数 如果达到文件末尾返回-1* BufferedOutputStream:该类实现缓冲的输出流。* * public BufferedOutputStream(OutputStream out)* * 写出成功,必须执行flush()方法* 调用close()也可以刷新成功,因为close方法调用了flush* BufferedInputStream:高效字节输入流* 在创建 BufferedInputStream 时,会创建一个内部缓冲区数组* 填充该内部缓冲区(读取了多个),读取的时候就是从内部缓冲区读取* 字符流* Writer:写出字符流的抽象类* 维护了一个缓冲区 不执行flush,写出失败* FileWriter:文件字符输出流 用来写出字符文件的便捷类* * * void write(char[] cbuf) 写入字符数组。 abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分。 void write(int c) 写入单个字符。 void write(String str) 写入字符串。 void write(String str, int off, int len) 写入字符串的某一部分。
* Reader* * FileReader:* 用来读取字符文件的便捷类* * int read() 读取单个字符。 int read(char[] cbuf) 将字符读入数组。 abstract int read(char[] cbuf, int off, int len) 将字符读入数组的某一部分。
评论区