Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8256683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:56:28+00:00 2026-06-08T01:56:28+00:00

I’ve been trying to open a text file and and save each line as

  • 0

I’ve been trying to open a text file and and save each line as the contents of an arraylist. Once this has been completed I would like to save it back to a file. I have been running into errors for so long and have tried numerous techniques. I found that for some reason, the files themselves are not being created. It may just be a simple error I’m overlooking but if you could provide any help I will be thankful.

Here’s the code:

public void addToFile(){

File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/appName/savedlocations");
root.mkdirs();
File fileName = new File(root, "locationslatitude.txt");
File fileName2 = new File(root, "locationslongitude.txt");

String file = fileName.toString();
String file2 = fileName2.toString();

String theContent = Double.toString(currLatitude);
String theContent2 = Double.toString(currLongitude);

s = new Scanner(file);
    while (s.hasNext()){
        fileList.add(s.next());
    }
    s.close();
    fileList.add(theContent);



    s2 = new Scanner(file2);
    while (s2.hasNext()){
        fileList2.add(s2.next());
    }
    s2.close();
    fileList2.add(theContent2);


try {//works for latitude file
        FileWriter writer = new FileWriter(file); 
        for(String str: fileList) { 
          writer.write(str);
          writer.write("\r\n");
        }
        writer.close();
        } catch (java.io.IOException error) {
          //do something if an IOException occurs.
            Toast.makeText(this, "Cannot Save Back To A File", Toast.LENGTH_LONG).show();
    }
//save the arraylist back to its appropriate file
    try {//works for longitude file
        FileWriter writer = new FileWriter(file2); 
        for(String str2: fileList2) { 
          writer.write(str2);
          writer.write("\r\n");
        }
        writer.close();
    } catch (java.io.IOException error) {
      //do something if an IOException occurs.
        Toast.makeText(this, "Cannot Save Back To A File", Toast.LENGTH_LONG).show();
    }
}

I believe I found the answer to the problem and I wanted to post it back on here so if anyone else faces the same problem this might help them.

The problem was that it wasn’t creating the file. The directory was created using “root.mkdirs();”. However, the files were not created and I was trying to read from non-existing files. This is what I believe caused the error. So, in order to fix this problem I altered the code to this:

  try{

        s = new Scanner(fileName);
        while (s.hasNext()){
            fileList.add(s.next());
        }
        s.close();
                fileList.add(theContent);

}catch (FileNotFoundException ex){
    ex.printStackTrace();
    try{
        fileName.createNewFile();
    }catch (IOException e){
        e.printStackTrace();
        Toast.makeText(this, "Hit IOException for file one", Toast.LENGTH_SHORT).show();
    }
}

try{  
        s2 = new Scanner(fileName2);
        while (s2.hasNext()){
            fileList2.add(s2.next());
        }
        s2.close();
                fileList2.add(theContent2);
}catch (FileNotFoundException ex){
    ex.printStackTrace();
    try{
        fileName2.createNewFile();
    }catch (IOException e){
        e.printStackTrace();
        Toast.makeText(this, "Hit IOException for file two", Toast.LENGTH_SHORT).show();
    }
}

This was the only piece of code I had to alter. The code which saved back to the file worked. I hope this will be of use to someone and thanks everyone for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:56:30+00:00Added an answer on June 8, 2026 at 1:56 am

    This code works in my project. You can use it to save ArrayList contents to text file. Make sure that the directory is created beforehand. Just iterate through your list and use println method to write it to txt file.

    FileWriter outFile = new FileWriter(Environment.getExternalStorageDirectory().getAbsolutePath() + "/appName/savedlocations/nameoftextfile.txt");
    PrintWriter out = new PrintWriter(outFile);
    out.println("PRINT LINES WITH ME");
    out.print("NOT NECCESSARILY A NEW LINE");
    out.close(); // at the very end
    

    Do not forget to catch IOException.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I have a reasonable size flat file database of text documents mostly saved in
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.