package com.dw.contactadapter.base;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.io.Connector;
public class FileOperater {
public static String path = null; // 存放联系人文件的路径
private static final int SIZE_INPUT_BUFFER = 8 * 1024; //设缓存为8K
String platform = null; // 平台类型
private static FileOperater fo = null;
/**
* 设定路径
*
*/
private FileOperater() {
System.out.println("public FileOperater() {}");
//platform = System.getProperty("microedtion.platform");
// if (platform.startsWith("Nokia")) {
// path = "c:/nokiaContact.txt";
// }
//path = readPath() + "coolpadContact.txt";
//System.out.println("public FileOperater()2 {}");
}
public static FileOperater getInstance() {
if (fo == null) {
fo = new FileOperater();
path = readPath();
}
return fo;
}
/**
* 保存文件
* @param path 文件路径
* @param fileData 文件数据
* @param overrite 是否要强制覆盖
* @return 1:成功写入文件 -1:文件已经存在没有重写 0:写入文件时出错
* 2008-3-3 下午05:42:43
*/
public int saveFile(String path, byte[] fileData, boolean overrite) {
try {
FileConnection fc = (FileConnection) (Connector.open(path));
if (fc.exists()) {
if (overrite) {
fc.delete(); //如果文件已经存在,先删除
fc.create();
fc.setWritable(true);
OutputStream os = fc.openOutputStream();
os.write(fileData);
os.close();
return 1;
} else
return -1;
} else {
fc.create();
fc.setWritable(true);
OutputStream os = fc.openOutputStream();
os.write(fileData);
os.close();
return 1;
}
} catch (Exception e) {
System.out.println("saveFileErr:" + e.getMessage());
e.printStackTrace();
return 0;
}
}
/**
* 读取文件 返回String类型
* @param path path中要包含文件名
* 2008-2-25 上午10:45:18
* zhangdezheng
*/
public String readFile (String path) {
byte[] buf = new byte[SIZE_INPUT_BUFFER];
StringBuffer sb = new StringBuffer();
int c = 0;
int b = 0;
try {
FileConnection fc = (FileConnection)Connector.open(path);
if (fc.exists()) {
InputStream in = fc.openInputStream();
while ((c < buf.length) && ((b = in.read(buf, c, buf.length - c)) >= 0)) {
c += b;
if (c == SIZE_INPUT_BUFFER) {
String tmp = new String(buf);
sb.append(tmp);
tmp = null;
buf = new byte[SIZE_INPUT_BUFFER];
c = 0;
}
}
if (c != 0) {
String tp = new String (buf,0,c);
//System.out.println("tp: " + tp);
sb.append(tp);
}
} else {
System.out.println("fc isn't exists!");
}
} catch (IOException e) {
e.printStackTrace();
}
String content = new String(sb);
System.out.println("file content: " + content);
return content;
}
/**
* 获取路径方法
* zhangdezheng
* @return the base path
* 2008-3-5 上午11:00:51
*/
private static String readPath () {
String path = "file:///";//文件所在路径
Enumeration enumRoot = FileSystemRegistry.listRoots();// 返回根目录组成的序列
Vector list = new Vector();
String root = "";
while (enumRoot.hasMoreElements()) {
list.addElement(enumRoot.nextElement());
}
String[] ret = null;
if (list.size() > 0) {
ret = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
ret[i] = (String) list.elementAt(i);
}
}
for (int i = 0; i < ret.length; i++) {
root = ret[i];
}
//path = path + root + "coolpadContact.txt";
path = path + root;
//this.path = path;
return path;
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.io.Connector;
public class FileOperater {
public static String path = null; // 存放联系人文件的路径
private static final int SIZE_INPUT_BUFFER = 8 * 1024; //设缓存为8K
String platform = null; // 平台类型
private static FileOperater fo = null;
/**
* 设定路径
*
*/
private FileOperater() {
System.out.println("public FileOperater() {}");
//platform = System.getProperty("microedtion.platform");
// if (platform.startsWith("Nokia")) {
// path = "c:/nokiaContact.txt";
// }
//path = readPath() + "coolpadContact.txt";
//System.out.println("public FileOperater()2 {}");
}
public static FileOperater getInstance() {
if (fo == null) {
fo = new FileOperater();
path = readPath();
}
return fo;
}
/**
* 保存文件
* @param path 文件路径
* @param fileData 文件数据
* @param overrite 是否要强制覆盖
* @return 1:成功写入文件 -1:文件已经存在没有重写 0:写入文件时出错
* 2008-3-3 下午05:42:43
*/
public int saveFile(String path, byte[] fileData, boolean overrite) {
try {
FileConnection fc = (FileConnection) (Connector.open(path));
if (fc.exists()) {
if (overrite) {
fc.delete(); //如果文件已经存在,先删除
fc.create();
fc.setWritable(true);
OutputStream os = fc.openOutputStream();
os.write(fileData);
os.close();
return 1;
} else
return -1;
} else {
fc.create();
fc.setWritable(true);
OutputStream os = fc.openOutputStream();
os.write(fileData);
os.close();
return 1;
}
} catch (Exception e) {
System.out.println("saveFileErr:" + e.getMessage());
e.printStackTrace();
return 0;
}
}
/**
* 读取文件 返回String类型
* @param path path中要包含文件名
* 2008-2-25 上午10:45:18
* zhangdezheng
*/
public String readFile (String path) {
byte[] buf = new byte[SIZE_INPUT_BUFFER];
StringBuffer sb = new StringBuffer();
int c = 0;
int b = 0;
try {
FileConnection fc = (FileConnection)Connector.open(path);
if (fc.exists()) {
InputStream in = fc.openInputStream();
while ((c < buf.length) && ((b = in.read(buf, c, buf.length - c)) >= 0)) {
c += b;
if (c == SIZE_INPUT_BUFFER) {
String tmp = new String(buf);
sb.append(tmp);
tmp = null;
buf = new byte[SIZE_INPUT_BUFFER];
c = 0;
}
}
if (c != 0) {
String tp = new String (buf,0,c);
//System.out.println("tp: " + tp);
sb.append(tp);
}
} else {
System.out.println("fc isn't exists!");
}
} catch (IOException e) {
e.printStackTrace();
}
String content = new String(sb);
System.out.println("file content: " + content);
return content;
}
/**
* 获取路径方法
* zhangdezheng
* @return the base path
* 2008-3-5 上午11:00:51
*/
private static String readPath () {
String path = "file:///";//文件所在路径
Enumeration enumRoot = FileSystemRegistry.listRoots();// 返回根目录组成的序列
Vector list = new Vector();
String root = "";
while (enumRoot.hasMoreElements()) {
list.addElement(enumRoot.nextElement());
}
String[] ret = null;
if (list.size() > 0) {
ret = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
ret[i] = (String) list.elementAt(i);
}
}
for (int i = 0; i < ret.length; i++) {
root = ret[i];
}
//path = path + root + "coolpadContact.txt";
path = path + root;
//this.path = path;
return path;
}
}