Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将光标导出到CSV文件。CSV文件丢失_Android_Csv_Export_Android Room - Fatal编程技术网

Android 将光标导出到CSV文件。CSV文件丢失

Android 将光标导出到CSV文件。CSV文件丢失,android,csv,export,android-room,Android,Csv,Export,Android Room,我有一个使用Room数据库体系结构的客户数据库。在后台,我有一个客户实体、一个客户Dao、一个客户数据存储库和一个客户视图模型。我创建的应用程序具有这4个组件。现在我需要将数据下载到CSV文件中,我正在寻找最好的方法 到目前为止,我已经根据我的研究提出了这个解决方案 拥有CSV文件写入实用程序(从internet获得) 最后,在Android清单中,我拥有以下权限:

我有一个使用Room数据库体系结构的客户数据库。在后台,我有一个客户实体、一个客户Dao、一个客户数据存储库和一个客户视图模型。我创建的应用程序具有这4个组件。现在我需要将数据下载到CSV文件中,我正在寻找最好的方法

到目前为止,我已经根据我的研究提出了这个解决方案

  • 拥有CSV文件写入实用程序(从internet获得)

  • 最后,在Android清单中,我拥有以下权限:

  • 命令Log.v(“MainActivity1”,arrStr[0]+arrStr[1]+arrStr[2]);显示我的日志文件中的所有数据。。但是文件不在哪里可以找到。没有出现错误。我能够检索数据并显示在日志中,但CSV编写器似乎不工作,或者我没有获得正确的目录或其他信息。需要一切帮助

    它起作用了。。我在线复制的代码缺少文件夹名称: File exportDir=新文件(Environment.getExternalStorageDirectory(),“/TestFolder”)

      import java.io.IOException;
      import java.io.PrintWriter;
      import java.io.Writer;
    
      public class CSVWriter {
            private PrintWriter pw;
            private char separator;
            private char quotechar;
            private char escapechar;
            private String lineEnd;
    
            /** The character used for escaping quotes. */
            public static final char DEFAULT_ESCAPE_CHARACTER = '"';
    
            /** The default separator to use if none is supplied to the constructor. */
            public static final char DEFAULT_SEPARATOR = ',';
    
            /**
            * The default quote character to use if none is supplied to the
            * constructor.
            */
            public static final char DEFAULT_QUOTE_CHARACTER = '"';
    
            /** The quote constant to use when you wish to suppress all quoting. */
            public static final char NO_QUOTE_CHARACTER = '\u0000';
    
            /** The escape constant to use when you wish to suppress all escaping. */
            public static final char NO_ESCAPE_CHARACTER = '\u0000';
    
            /** Default line terminator uses platform encoding. */
            public static final String DEFAULT_LINE_END = "\n";
    
            /**
             * Constructs CSVWriter using a comma for the separator.
             *
             * @param writer
             *            the writer to an underlying CSV source.
             */
             public CSVWriter(Writer writer) {
                   this(writer, DEFAULT_SEPARATOR, DEFAULT_QUOTE_CHARACTER,
                      DEFAULT_ESCAPE_CHARACTER, DEFAULT_LINE_END);
             } 
    
             /**
              * Constructs CSVWriter with supplied separator, quote char, escape char and 
                 line ending.
              *
              * @param writer
              *            the writer to an underlying CSV source.
              * @param separator
              *            the delimiter to use for separating entries
              * @param quotechar
              *            the character to use for quoted elements
              * @param escapechar
              *            the character to use for escaping quotechars or escapechars
              * @param lineEnd
              *               the line feed terminator to use
              */
              public CSVWriter(Writer writer, char separator, char quotechar, char 
           escapechar, String lineEnd) {
                    this.pw = new PrintWriter(writer);
                    this.separator = separator;
                    this.quotechar = quotechar;
                    this.escapechar = escapechar;
                    this.lineEnd = lineEnd;
            }
    
            /**
            * Writes the next line to the file.
            *
            * @param nextLine
            *            a string array with each comma-separated element as a separate
            *            entry.
            */
            public void writeNext(String[] nextLine) {
    
                   if (nextLine == null)
                   return;
    
                   StringBuffer sb = new StringBuffer();
                   for (int i = 0; i < nextLine.length; i++) {
    
                          if (i != 0) {
                               sb.append(separator);
                          }  
    
                          String nextElement = nextLine[i];
                          if (nextElement == null)
                               continue;
                          if (quotechar !=  NO_QUOTE_CHARACTER)
                               sb.append(quotechar);
                          for (int j = 0; j < nextElement.length(); j++) {
                               char nextChar = nextElement.charAt(j);
                               if (escapechar != NO_ESCAPE_CHARACTER && nextChar == quotechar) {
                                  sb.append(escapechar).append(nextChar);
                               } else if (escapechar != NO_ESCAPE_CHARACTER && nextChar == 
                                      escapechar) {
                                  sb.append(escapechar).append(nextChar);
                               } else {
                                  sb.append(nextChar);
                               }
                          }
                          if (quotechar != NO_QUOTE_CHARACTER)
                               sb.append(quotechar);
                   }
    
              sb.append(lineEnd);
              pw.write(sb.toString());
    
              }
    
              /**
              * Flush underlying stream to writer.
              *
              * @throws IOException if bad things happen
              */
              public void flush() throws IOException {
                   pw.flush();
              }
    
           /**
            * Close the underlying stream writer flushing any buffered content.
            *
            * @throws IOException if bad things happen
             *
            */
            public void close() throws IOException {
                pw.flush();
                pw.close();
            }
    
        }
    
    private CustomerViewModel mCustomerViewModel;
    private Cursor mCursor;
    
    mCustomerViewModel = new ViewModelProvider(this).get(CustomerViewModel.class);
    mCustomerViewModel.getAllCustomers().observe(this, new Observer<List<Customer>>() {
        @Override
        public void onChanged(List<Customer> customers) {
            mCursor = getCursorFromList(customers);
        }
    });
    
    public Cursor getCursorFromList(List<Customer> customers) {
         MatrixCursor cursor = new MatrixCursor(
            new String[]{"id", "customerCode", "branchCode", "telephone"}
         );
    
        for (Customer customer : customers) {
             cursor.newRow()
                .add("id", customer.getId())
                .add("customerCode", customer.getCustomerCode())
                .add("branchCode", customer.getBranchCode())
                .add("telephone", customer.getTelephone());
         }
    
         return cursor;
      } 
    
            File exportDir = new File(Environment.getExternalStorageDirectory(), "");
            if (!exportDir.exists()) {
                exportDir.mkdirs();
            }
    
            File file = new File(exportDir, "customer.csv");
            try {
                file.createNewFile();
                CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
                csvWrite.writeNext(mCursor.getColumnNames());
                while (mCursor.moveToNext()) {
                    //Which column you want to exprort
                    String arrStr[] = {mCursor.getString(0), mCursor.getString(1), mCursor.getString(2)};
                    Log.v("MainActivity1", arrStr[0] + arrStr[1] + arrStr[2]);
                    csvWrite.writeNext(arrStr);
                }
                csvWrite.close();
                mCursor.close();
            } catch (Exception sqlEx) {
                Log.e("MainActivity1", sqlEx.getMessage(), sqlEx);
            }