0

I'm trying to import a list of words into an arraylist from a .txt file. Right now i'm placing my .txt file into the assets folder. So far i can do this using the following code

try {
        AssetManager am = this.getAssets();
        InputStream inputStream = am.open(inputFile);

        if (inputStream != null) {
            InputStreamReader streamReader = new InputStreamReader(
                    inputStream);
            BufferedReader bufferedReader = new BufferedReader(streamReader);

            String line;

            while ((line = bufferedReader.readLine()) != null) {
                words.add(line);
            }
        }

        inputStream.close(); // close the file
    } catch (IOException e) {
        e.printStackTrace();
    }

I then want to be able to shuffle my arraylist and put the words back into the same .txt file, so that the next time I open the app it will import the shuffled list. But it turns out you can't write to files in the asset folder. Is there a different way to import words from a .txt file and still be able to export to that same .txt file? Where do I need to put my .txt file?

Fsh
  • 3
  • 2
  • 3

2 Answers2

2

1) Place your txt file in assets.

2) At the first launch copy your txt file from assets to internal storage

Get InputStream of file in assets:

InputStream inputStream = am.open(inputFile);

Get OutputStream of file in internal storage:

File f = context.getFileStreamPath("filename.txt");
OutputStream outputStream = new FileOutputStream(f);

Copy data from input stream to output stream:

ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 8);

ReadableByteChannel ich = Channels.newChannel(inputStream);
WritableByteChannel och = Channels.newChannel(outputStream);

while (ich.read(buffer) > -1 || buffer.position() > 0)
{
    buffer.flip();
    och.write(buffer);
    buffer.compact();
}   
ich.close();
och.close();

3) Read data from internal storage:

File f = context.getFileStreamPath("filename.txt");
FileReader fr = new FileReader(f);
int chr = fr.read(); // read char
fr.close();

4) Write data to internal storage:

File f = context.getFileStreamPath("filename.txt");
FileWriter fw = new FileWriter(f);
fw.write("word"); // write string
fw.close();

You can use BufferedReader instead of FileReader for read file line by line.

Nik
  • 7,114
  • 8
  • 51
  • 75
  • Where does this save the file? Where in my project would I place the .txt file? – Fsh May 15 '14 at 10:17
  • This sounds good. If i only want the app to copy the file the first time it is ever run so that it doesn't overwrite the internal storage file, i guess i can just have a boolean "isFirstTimeRunning" or something that i save in savedpreferences? – Fsh May 15 '14 at 10:22
  • Just check, that file in internal storage is exists: f.exists(); If file not exists you must copy it from assest. NOTE: IO operations must be perform in background thread! – Nik May 15 '14 at 10:28
  • Turns out, "copy your txt file from assets to internal storage" is harder than it sounds. Any tips or links to a good guide? – Fsh May 15 '14 at 11:22
  • @Fsh, see changes in my answer! – Nik May 16 '14 at 06:12
0

You can not write into the file of assets folder because they are the read only files. You can only read it can't modify or update it into assets folder.

Keep in mind that all the resources of assets or drawables,values,xml are only read only files. It can not be modified.

So its better you copy your file into the sdcard and then modify it into external storage and then read it.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102