首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > BlackBerry >

[IO]BlackBerry资料操作实例

2012-09-08 
[IO]BlackBerry文件操作实例?文件操作实例import java.io.DataOutputStreamimport java.io.IOExceptioni

[IO]BlackBerry文件操作实例

?

文件操作实例

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;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;}}}}

?

~

热点排行