File类
File类和四大家族没有关系,所以File类不能完成文件的读和写。 File对象代表什么? 文件和目录路径名的抽象表示形式。 C:\Drivers这是一个File对象 C:\Drivers\Lan\Realtek\Readme.txt也是File对象。 一个File对象有可能对应的是目录,也可能是文件。 File只是一个路径名的抽象表示形式。 3、File类中常用的方法
public class FileTest01 {
public static void main ( String[ ] args) throws Exception{
File f1= new File ( "F:\\file" ) ;
System. out. println ( f1. exists ( ) ) ;
File f2= new File ( "F:\\a\\b" ) ;
File f3= new File ( "F:\\java\\test\\log.txt" ) ;
String parentPath = f3. getParent ( ) ;
System. out. println ( parentPath) ;
File parentFile= f3. getParentFile ( ) ;
System. out. println ( "获取绝对路径:" + parentFile. getAbsolutePath ( ) ) ;
File f4= new File ( "copy" ) ;
System. out. println ( "获取绝对路径:" + f4. getAbsolutePath ( ) ) ;
}
}
public class FileTest02 {
public static void main ( String[ ] args) {
File f1= new File ( "F:\\java\\test\\copy02.java" ) ;
System. out. println ( "文件名:" + f1. getName ( ) ) ;
System. out. println ( f1. isDirectory ( ) ) ;
System. out. println ( f1. isFile ( ) ) ;
long haoMiao = f1. lastModified ( ) ;
Date time = new Date ( haoMiao) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss SSS" ) ;
String strTime = sdf. format ( time) ;
System. out. println ( strTime) ;
System. out. println ( f1. length ( ) ) ;
}
}
public class FileTest03 {
public static void main ( String[ ] args) {
File f = new File ( "F:\\java\\test" ) ;
File[ ] files = f. listFiles ( ) ;
for ( File file : files) {
System. out. println ( file. getName ( ) ) ;
}
}
}
目录的拷贝
public class CopyAll {
public static void main ( String[ ] args) {
File srcFile = new File ( "F:\\java" ) ;
File destFile = new File ( "D:\\" ) ;
copyDir ( srcFile, destFile) ;
}
private static void copyDir ( File srcFile, File destFile) {
if ( srcFile. isFile ( ) ) {
FileInputStream in= null;
FileOutputStream out= null;
try {
in= new FileInputStream ( srcFile) ;
String path= ( destFile. getAbsolutePath ( ) . endsWith ( "\\" ) ? destFile. getAbsolutePath ( ) : destFile. getAbsolutePath ( ) + "\\" ) + srcFile. getAbsolutePath ( ) . substring ( 3 ) ;
out= new FileOutputStream ( path) ;
byte [ ] bytes= new byte [ 1024 * 1024 ] ;
int readCount= 0 ;
while ( ( readCount= in. read ( bytes) ) != - 1 ) {
out. write ( bytes, 0 , readCount) ;
}
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( out!= null) {
try {
out. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
if ( in != null) {
try {
in. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
return ;
}
File[ ] files = srcFile. listFiles ( ) ;
for ( File file : files) {
if ( file. isDirectory ( ) ) {
String srcDir= file. getAbsolutePath ( ) ;
String destDir= ( destFile. getAbsolutePath ( ) . endsWith ( "\\" ) ? destFile. getAbsolutePath ( ) : destFile. getAbsolutePath ( ) + "\\" ) + srcDir. substring ( 3 ) ;
File newFile= new File ( destDir) ;
if ( ! newFile. exists ( ) ) {
newFile. mkdirs ( ) ;
}
}
copyDir ( file, destFile) ;
}
}
}