0

I'm trying to create a txt file during the operation of my App which I then want to download onto my computer and assess the contents.

I've try both on the internal and external storage but I am still unable to find the text file after (when my tablet is plugged into my computer).

private void writeToFile(String data) {
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("orp.txt",Context.MODE_WORLD_WRITEABLE));
        outputStreamWriter.write(data);
        outputStreamWriter.close();

        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"orp2.txt");
        String tempS = file.getAbsolutePath();
        file.setReadable( true, false ); 

        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(data);
        bw.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    } 
}

I have found been able to find either ("orp.txt" or "orp2.txt"), but also I do not receive any error during the running of the app, and the app is able to open "orp.txt" at a later time, so I know is has been created.

  • I'm sure there's a media something that creates a list of files on the device, and it is not updated unless a certain command is called. Will try to recover the article for you.. It used to sometimes work by either unplugging and plugging the device back into pc, or restarting the device – matty357 Jul 08 '15 at 13:26
  • Thanks, your comment pointed me in the right direction – Gareth Edwards Jul 09 '15 at 08:46

2 Answers2

1

I cant find files created unless using Eclipse file explorer, or maybe its the media scan that has to be triggered, can be done by restarting the device. Scans the files available on the device.

It may be the directory, this is what I do in my code.

            File f = new File(cxt.getExternalFilesDir(filepath), fileName);
            f.createNewFile();
            FileWriter writer = new FileWriter(f); 
            writer.append(manNo+System.getProperty("line.separator"));
            writer.append(dateTime);
            writer.flush();
            writer.close();
matty357
  • 637
  • 4
  • 16
0

Thanks for the input but I found the answer was to do with the media scanner, "orp2.txt" appeared in my download folder after I restarted the device.

This answer and blog post helped

Android: save file to downloads that can be viewed later

Community
  • 1
  • 1