写入.txt并继续写而不是替换

时间:2016-07-09 22:12:38

标签: android

如何让它创建新行并继续编写而不是替换.txt中的文本并添加其他一些东西

1.5(这应该只在顶部添加一次,我不知道该怎么做)

LAGERIND(文本)

KW89668(Cardnumberbox)

RHAL4(shelfnumberbox)

这就是它在.txt中应该是什么样子的 enter image description here

    public void onPictureTaken(byte[] bytes, Camera camera) {
                 File dir = new File(path);
                dir.mkdirs();
                File file = new File (path + "/append.txt");
String [] saveText = String.valueOf("LAGERIND"+cardnumberbox.getText()+shelfnumberbox.getText()).split(System.getProperty("line.separator"));

                cardnumberbox.setText("");

                Save (file ,saveText);

   public static void Save(File file, String[] data)
    {
        FileOutputStream fos = null;
        try
        {
            fos = new FileOutputStream(file);
        }
        catch (FileNotFoundException e) {e.printStackTrace();}
        try
        {
            try
            {
                for (int i = 0; i<data.length; i++)
                {
                    fos.write(data[i].getBytes());
                    if (i < data.length-1)
                    {
                        fos.write("\n".getBytes());
                    }
                }
            }
            catch (IOException e) {e.printStackTrace();}
        }
        finally
        {
            try
            {
                fos.close();
            }
            catch (IOException e) {e.printStackTrace();}
        }
    }

1 个答案:

答案 0 :(得分:2)

FileOutputStream fos = null;
    try
    {
        fos = new FileOutputStream(file, true);
    }
    catch (FileNotFoundException e) {e.printStackTrace();}

使用此构造函数追加文件 FileOutputStream(file,true)

更多详情here (FileOutputStream api doc)