手机访问文件系统(FileConnection)


import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.io.Connector;
import javax.microedition.io.PushRegistry;
import javax.microedition.io.file.FileConnection;


try {
FileConnection filecon = (FileConnection) Connector.open("file:///E:/videos/");
if (filecon.exists()) {
Consts.LOCAL_DIR = "file:///E:/videos/";
} else {
filecon.mkdir();
Consts.LOCAL_DIR = "file:///E:/videos/";
}
} catch (Exception e) {
Consts.LOCAL_DIR = System.getProperty("fileconn.dir.videos");
} finally {
if (filecon != null) {
try {
filecon.close();
} catch (IOException e) {

}
filecon = null;
}
}



import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import com.wondertek.controller.MainController;

public class FileUtil {

public static Vector getList(String path) {
FileConnection filecon = null;
try {
filecon = (FileConnection) (Connector.open(path));
if (filecon.exists()) {
Vector listVec = new Vector();
Enumeration en = filecon.list();
while (en.hasMoreElements()) {
String name = (String) en.nextElement();
if (name.endsWith(".3gp") || name.endsWith(".3GP")) {
listVec.addElement(name);
}
}
return listVec;
} else {
filecon.mkdir();
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (filecon != null)
filecon.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void deleteFile(String path, String name) {
FileConnection fc = null;
try {
fc = (FileConnection) (Connector.open(path + name));
if (fc.exists()) {
fc.delete();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fc != null)
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

public static String checkFileName(String fPath, String fName)
throws IOException {
boolean needCheck = true;
FileConnection tmp = null;
String newName = fName;
int i = 0;
while (needCheck) {
tmp = (FileConnection) Connector.open(fPath + newName);
if (tmp.exists()) {
newName = fName.substring(0, fName.indexOf('.')) + "(" + i
+ ")" + fName.substring(fName.indexOf('.'));
i++;
} else {
needCheck = false;
}
}
tmp.close();
tmp = null;
return newName;
}

public static void writeImage(String name, byte[] image) {
FileConnection fc_writeLog = null;
DataOutputStream dos = null;
try {
fc_writeLog = (FileConnection) Connector.open(Consts.LOCAL_DIR
+ name);
if (!fc_writeLog.exists()) {
fc_writeLog.create();
}
dos = new DataOutputStream(fc_writeLog.openOutputStream());
dos.write(image);
dos.flush();
dos.close();
dos = null;
fc_writeLog.close();
fc_writeLog = null;
} catch (IOException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
dos = null;
}
if (fc_writeLog != null) {
try {
fc_writeLog.close();
} catch (IOException e) {
e.printStackTrace();
}
fc_writeLog = null;
}
}
}

public static void writeLog(String str, String logAttributeName) {
FileConnection fc_writeLog = null;
InputStream is = null;
int pos;
DataOutputStream dos = null;
try {
fc_writeLog = (FileConnection) Connector.open(Consts.LOCAL_DIR
+ "log.txt");
if (!fc_writeLog.exists()) {
fc_writeLog.create();
pos = 0;
} else {
is = fc_writeLog.openInputStream();
int size = is.available();
byte[] posdata = new byte[size];
pos = is.read(posdata);
}
Calendar cal = Calendar.getInstance();
cal.getTime().toString();
dos = new DataOutputStream(fc_writeLog.openOutputStream(pos));
dos.writeUTF("#[" + cal.getTime().toString() + "|||***"
+ logAttributeName + "] : " + str + "\r\n");
is.close();
is = null;
dos.flush();
dos.close();
dos = null;
fc_writeLog.close();
fc_writeLog = null;
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
dos = null;
}
if (fc_writeLog != null) {
try {
fc_writeLog.close();
} catch (IOException e) {
e.printStackTrace();
}
fc_writeLog = null;
}
}
}

public static boolean checkDirSize(long fileSize) {
long dirOverSize = 0;
try {
MainController.filecon = (FileConnection) Connector
.open(Consts.LOCAL_DIR);
if (MainController.filecon.exists()
&& MainController.filecon.isDirectory()) {
dirOverSize = MainController.filecon.totalSize()
- MainController.filecon.usedSize() - 1024 * 1024;
}
if (fileSize >= dirOverSize) {
return false;
} else {
return true;
}
} catch (IOException e) {
return false;
} finally {
if (MainController.filecon != null) {
try {
MainController.filecon.close();
} catch (IOException e) {

}
MainController.filecon = null;
}
}
}
}



public static String LOCAL_DIR = "file:///E:/videos/";
FileUtil.deleteFile(Consts.LOCAL_DIR, currentDown.getSFileName()
+ ".tmp");
nameVec = FileUtil.getList(Consts.LOCAL_DIR);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值