为什么这里会出错???
“Exception in thread "main" java.lang.NullPointerException
”
import java.io.*;
import java.util.*;
class CopyFile
{
String newFileDir; //新的路径
String srcFileDir; //原来的目录
String desFileDir; //新的目录
File srcFile;
CopyFile( String srcFileDir,String desFileDir )
{
this.srcFileDir=srcFileDir;
this.desFileDir=desFileDir;
srcFile=new File(srcFileDir);
}
public void start( ) throws Exception
{
getList( srcFile );
System.out.println( "d" );
}
private void getList( File f ) throws Exception
{
File fileList[]=f.listFiles();
for( int i=0; i<fileList.length; i++ )
{
if( fileList.isDirectory() )
{
copyDirectory( fileList );
getList( fileList ) ;
}
else
{
copyFile( fileList );
}
}
}
private void copyDirectory( File f )
{
File desDirectory=new File( replacePath(f));
if( !(desDirectory.exists()))
{
desDirectory.mkdir();
}
//System.out.println( desDirectory.getAbsolutePath() );
}
private void copyFile( File f ) throws Exception
{
FileOutputStream fos=
new FileOutputStream( f );
File newFile=new File( replacePath(f ));
newFile.createNewFile();
FileInputStream fis=
new FileInputStream( newFile );
byte buf[]=new byte[1024];
int num=0;
while( (num=fis.read(buf))!=-1 )
{
fos.write(buf,0,num);
}
fos.close();
fis.close();
//System.out.println( replacePath(f ) );
}
private String replacePath( File f )
{
String s=f.getAbsolutePath();
System.out.println( s );
s=s.replace( srcFileDir,desFileDir );
System.out.println( desFileDir );
System.out.println( srcFileDir );
System.out.println( s );
System.out.println( " " );
return s;
}
}
class Demo
{
public static void main(String args[]) throws Exception
{
new CopyFile( "E:\\zjs\\day22","D:\\zjs").start();
}
}
[ 本帖最后由 gdky005 于 2011-07-19 10:47 编辑 ] |