获取当前项目路径
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("a.txt");
System.out.println(file.getAbsolutePath());
file = new File("/TestProject/a.txt");
System.out.println(file.getName());
System.out.println(file.getParent());
System.out.println(file.getAbsolutePath());
file = new File("");
System.out.println(file.getCanonicalPath());
System.out.println(file.getParent());
System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径
}
文件判断
- exists()
- canWrite()
- canRead()
- isFile()
- isDirectory()
- isAbsolute():消除平台差异,ie以盘符开头,其他以/开头
- length() 长度 字节数 不能读取文件夹的长度
- createNewFile() 不存在创建新文件,存在返回false
- delete() 删除文件
static createTempFile(前缀3个字节长,后缀默认.temp) 默认临时空间
staticcreateTempFile(前缀3个字节长,后缀默认.temp,目录)
deleteOnExit() 退出虚拟机删除,常用于删除临时文件
//创建删除文件
public static void test3() throws IOException, InterruptedException{
//createNewFile() 不存在创建新文件
//String path="E:/xp/test/con"; //con系统关键字
String path="E:/xp/test/200.jpg";
//String path="E:/xp/test/1.jpg";
File src =new File(path);
if(!src.exists()){
boolean flag =src.createNewFile();
System.out.println(flag?"成功":"失败");
}
//删除文件
boolean flag =src.delete();
System.out.println(flag?"成功":"失败");
//static createTempFile(前缀3个字节长,后缀默认.temp) 默认临时空间
//static createTempFile(前缀3个字节长,后缀默认.temp,目录)
File temp= File.createTempFile("tes", ".temp",new File("e:/xp/test"));
Thread.sleep(10000);
temp.deleteOnExit(); //退出即删除
}
文件夹操作
- mkdir() // 创建目录,必须确保父目录存在,如果不存在,创建失败
- mkdirs 创建目录,父目录链不存在一同创建
- listFiles // 获取子文件File对象
File src = new File("src/com/cai/gen01");
if(src.isDirectory()) {
String[] subNames = src.list();
for(String temp : subNames) {
System.out.println(temp);
}
}
// 命令设计模式
File[] subFiles = src.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(".java");
}
});
for(File temp : subFiles) {
System.out.println(temp.getAbsolutePath());
}
遍历文件
public static void main(String[] args) {
// TODO Auto-generated method stub
String path = "src";
File parent = new File(path);
printName(parent);
}
/**
* 输出路径
*/
public static void printName(File src) {
if(null == src || !src.exists()) {
return;
}
System.out.println(src.getAbsolutePath());
if(src.isDirectory()) {
for(File sub : src.listFiles()) {
printName(sub);
}
}
}
字节流
输入流
// TODO Auto-generated method stub
File src = new File("src/com/cai/io/Demo01.java");
InputStream is = null;
try {
is = new FileInputStream(src);
byte[] car = new byte[10];
int len = 0;
while(-1!=(len = is.read(car))) {
String info = new String(car, 0, len);
System.out.print(info);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
输出流
File file = new File("src/com/cai/io/one.txt");
OutputStream os = null;
try {
os= new FileOutputStream(file,true);
String str = "添加新行";
os.write(str.getBytes());
os.flush();
} catch (IOException e) {
// TODO: handle exception
}
复制文件
File src = new File("src/com/cai/io/one.txt");
File dest = new File("src/com/cai/io/one1.txt");
if(!src.isFile()) {
System.out.println("文件夹不能拷贝");
return;
}
try {
InputStream is = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] flush = new byte[1024];
int len = 0;
while(-1!=(len=is.read(flush))) {
out.write(flush, 0, len);
}
out.flush();
out.close();
is.close();
} catch (IOException e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
复制文件夹
复制文件时目标文件重名问题,和文件夹重名失败,和文件重名自动覆盖
/**
* @param args
*/
public static void main(String[] args) {
//源目录
String srcPath="E:/xp/test/a";
//目标目录
String destPath="E:/xp/test/a/b";
try {
FileUtil.copyDir(srcPath,destPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 拷贝文件夹
* @param src 源路径
* @param dest 目标路径
*/
public static void copyDir(String srcPath,String destPath){
File src=new File(srcPath);
File dest =new File(destPath);
copyDir(src,dest);
}
/**
* 拷贝文件夹
* @param src 源File对象
* @param dest 目标File对象
*/
public static void copyDir(File src,File dest){
if(src.isDirectory()){ //文件夹
dest =new File(dest,src.getName());
}
copyDirDetail(src,dest);
}
/**
* 拷贝文件夹细节
* @param src
* @param dest
*/
public static void copyDirDetail(File src,File dest){
if(src.isFile()){ //文件
try {
FileUtil.copyFile(src, dest);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if(src.isDirectory()){ //文件夹
//确保目标文件夹存在
dest.mkdirs();
//获取下一级目录|文件
for(File sub:src.listFiles()){
copyDirDetail(sub,new File(dest,sub.getName()));
}
}
}
字符流
//创建源
File src =new File("E:/xp/test/a.txt");
//选择流
Reader reader =null;
try {
reader =new FileReader(src);
//读取操作
char[] flush =new char[1024];
int len =0;
while(-1!=(len=reader.read(flush))){
//字符数组转成 字符串
String str =new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("源文件不存在");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取失败");
}finally{
try {
if (null != reader) {
reader.close();
}
} catch (Exception e2) {
}
}
拷贝文件
//创建源 仅限于 字符的纯文本
File src =new File("E:/xp/test/Demo03.java");
File dest =new File("e:/xp/test/char.txt");
//选择流
Reader reader =null;
Writer wr =null;
try {
reader =new FileReader(src);
wr =new FileWriter(dest);
//读取操作
char[] flush =new char[1024];
int len =0;
while(-1!=(len=reader.read(flush))){
wr.write(flush, 0, len);
}
wr.flush();//强制刷出
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("源文件不存在");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取失败");
}finally{
try {
if (null != wr) {
wr.close();
}
} catch (Exception e2) {
}
try {
if (null != reader) {
reader.close();
}
} catch (Exception e2) {
}
}