문제

I am .NET developer for many years (10 years) and I am programming android java the first time in my free time. 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. So far no problem for a C#.net developer, because java syntax is like c# syntax. 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. But if I connect the smartphone to the desktop pc, the csv file is not shown in the windows file explorer.

The file is only shown in the windows file explorer, if I reboot the smartphone. It seems that the code does not free the resource, although I close the file with the close method and end the application.

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:

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. 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.

Viel Glück damit.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top