简体   繁体   English

如何从Android中的.txt文件读取/写入

[英]How to read/write from .txt file in android

I'm trying to import a list of words into an arraylist from a .txt file. 我正在尝试将单词列表从.txt文件导入到arraylist中。 Right now i'm placing my .txt file into the assets folder. 现在,我将.txt文件放入资产文件夹中。 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. 然后,我希望能够将我的arraylist改组并将单词放回相同的.txt文件中,以便下次我打开该应用程序时,它将导入经过改组的列表。 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? 有没有其他方法可以从.txt文件导入单词,但仍然能够导出到相同的.txt文件? Where do I need to put my .txt file? 我需要将.txt文件放在哪里?

1) Place your txt file in assets. 1)将您的txt文件放入资产中。

2) At the first launch copy your txt file from assets to internal storage 2)在第一次启动时,将txt文件从资产复制到内部存储中

Get InputStream of file in assets: 获取资产中文件的InputStream:

InputStream inputStream = am.open(inputFile);

Get OutputStream of file in internal storage: 获取内部存储中文件的OutputStream:

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: 3)从内部存储读取数据:

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: 4)将数据写入内部存储器:

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. 您可以使用BufferedReader而不是FileReader来逐行读取文件。

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. 请记住,资产或drawables,values,xml的所有资源都是只读文件。 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. 因此最好将文件复制到sdcard中,然后将其修改到外部存储中,然后再读取它。

try this, 尝试这个,

BufferedReader reader = null;
try {
    reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt"), "UTF-8")); 

    // do reading, usually loop until end of file reading 
   if (inputStream != null) {
            InputStreamReader streamReader = new InputStreamReader(
                    inputStream);
            BufferedReader bufferedReader = new BufferedReader(streamReader);

            String line;

            while ((line = bufferedReader.readLine()) != null) {
                words.add(line);
            }
        }
} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
        reader.close();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM