0

I'm looking to reaccess a file created in android studio. I found the following code snippet on stack exchange

File root = android.os.Environment.getExternalStorageDirectory();
    // See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

    File dir = new File(root.getAbsolutePath() + "/download");
    File file = new File(dir, "myData.txt");

From my understanding this creates a file called "myData.txt" in the download folder. What im looking to do is pass "file" into a function in another method like so

  public void onLocationChanged(Location location) {
        ArrayList<Location> list = new ArrayList<>();
        list.add(location);
        GPX.writePath(file,"hello",list);

    }

How do I go about creating a file variable that acesses the txt file without actually creating a new file?

  • `GPX.writePath(**file**,"hello",list);`. Just change to ` GPX.writePath(file,"hello",list);` – greenapps May 08 '18 at 11:17
  • @greenapps It says the symbol file cannot be resolved, as the file reference is in a different method it cannot be called in this method. – user9558800 May 08 '18 at 12:26
  • So it is a compile time error. Then make your file variable global or transfer it to the next function with an extra File parameter. – greenapps May 08 '18 at 12:28
  • But do you really want to write to the same gpx file at every location changed event? As you do it now it looks as writePath() would overwrite all of the file. It looks as if your gpx file would only contain the last location. Or isn't this the case? Please tell us. – greenapps May 08 '18 at 12:30
  • Ye I want to have multiple lines of gps locations appended to the gpx file , didnt think it would override all of the previous locations. I will link you the GPX.java file (it wasnt self written, I found it on github). Here it is: http://pasted.co/0c686df1 – user9558800 May 08 '18 at 12:37
  • You can link what you want but i will not follow it. Please test yourself. You should do the work. – greenapps May 08 '18 at 13:15
  • Whats the purpose of this comment thread then? What was your motivation to even comment in the first place? There is literally 0 point to your last response. You asked me a question in your penultimate response and I responded to it while also linking you to further code, im starting to question the community on this site with answers like yours to be honest. – user9558800 May 08 '18 at 13:20
  • Yes. Answer the questions. Experiment. Do the work. This is not a debugging service or code writing service. – greenapps May 08 '18 at 13:24
  • You havent provided an answer to the question I asked, so again you are totally useless in the context of this thread. For a question and answering site the users here seem to really enjoy not actually answering the questions. – user9558800 May 08 '18 at 13:34
  • That can be. But you got many tips and hints. So to continue you try them out. And if you have problems you post your problems and ask whats wrong. Thats what we enjoy here. – greenapps May 08 '18 at 13:38

1 Answers1

0
File root = android.os.Environment.getExternalStorageDirectory();
// See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder   
File dir = new File(root.getAbsolutePath() + "/download");
File file = new File(dir, "myData.txt");

Actually it does not create a physical file. It just creates an object in memory (referenced by file).

You can then use the reference in your method to write to a physical file, like so:

public void onLocationChanged(File location) throws IOException {
    ArrayList<File> list = new ArrayList<>();
    list.add(location);
    writeToFile("Text", location);
}

private static void writeToFile(String text, File file)
        throws IOException {
    try(BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
        writer.write(text);
    }
}

Update 2

If you want to use the reference in your method, you can pass the file reference to your method which should have some code to write the text into the actual file:

GPX.writePath(file,"hello",list);

...

public static void writePath(File file, String n, List<Location> points) throws IOException {
    ...
    try(BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
        writer.write(n);
    }
}
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
  • I already have a separate class that writes to a file (which will be a gpx file im just testing with txt). Im just looking for a way to pass the reference through in GPX.writePath when I have already created the reference in a different method. How do I access the already created reference to use in the LocationChanged method. – user9558800 May 08 '18 at 11:09
  • @user9558800 which is the type of the **file** parameter in `GPX.writePath(**file**,"hello",list);`? Is it a `java.lang.String`? – gil.fernandes May 08 '18 at 11:12
  • @user9558800 Check the updated answer and see, if it helps. – gil.fernandes May 08 '18 at 11:42
  • Sorry for the late reply, the first parameter is actually of the type file here is the method................. public static void writePath(File file, String n, List points) – user9558800 May 08 '18 at 12:13
  • @user9558800 I have updated the answer with an example of the usage of the `file` reference. – gil.fernandes May 08 '18 at 12:50
  • The problem with this is I cannot access the file reference as I originally create the "File file" variable in another method thus making it inaccesible in the onLocationChanged method. What I need to find out is simply how to reference the exact same file again in another method. Do I just repeat the code that I originally used to create the reference variable in the original method? – user9558800 May 08 '18 at 13:33
  • So if I understand your problem you want to write one distinct file per call of `onLocationChanged`. All files should go into the same folder, is this correct? – gil.fernandes May 08 '18 at 14:00
  • No i want to have one file in the same folder with all the gps lines/ – user9558800 May 08 '18 at 15:21
  • Then the solution should be simply to have a member with the `file` reference in your Android activity (if I am not wrong). You can put the code for the creation of the file object in the `onCreate` method. Then you can simply call `GPX.writePath(file,"hello",list);` – gil.fernandes May 08 '18 at 15:41