JAVA文件/文件夹操作实用函数

列举了一些这个项目中用到的文件/文件夹操作的函数,以备后用。

/**
* 计算文件夹大小
*/
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;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值