欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

[io]blackberry资料操作实例

更新时间: 2014-01-05 02:42:31 责任编辑: Author_N1

 

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

 

~

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 02:42:31
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/13385/
本文WWW.DOC100.NET DOC100.NET版权所有。