day34
File类
理解
需求1:
public class Test01 {
public static void main(String[] args) {
File file = new File("D:\\笔记\\奇男子\\qnz.txt");
System.out.println("获取文件路径:" + file.getAbsolutePath());
System.out.println("获取文件名:" + file.getName());
System.out.println("获取文件是否可读:" + file.canRead());
System.out.println("获取文件是否可写:" + file.canWrite());
System.out.println("获取文件是否隐藏:" + file.isHidden());
System.out.println("获取文件大小(字节):" + file.length());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String datetime = sdf.format(file.lastModified());
System.out.println("获取文件最后修改时间:" + datetime);
}
}
相对路径 和 绝对路径
绝对路径:
相对路径:
public class Test02 {
public static void main(String[] args) {
File file = new File("file.txt");
System.out.println(file.length());
System.out.println("绝对路径:" + file.getAbsolutePath());
System.out.println("相对路径:" + file.getPath());
}
}
需求2
1)目录已存在的情况
public class Test01 {
public static void main(String[] args) throws IOException {
File file = new File("file01\\hhy.txt");
if(!file.exists()){
file.createNewFile();
}
}
}
2)有一个层级的目录不存在的情况
public class Test02 {
public static void main(String[] args) throws IOException {
File file = new File("file01\\qnz.txt");
File parentFile = file.getParentFile();
if(!parentFile.exists()){
parentFile.mkdir();
}
if(!file.exists()){
file.createNewFile();
}
}
}
3)有多个层级的目录不存在的情况
public class Test03 {
public static void main(String[] args) throws IOException {
File file = new File("file01\\file02\\file03\\qnz.txt");
File parentFile = file.getParentFile();
if(!parentFile.exists()){
parentFile.mkdirs();
}
if(!file.exists()){
file.createNewFile();
}
}
}
需求3:
public class Test01 {
public static void main(String[] args) {
File file = new File("D:\\笔记");
File[] listFiles = file.listFiles();
for (File f : listFiles) {
System.out.println(f.getName() + " -- " + f.canRead() + " -- " + f.canWrite());
}
}
}
1)要求只输出文件后缀名为txt的文件
public class Test02 {
public static void main(String[] args) {
File file = new File("D:\\笔记");
File[] listFiles = file.listFiles();
for (File f : listFiles) {
String fileName = f.getName();
if(f.isFile() && fileName.endsWith(".txt")){
System.out.println(fileName);
}
}
}
}
2)根据API的过滤器来完成该功能
public class Test03 {
public static void main(String[] args) {
File file = new File("D:\\笔记");
String[] list = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
File childFile = new File(dir, name);
if(childFile.isFile() && childFile.getName().endsWith(".txt")){
return true;
}
return false;
}
});
for (String fileName : list) {
System.out.println(fileName);
}
}
}
3)需求继续跟进,列出当前目录及子目录中符合该条件的文件信息(递归)
public class Test04 {
public static void main(String[] args) {
File file = new File("D:\\笔记");
fileHandler(file, ".txt");
}
public static void fileHandler(File file,String suffix){
File[] listFiles = file.listFiles();
for (File f : listFiles) {
if(f.isFile()){
String name = f.getName();
if(name.endsWith(suffix)){
System.out.println(name);
}
}else if(f.isDirectory()){
fileHandler(f, suffix);
}
}
}
}
IO流
概念
分类
存储单位
学习注意事项
字节流
利用 文件字节输出流 向文件写入数据
public class Test01 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("io.txt");
fos.write("123abc".getBytes(), 2, 3);
fos.close();
}
}
public class Test02 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("io.txt",true);
fos.write("123abc".getBytes());
fos.close();
}
}
public class Test03 {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("io.txt",true);
fos.write("123abc".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
利用 文件字节输入流 读取文件里的数据
public class Test04 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("io.txt");
int read = fis.read();
System.out.println(read);
read = fis.read();
System.out.println(read);
read = fis.read();
System.out.println(read);
read = fis.read();
System.out.println(read);
read = fis.read();
System.out.println(read);
read = fis.read();
System.out.println(read);
read = fis.read();
System.out.println(read);
fis.close();
}
}
int read;
while((read = fis.read()) != -1){
System.out.println((char)read);
}
byte[] bs = new byte[1024];
int len;
while((len = fis.read(bs)) != -1){
System.out.println(new String(bs, 0, len));
}
public class Test07 {
public static void main(String[] args){
FileInputStream fis = null;
try {
fis = new FileInputStream("io.txt");
byte[] bs = new byte[1024];
int len;
while((len = fis.read(bs)) != -1){
System.out.println(new String(bs, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件拷贝
public class Copy01 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("奇男子.mp4");
FileOutputStream fos = new FileOutputStream("copy.mp4");
int read;
while((read = fis.read()) != -1){
fos.write(read);
}
fis.close();
fos.close();
}
}
byte[] bs = new byte[1024];
int len;
while((len = fis.read(bs)) != -1){
fos.write(bs, 0, len);
}
public class Copy03 {
public static void main(String[] args){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("奇男子.mp4");
fos = new FileOutputStream("copy.mp4");
byte[] bs = new byte[1024];
int len;
while((len = fis.read(bs)) != -1){
fos.write(bs, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class Copy04 {
public static void main(String[] args){
try(FileInputStream fis = new FileInputStream("奇男子.mp4");
FileOutputStream fos = new FileOutputStream("copy.mp4");) {
byte[] bs = new byte[1024];
int len;
while((len = fis.read(bs)) != -1){
fos.write(bs, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符流
各种流
总结