文件的创建格式:
//第一种构造方式
File f1 = new File("E:\\io\\done\\io.txt");// 第一个\作为转义
//第二种构造方式
File f2 = new File("E:\\io\\done","io.txt");
//第三种构造方式
File f3 = new File("E:\\io\\done");
File f4 = new File(f3,"io.txt");
//第四种 URI网络格式,只看前三种
课上所学程序代码:
文件操作:
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileDome {
public static void main(String[] args) throws Exception {
// //第一种构造方式
// File f1=new File("E:\\io\\dome\\io.txt");
// //第二种构造方式
// File f2=new File("E:\\io\\dome\\", "io.txt");
// //第三种构造方式
// File f3=new File("E:\\io\\dome\\");
// File f4=new File(f3, "io.txt");
// File f5=new File("E:\\io\\test");
// //先判断,在删除
// if(f5.exists()) {
// f5.delete();
// }
// //创建文件夹
// boolean flag=f5.mkdir();
// System.out.println(flag);
//绝对路径
// File f6=new File("E:\\io\\test\\test.txt");
// //创建文件
//// boolean flag=f6.createNewFile();
//// System.out.println(flag);
// //获取文件的名字
// System.out.println(f6.getName());
// //获取文件的路径
// System.out.println(f6.getParentFile());
// //获取绝对路径
// System.out.println(f6.getPath());
//相对路径
// File f7=new File(".\\file\\test.txt");
//// f7.createNewFile();
// //获取相对路径
// System.out.println(f7.getPath());
// //获取绝对路径有点
// System.out.println(f7.getAbsolutePath());
// //获取绝对路径去掉点
// System.out.println(f7.getCanonicalPath());
// File f8=new File("E:\\io\\test\\aa.docx");
//获取文件大小
// System.out.println(f8.length());
//获取文件最后修改时间
// long lo=f8.lastModified();
// Date date=new Date(lo);
// SimpleDateFormat sid=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// String str=sid.format(date);
// System.out.println(str);
// File f9=new File("E:\\io\\test\\io\\test\\io\\test");
// //创建多层
// f9.mkdirs();
File f10=new File("e:"+File.separator+"io"+File.separator+"file.txt");
f10.createNewFile();
}
}
IO流操作:
package com.dhy.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class IODome {
public static void main(String[] args) throws Exception {
int data=0;
FileInputStream fin=null;
FileOutputStream fos=null;
//第一种创建方式
// File f=new File(".\\file\\my.txt");
// try {
// fin=new FileInputStream(f);
// } catch (Exception e) {
// e.printStackTrace();
// }
//第二种构造方式
fin=new FileInputStream(".\\file\\dog.jpg");
fos=new FileOutputStream(".\\file\\bb.jpg");
while((data=fin.read())!=-1) {
fos.write(data);
}
fos.close();
fin.close();
}
}