String filename =fileName();
initData(filename);
@SuppressLint("SimpleDateFormat")
public void initData(String f1) {
File f = new File(f1);
if (f.exists()) {
f.delete();
}
file = new File(getSDPath() + "/Scan");
makeDir(file);
TxtUtils.writeTxt(f1,"条码"+" "+"数量"+"\n");
//将从数据库中读取的数据写入txt文件中 cursor = databaSEOperation.query_db();
if(cursor.moveToFirst()){
do{
String code=cursor.getString(cursor.getColumnIndex("code"));
String num=cursor.getString(cursor.getColumnIndex("num"));
TxtUtils.writeTxt(f1,code+","+" "+num+"\n");
}while (cursor.moveToNext());
}
}
public static void makeDir(File dir) {
if (!dir.getParentFile().exists()) {
makeDir(dir.getParentFile());
}
dir.mkdir();
}
public static void writeTxt(String fileName,String content) {
try
{ //要指定编码方式,否则会出现乱码 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(fileName,true),"gbk");
osw.write(content);
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private String fileName(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-hh-mm");
String date=sdf.format(new java.util.Date());
String filename="/mnt/sdcard/Scan/"+date+".txt";
return filename;
}
public String getSDPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();
}
String dir = sdDir.toString();
return dir;
}