java.io包里的类:
InputStream/OutputStream
: 以字节为单位读写文件内容,一次读/写一个字节,常用于读二进制文件,如图片、音频、视频等文件FileInputStream/FileOutputStream
: 以字节为单位读写文件内容,一次读写多个字节Reader/Writer
: 以字符为单位,一次读写一个字符BufferedReader/BufferedWriter
: 以行为单位,一次读写一行,以换行符为结束标志
java.nio (new i/o)(jdk 1.4加入)
特点:非阻塞
创建方法
- boolean createNewFile() 不存在返回true 存在返回false
- boolean mkdir() 创建目录
- boolean mkdirs() 创建多级目录
删除方法
- boolean delete()
- boolean deleteOnExit() 文件使用完成后删除
判断方法
- boolean canExecute()判断文件是否可执行
- boolean canRead()判断文件是否可读
- boolean canWrite() 判断文件是否可写
- boolean exists() 判断文件是否存在
- boolean isDirectory() 判断是否是一个目录
- boolean isFile() 判断是否是一个文件
- boolean isHidden()8.boolean isAbsolute()判断是否是绝对路径 文件不存在也能判断
获取方法
- String getName() 返回文件名
- String getPath()
- String getAbsolutePath()
- String getParent()//如果没有父目录返回null
- long lastModified()//获取最后一次修改的时间
- long length()
- boolean renameTo(File f)
- File[] liseRoots()//获取机器盘符
- String[] list()
- String[] list(FilenameFilter filter)
具体文件操作代码:
-
创建文件/文件夹 :
//创建文件夹
public static void createFolder(String path){
File file = new File(path);
file.mkdirs();
}
//创建文件
public static boolean createFile(String fileName){
File file = new File(fileName);
System.out.println(file.getAbsolutePath());
boolean isSucess = false;
try {
isSucess = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return isSucess;
}
-
写文件:
//写入文件
public static void writeFile(String fileName){
File file = new File(fileName);
BufferedWriter bWriter = null;
try {
FileWriter writer = new FileWriter(file);
bWriter = new BufferedWriter(writer);
bWriter.write("hello ");
bWriter.newLine();
bWriter.write('a');
bWriter.write(27);
bWriter.write("world");
} catch (IOException e) {
e.printStackTrace();
}finally{
if (bWriter != null) {
try {
bWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
-
读文件:
//读取文件
public static void readFile(String fileName){
File file = new File(fileName);
BufferedReader bReader = null;
try {
FileReader reader = new FileReader(file);
bReader = new BufferedReader(reader);
String line = bReader.readLine();
while(line != null){
System.out.println(line);
line = bReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
try {
if (bReader != null) {
bReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
-
列出当前目录下所有文件:
//列出当前目录下的所有文件
public static void listFile(String folderPath){
File file = new File(folderPath);
for(String fileName : file.list()){
System.out.println("fileName : "+ fileName);
}
}
-
删除文件:
//删除文件
public static void delFile(String folderPath){
File file = new File(folderPath);
System.out.println(folderPath);
file.delete();
}
-
拷贝文件:
- 方法一:
public static void copyFile(String oldPath, String newPath){
File oldFile = new File(oldPath);
File newFile = new File(newPath);
BufferedReader bReader = null;
BufferedWriter bWriter = null;
try {
FileReader fr = new FileReader(oldFile);
bReader = new BufferedReader(fr);
String all = "";
String line = bReader.readLine();
System.out.println("拷贝中。。。请稍候");
while(line != null){
all += line;
line = bReader.readLine();
}
FileWriter fw = new FileWriter(newFile);
bWriter = new BufferedWriter(fw);
bWriter.write(all);
System.out.println("完成!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
try {
bReader.close();
bWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 方法二:
public static void copyFile2(File source, File dest) throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
System.out.println("拷贝中。。。请稍候");
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
System.out.println("完成!");
} finally {
input.close();
output.close();
}
}
- 方法三:
public static void copyFiles(File source, File dest){
try {
System.out.println("拷贝中。。。请稍候");
Files.copy(source.toPath(), dest.toPath());
System.out.println("完成!");
} catch (IOException e) {
e.printStackTrace();
}
}