繁体   English   中英

将信息导入到现有的csv文件中

[英]import information into existing csv file android

我的应用程序确实每天都会创建一个新的CSV文件。 现在,我想用一些信息写一个新行。 当我按下一个按钮时,应该会发生这种情况。 我的问题是,我不知道如何打开现有文件并继续写入最新信息的位置。 我做了很多尝试,但是没有找到解决方案。

建立档案

File myFile = new File("/sdcard/Protokolle" + date
                    + "/Protokoll.csv");
            myFile.createNewFile();
            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);

            myOutWriter.append("Date");
            myOutWriter.append(',');
            myOutWriter.append(date);
            myOutWriter.append('\n');
            myOutWriter.append('\n');

            myOutWriter.close();
            fOut.close();

onClick

FileWriter fWriter;

            try {

                File sdCardFile = new File("/sdcard/Protokolle" + date + "/Protokoll.csv");
                Log.d("TAG", sdCardFile.getPath());

                fWriter = new FileWriter(sdCardFile, true);
                fWriter.write("\n");
                fWriter.write("Test");
                fWriter.write("Test");
                fWriter.write("\n");
                fWriter.write("test");

                fWriter.flush();
                fWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

将您的代码更改为

FileWriter writer = new FileWriter(Protokoll,true);

这使您可以追加到现有文件。 请参阅FileWriter的Java API文档 如果文件不存在,则创建文件,否则将附加内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM