写入项目中已经存在的CSV文件

时间:2018-08-03 10:59:32

标签: java android csv storage

我目前有2个CSV文件保存在Android项目的原始文件夹中。我拥有这些文件的原因是,我有2个AutoCompleteTextView字段可从这些CSV文件中读取。

问题是随着时间的推移,这些CSV文件将过时。因此,我想实现的是,如果输入的值在CSV文件中不存在,则将其添加到其中。

这可能吗?

谢谢!

CSV Files

写代码:

public void exportEmailInCSV() throws IOException {
    {
        File folder = new File(Environment.getExternalStorageDirectory()
                + "/Folder");
        boolean var = false;
        if (!folder.exists())
            var = folder.mkdir();
        final String filename = folder.toString() + "/" + "Test.csv";
        try {
            FileWriter fw = new FileWriter(filename, true);
            fw.append(batchNumber);
            Toast.makeText(this, "SAVED", Toast.LENGTH_SHORT).show();
            // fw.flush();
            fw.close();
        } catch (Exception e) {
            e.fillInStackTrace();
        }
    }
}

读取代码:

 public void ReadInCSV() throws IOException {
    {
        BufferedReader br = null;
        File folder = new File(Environment.getExternalStorageDirectory()
                + "/Batchnumbers");
        final String filename = folder.toString() + "/" + "BatchNumbers";

        try {
            br = new BufferedReader(new FileReader(filename + ".csv"));
            while ((sCurrentLine = br.readLine()) != null) {
                Toast.makeText(this, sCurrentLine, Toast.LENGTH_LONG).show();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

AutocompleteTextView代码:

//READ FROM ITEM CODES CSV
    Scanner scanner2 = new Scanner(getResources().openRawResource(R.raw.itemcodesofficial));
    List<String> listitemcodes = new ArrayList<String>();
    while (scanner2.hasNext()) {
        listitemcodes.add(scanner2.next());
    }
    ArrayAdapter<String> adapter2 =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listitemcodes );
    itemcodetextview.setAdapter(adapter2);
    scanner2.close();

0 个答案:

没有答案