列举了一些这个项目中用到的文件/文件夹操作的函数,以备后用。
/**
* 计算文件夹大小
*/
public static final long getDirectorySize( File dir ) {
long retSize = 0;
if ( ( dir == null ) || !dir.isDirectory() ) {
return retSize;
}
File[] entries = dir.listFiles();
int count = entries.length;
for ( int i = 0; i < count; i++ ) {
if ( entries[ i ].isDirectory() ) {
retSize += getDirectorySize( entries[ i ] );
} else {
retSize += entries[ i ].length();
}
}
return retSize;
}
/**
* 删除文件夹
*/
public static final boolean deleteDirectory( File dir ) {
boolean bRet = false;
if ( dir !=null && dir.isDirectory() ) {
File[] entries = dir.listFiles();
int sz = entries.length;
for ( int i = 0; i < sz; i++ ) {
if ( entries[ i ].isDirectory() ) {
deleteDirectory( entries[ i ] );
} else {
entries[ i ].delete();
}
}
dir.delete();
bRet = true;
}
return bRet;
}
/**
* 新建文件夹
*/
public static final void createDirectory( String strDir ) {
File file = new File( strDir );
if ( !file.isDirectory() ) {
file.mkdir();
}
}
/**
* 移动文件
*/
public static final void moveFile( String strOriginal, String strDest ) throws IOException {
try {
File fileOriginal = new File( strOriginal );
File fileDest = new File( strDest );
fileOriginal.renameTo( fileDest );
} catch ( IOException e ) {
throw new IOException("");
}
}
/**
* 复制文件
*/
public static final void copyFile( String strOriginal, String strDest ) throws IOException{
try {
File fileOriginal = new File( strOriginal );
File fileDest = new File( strDest );
FileInputStream fis = new FileInputStream( fileOriginal );
FileOutputStream fos = new FileOutputStream( fileDest );
byte[] bytes = new byte[ 1024 ];
int c;
while ( ( c = fis.read( bytes ) ) != -1 ) {
fos.write( bytes, 0, c );
}
fis.close();
fos.close();
} catch ( IOException e ) {
throw new IOException("");
}
}
/**
* 复制文件夹
*/
public static final boolean copyDirectory( String strOriginal, String strDest ) throws Exception {
boolean bRet = false;
File dirOriginal = new File( strOriginal );
if ( dirOriginal !=null && dirOriginal.isDirectory() ) {
createDirectory( strDest );
File dirDest = new File( strDest );
File[] entries = dirOriginal.listFiles();
int sz = entries.length;
for ( int i = 0; i < sz; i++ ) {
StringBuffer sbDest = new StringBuffer();
sbDest.append( dirDest.getPath() );
sbDest.append( "\" );
sbDest.append( entries[ i ].getName() );
if ( entries[ i ].isDirectory() ) {
strDest = dirDest.getPath() + "\" + entries[ i ].getName();
copyDirectory( entries[ i ].getPath(), sbDest.toString() );
} else {
copyFile( entries[ i ].getPath(), sbDest.toString() );
}
}
bRet = true;
}
return bRet;
}
/**
* 读取文件
*/
private String readFile( File file ) {
String text= "";
if ( file.exists() ) {
Reader reader = null;
try {
char[] tempchars = new char[1024];
int charread = 0;
reader = new InputStreamReader( new FileInputStream( file ), "UTF-8" );
while ( ( charread = reader.read( tempchars ) ) != -1 ) {
for ( int i = 0; i < charread; i++ ) {
if ( tempchars[i] == '\r' ) {
continue;
} else {
text += tempchars[i];
}
}
}
reader.close();
} catch ( IOException e ) {
throw new IOException("");
}
}
return text;
}
/**
* 写入文件
*/
private boolean writeFile( File file, String text ) {
try {
Writer writer = new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" );
writer.write( text );
writer.close();
return true;
} catch ( IOException e ) {
return false;
}
}
/**
* 文件夹中的文件按文件名排序(升序)
*/
File[] files = ( new File( strPictureDirectory ) ).listFiles();
File temp;
for ( int i = 0; i < files.length; i++ ) {
for ( int j = i; j < files.length; j++ ) {
if ( files[i].getName().compareTo( files[j].getName() ) > 0 ) {
temp = files[i];
files[i] = files[j];
files[j] = temp;
}
}
}
/**
* 计算文件夹大小
*/
public static final long getDirectorySize( File dir ) {
long retSize = 0;
if ( ( dir == null ) || !dir.isDirectory() ) {
return retSize;
}
File[] entries = dir.listFiles();
int count = entries.length;
for ( int i = 0; i < count; i++ ) {
if ( entries[ i ].isDirectory() ) {
retSize += getDirectorySize( entries[ i ] );
} else {
retSize += entries[ i ].length();
}
}
return retSize;
}
/**
* 删除文件夹
*/
public static final boolean deleteDirectory( File dir ) {
boolean bRet = false;
if ( dir !=null && dir.isDirectory() ) {
File[] entries = dir.listFiles();
int sz = entries.length;
for ( int i = 0; i < sz; i++ ) {
if ( entries[ i ].isDirectory() ) {
deleteDirectory( entries[ i ] );
} else {
entries[ i ].delete();
}
}
dir.delete();
bRet = true;
}
return bRet;
}
/**
* 新建文件夹
*/
public static final void createDirectory( String strDir ) {
File file = new File( strDir );
if ( !file.isDirectory() ) {
file.mkdir();
}
}
/**
* 移动文件
*/
public static final void moveFile( String strOriginal, String strDest ) throws IOException {
try {
File fileOriginal = new File( strOriginal );
File fileDest = new File( strDest );
fileOriginal.renameTo( fileDest );
} catch ( IOException e ) {
throw new IOException("");
}
}
/**
* 复制文件
*/
public static final void copyFile( String strOriginal, String strDest ) throws IOException{
try {
File fileOriginal = new File( strOriginal );
File fileDest = new File( strDest );
FileInputStream fis = new FileInputStream( fileOriginal );
FileOutputStream fos = new FileOutputStream( fileDest );
byte[] bytes = new byte[ 1024 ];
int c;
while ( ( c = fis.read( bytes ) ) != -1 ) {
fos.write( bytes, 0, c );
}
fis.close();
fos.close();
} catch ( IOException e ) {
throw new IOException("");
}
}
/**
* 复制文件夹
*/
public static final boolean copyDirectory( String strOriginal, String strDest ) throws Exception {
boolean bRet = false;
File dirOriginal = new File( strOriginal );
if ( dirOriginal !=null && dirOriginal.isDirectory() ) {
createDirectory( strDest );
File dirDest = new File( strDest );
File[] entries = dirOriginal.listFiles();
int sz = entries.length;
for ( int i = 0; i < sz; i++ ) {
StringBuffer sbDest = new StringBuffer();
sbDest.append( dirDest.getPath() );
sbDest.append( "\" );
sbDest.append( entries[ i ].getName() );
if ( entries[ i ].isDirectory() ) {
strDest = dirDest.getPath() + "\" + entries[ i ].getName();
copyDirectory( entries[ i ].getPath(), sbDest.toString() );
} else {
copyFile( entries[ i ].getPath(), sbDest.toString() );
}
}
bRet = true;
}
return bRet;
}
/**
* 读取文件
*/
private String readFile( File file ) {
String text= "";
if ( file.exists() ) {
Reader reader = null;
try {
char[] tempchars = new char[1024];
int charread = 0;
reader = new InputStreamReader( new FileInputStream( file ), "UTF-8" );
while ( ( charread = reader.read( tempchars ) ) != -1 ) {
for ( int i = 0; i < charread; i++ ) {
if ( tempchars[i] == '\r' ) {
continue;
} else {
text += tempchars[i];
}
}
}
reader.close();
} catch ( IOException e ) {
throw new IOException("");
}
}
return text;
}
/**
* 写入文件
*/
private boolean writeFile( File file, String text ) {
try {
Writer writer = new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" );
writer.write( text );
writer.close();
return true;
} catch ( IOException e ) {
return false;
}
}
/**
* 文件夹中的文件按文件名排序(升序)
*/
File[] files = ( new File( strPictureDirectory ) ).listFiles();
File temp;
for ( int i = 0; i < files.length; i++ ) {
for ( int j = i; j < files.length; j++ ) {
if ( files[i].getName().compareTo( files[j].getName() ) > 0 ) {
temp = files[i];
files[i] = files[j];
files[j] = temp;
}
}
}