简体   繁体   English

Android Java:文件似乎在创建/写入后被锁定

[英]Android Java: File seems to be locked after creating/writing

I am .NET developer for many years (10 years) and I am programming android java the first time in my free time. 我是.NET开发人员多年(10年),我有空时第一次编程android java。 The application I am programming is an app for input data of some course participant. 我正在编程的应用程序是用于某些课程参与者的输入数据的应用程序。 The course instructor inputs some course number (which is part of the file name) and each participant enters his name and birthdate. 课程讲师输入一些课程编号(这是文件名的一部分),每个参与者输入他的姓名和生日。 So I have two activities. 所以我有两个活动。 One for the instructor, one for the participant. 一个给讲师,一个给参与者。

The first Activity creates the File in the download folder, the second one append he participant data at the end of the CSV file. 第一个活动在下载文件夹中创建文件,第二个活动在CSV文件末尾附加参与者数据。 So far no problem for a C#.net developer, because java syntax is like c# syntax. 到目前为止,对于C#.net开发人员来说没有问题,因为Java语法类似于c#语法。 The App was programmed in one week. 该应用程序在一周内完成了编程。

But now during testing following problem occurs: The generated csv file can be viewed on android in the android file explorer and the content can be opened in android. 但是现在在测试过程中会出现以下问题:可以在android文件浏览器中的android上查看生成的csv文件,并且可以在android中打开内容。 But if I connect the smartphone to the desktop pc, the csv file is not shown in the windows file explorer. 但是,如果我将智能手机连接到台式机,则Windows文件资源管理器中不会显示csv文件。

The file is only shown in the windows file explorer, if I reboot the smartphone. 如果重新启动智能手机,则该文件仅显示在Windows文件资源管理器中。 It seems that the code does not free the resource, although I close the file with the close method and end the application. 尽管我使用close方法关闭了文件并结束了应用程序,但是代码似乎并没有释放资源。

What do I make wrong in the following code: 我在以下代码中犯了什么错误:

public boolean CreateFile() {
    boolean OK = false;

    File file = new File(_FileName);
    if (!file.exists()) {
            try {                   
                    if (file.createNewFile())
                    {
                        _StatusText.setText( "Status:Leere Kurs-Teilnehmer-Datei angelegt:\n" + _FileName + ".csv");
                        OK = true;
                    }
                    else
                        _StatusText.setText("Status:Datei nicht angelegt");                     
            } catch (Exception e) {
                _StatusText.setText("Status:Fehler:" + e.toString());
            }
            finally {
                return OK;
            }
    }
    else
    {
        _StatusText.setText("Status:Bereits Vorhandene Kursliste wird verwendet");
        return true;
    }
}

This is the code for adding lines to the end of csv file: 这是用于在csv文件末尾添加行的代码:

public static void AppendDataToCSV(String Filename, String Nachname, String Vorname, String Datum) throws IOException {
    FileWriter out = null;
    try {
        StringBuilder textline = new StringBuilder();
        textline.append(Nachname)
        .append(";")
        .append(Vorname)
        .append(";")
        .append(Datum)
        .append("\n");

        out = new FileWriter(Filename, true);
        out.write(textline.toString());
    }
    finally {
        if (out != null)
            out.close();            
    }
}

This is the code for finishing the application: 这是完成应用程序的代码:

private void ButtonEndeClick(){
    _StatusText.setText("Ende");
    finish();
    System.exit(0);
}

Please, let me know how to be sure that all ressorces are freed after app is closed. 请让我知道如何确保在关闭应用程序后释放所有的调料。

Many thanks to you for your help :-) 非常感谢您的帮助:-)

It's not locking of files, but the way Android handles new file entries. 它不是锁定文件,而是Android处理新文件条目的方式。 The good news is, that it looks like somebody else had the problem before and somebody seems to have solved it. 好消息是,好像以前有人遇到过问题,似乎有人已经解决了问题。

So this Stackoverflow question and answer might help you. 因此, 此Stackoverflow问题和解答可能会对您有所帮助。

Viel Glück damit. VielGlück戴米特。

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

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