微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将SQlite中的数据以txt文件导出



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;

}

原文地址:https://www.jb51.cc/sqlite/198386.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐