黑马程序员--IO---FIle类常用方法
2014-02-18 23:19
429 查看
File类(文件和目录路径名的抽象表示形式):
1.用来将文件或者文件夹封装成对象
2.方便对文件和文件夹进行操作
3.File对象可以作为参数传递给流的构造函数
4.了解File类中的常用方法
构造一个File对象
package com.mth.file;
import java.io.File;
/**
*
* @ClassName: FileTest
* @Description: 构造一个file对象
* @author mth 75100313@qq.com
* @date 2014-2-18 下午06:59:11
*
*/
public class FileTest {
/**
* 创建File对象
*
*/
public static void consMethod() {
// 将a.txt封装成对象。可以将已有的和未出现的文件和文件夹封装成文件。
File file = new File("a.txt");
File file2 = new File("e:\\abc\\a.txt");
// parent父目录 child 文件名
File file3 = new File("e:\\abc", "a.txt");
// File.separator 代表分隔符 跨平台
File file4 = new File("e:" + File.separator + "abc" + File.separator
+ "a.txt");
}
public static void main(String[] args) {
consMethod();
}
}
File类常见方法:
package com.mth.file;
import java.io.File;
import java.io.IOException;
/**
*
* @ClassName: FileTest1
* @Description: 创建文件
* @author mth 75100313@qq.com
* @date 2014-2-18 下午07:40:32
* File类常见方法:
* 1.创建
* boolean createNewFile() 在指定位置创建 如果不存在 则建立 返回 true;如果存在 则不创建,返回false。(合理)
* 和输出流不一样,输出流对象一建立创建文件。而且文件如果存在,则覆盖。
* boolean mkdir() 创建文件夹
* boolean mkdirs() 创建多级文件夹
* 2.删除
* boolean delete() 删除失败返回false。
* void deleteOnExit() 在程序,退出时删除指定文件。
* 3.判断
* boolean exists() 判断文件或目录是否存在。
* boolean isDirectory() 是否是目录。
* boolean isFile() 是否是文件。
* boolean isHidden() 是否隐藏 。
* boolean isAbsolute() 是否是绝对路径
* 4.获取信息
* String getName() 获取文件或目录的名称
* String getPath() 获取相对路径名
* String getParent() 获取父目录名称 ;如果没有父目录,返回null
* String getAbsolutePath() 获取绝对路径名
* long lastModified() 获取文件最后一次修改的时间
* long length()
*/
public class FileTest1 {
/*
* 创建和删除
* */
public static void method_1() throws IOException {
File file = new File("e:/file.txt");
//创建文件
file.createNewFile();
//删除文件
file.delete();
//创建文件夹abcd
File dir=new File("abcd\\abcd");
//mkdir()只能创建一级目录
boolean b=dir.mkdir();
System.out.println("dir:"+b);
//创建多级文件夹ss
File dirs=new File("ss\\ss\\ss\\ss\\ss");
//mkdirs()创建多级目录
boolean bs=dirs.mkdirs();
System.out.println("dirs:"+bs);
}
/*
* 判断
*
* */
public static void method_2() throws IOException{
File file=new File("abc.txt");
file.createNewFile();
System.out.println("file:"+file.exists());
}
public static void method_3() throws IOException{
File file=new File("method_3.txt");
//记住在判断文件对象是否是文件或者目录时,必须要求先判断该文件对象封装的内容是否存在、
//通过exists()来判断
System.out.println("是否存在?:"+file.exists());
System.out.println("dir:"+file.isDirectory());
}
/*
*
* 获取信息
*
* */
public static void method_4() throws IOException{
File file=new File("f://method_4.txt");
file.createNewFile();
/*
* f:\abc\method_4.txt
f:\abc\method_4.txt
f:\abc
* */
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
//刚方法返回的是绝对路径中的父目录 如果获取的是相对路径,返回null
//如果相对路径中有上一层目录 即返回该目录
System.out.println(file.getParent());
System.out.println(file.lastModified());
System.out.println(file.length());
}
/*
*
* boolean renameTo(File dest) 重命名
*
*
* */
public static void method_5() throws IOException{
//相当于剪切同时把名字更改了
File file1=new File("f:/method_5.txt");
File file2=new File("e:/method_rename.txt");
boolean b=file1.renameTo(file2);
System.out.println(b);
}
public static void main(String[] args) throws IOException {
method_5();
}
}
1.用来将文件或者文件夹封装成对象
2.方便对文件和文件夹进行操作
3.File对象可以作为参数传递给流的构造函数
4.了解File类中的常用方法
构造一个File对象
package com.mth.file;
import java.io.File;
/**
*
* @ClassName: FileTest
* @Description: 构造一个file对象
* @author mth 75100313@qq.com
* @date 2014-2-18 下午06:59:11
*
*/
public class FileTest {
/**
* 创建File对象
*
*/
public static void consMethod() {
// 将a.txt封装成对象。可以将已有的和未出现的文件和文件夹封装成文件。
File file = new File("a.txt");
File file2 = new File("e:\\abc\\a.txt");
// parent父目录 child 文件名
File file3 = new File("e:\\abc", "a.txt");
// File.separator 代表分隔符 跨平台
File file4 = new File("e:" + File.separator + "abc" + File.separator
+ "a.txt");
}
public static void main(String[] args) {
consMethod();
}
}
File类常见方法:
package com.mth.file;
import java.io.File;
import java.io.IOException;
/**
*
* @ClassName: FileTest1
* @Description: 创建文件
* @author mth 75100313@qq.com
* @date 2014-2-18 下午07:40:32
* File类常见方法:
* 1.创建
* boolean createNewFile() 在指定位置创建 如果不存在 则建立 返回 true;如果存在 则不创建,返回false。(合理)
* 和输出流不一样,输出流对象一建立创建文件。而且文件如果存在,则覆盖。
* boolean mkdir() 创建文件夹
* boolean mkdirs() 创建多级文件夹
* 2.删除
* boolean delete() 删除失败返回false。
* void deleteOnExit() 在程序,退出时删除指定文件。
* 3.判断
* boolean exists() 判断文件或目录是否存在。
* boolean isDirectory() 是否是目录。
* boolean isFile() 是否是文件。
* boolean isHidden() 是否隐藏 。
* boolean isAbsolute() 是否是绝对路径
* 4.获取信息
* String getName() 获取文件或目录的名称
* String getPath() 获取相对路径名
* String getParent() 获取父目录名称 ;如果没有父目录,返回null
* String getAbsolutePath() 获取绝对路径名
* long lastModified() 获取文件最后一次修改的时间
* long length()
*/
public class FileTest1 {
/*
* 创建和删除
* */
public static void method_1() throws IOException {
File file = new File("e:/file.txt");
//创建文件
file.createNewFile();
//删除文件
file.delete();
//创建文件夹abcd
File dir=new File("abcd\\abcd");
//mkdir()只能创建一级目录
boolean b=dir.mkdir();
System.out.println("dir:"+b);
//创建多级文件夹ss
File dirs=new File("ss\\ss\\ss\\ss\\ss");
//mkdirs()创建多级目录
boolean bs=dirs.mkdirs();
System.out.println("dirs:"+bs);
}
/*
* 判断
*
* */
public static void method_2() throws IOException{
File file=new File("abc.txt");
file.createNewFile();
System.out.println("file:"+file.exists());
}
public static void method_3() throws IOException{
File file=new File("method_3.txt");
//记住在判断文件对象是否是文件或者目录时,必须要求先判断该文件对象封装的内容是否存在、
//通过exists()来判断
System.out.println("是否存在?:"+file.exists());
System.out.println("dir:"+file.isDirectory());
}
/*
*
* 获取信息
*
* */
public static void method_4() throws IOException{
File file=new File("f://method_4.txt");
file.createNewFile();
/*
* f:\abc\method_4.txt
f:\abc\method_4.txt
f:\abc
* */
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
//刚方法返回的是绝对路径中的父目录 如果获取的是相对路径,返回null
//如果相对路径中有上一层目录 即返回该目录
System.out.println(file.getParent());
System.out.println(file.lastModified());
System.out.println(file.length());
}
/*
*
* boolean renameTo(File dest) 重命名
*
*
* */
public static void method_5() throws IOException{
//相当于剪切同时把名字更改了
File file1=new File("f:/method_5.txt");
File file2=new File("e:/method_rename.txt");
boolean b=file1.renameTo(file2);
System.out.println(b);
}
public static void main(String[] args) throws IOException {
method_5();
}
}
相关文章推荐
- 黑马程序员——Java学习总结:IO常用方法练习
- 黑马程序员——Java学习总结:IO常用方法练习
- 黑马程序员_IO输入输出_常用的类和方法
- j 4000 ava IO的File类常用方法总结一
- java IO的File类常用方法总结二
- 黑马程序员_.NET Path类 File类 常用方法
- 黑马程序员学习笔记——File类的基础方法
- 黑马程序员——java之IO体系中File类、编码问题、对象序列化
- 黑马程序员——Java语言基础——02.java语言基础组成(4)数组及常用排序方法
- 黑马程序员——Java基础---IO(二)--File类、Properties类、打印流、序列流(合并流)
- (java)中file类的常用方法
- 黑马程序员之ASP.NET学习笔记: 几个常用方法有效优化ASP.NET的性能
- 黑马程序员-反射的常用方法
- IO_File_常用方法_文件名_判断_长度_创建_删除JAVA144
- java io 常用方法
- 黑马程序员——IO——File、常用流对象演示
- 黑马程序员--数组操作的常用方法
- 黑马程序员-IO的概述和File方法
- 黑马程序员_日记21_JavaString常用方法
- java中File类的常用所有方法及其应用