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

 

你可能感兴趣的:(java,dos,BlackBerry)