在安卓系统中从手机内存中读取文本文件[英] read text file from phone memory in android

本文是小编为大家收集整理的关于在安卓系统中从手机内存中读取文本文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我只想创建一个文本文件到手机内存中,必须读取其内容到display.now我创建了一个文本文件.但它不存在于路径数据/数据/包名称/文件名中.txt&it没有显示模拟器上的内容.

我的代码是..

public class PhonememAct extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv=(TextView)findViewById(R.id.tv);

        FileOutputStream fos = null;
        try {
            fos = openFileOutput("Test.txt", Context.MODE_PRIVATE);
        } catch (FileNotFoundException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        try {
            fos.write("Hai..".getBytes());
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        FileInputStream fis = null;
        try {
            fis = openFileInput("Test.txt");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int c;

        try {
            while((c=fis.read())!=-1)
                    {
                        tv.setText(c);
                        setContentView(tv);

                        //k += (char)c;
                    }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    }
}

谢谢.

推荐答案

如果您只是尝试编写/读取文本,则无需使用输入/输出流.

使用filewriter将文本写入文件和bufferedreader以从文件中读取文本 - 这更简单.这完美的工作...

try {
    File myDir = new File(getFilesDir().getAbsolutePath());
    String s = "";

    FileWriter fw = new FileWriter(myDir + "/Test.txt");
    fw.write("Hello World");
    fw.close();

    BufferedReader br = new BufferedReader(new FileReader(myDir + "/Test.txt"));
    s = br.readLine();

    // Set TextView text here using tv.setText(s);

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

其他推荐答案

    //Find the directory for the SD Card using the API
    //*Don't* hardcode "/sdcard"
    File sdcard = Environment.getExternalStorageDirectory();

    //Get the text file
    File file = new File(sdcard,"file.txt");

    //Read text from file
    StringBuilder text = new StringBuilder();

    try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
    text.append(line);
    text.append('\n');
    }
    }
    catch (IOException e) {
    //You'll need to add proper error handling here
    }

    //Find the view by its id
    TextView tv = (TextView)findViewById(R.id.text_view);

    //Set the text
    tv.setText(text);

其他推荐答案

//To read file from internal phone memory
//get your application context:
Context context = getApplicationContext(); 

filePath = context.getFilesDir().getAbsolutePath();
File file = new File(filePath, fileName);
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));   
    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
}
catch (IOException e) {

}
return text.toString(); //the output text from file.

本文地址:https://www.itbaoku.cn/post/102453.html

问题描述

I just wanna create a text file into phone memory and have to read its content to display.Now i created a text file.But its not present in the path data/data/package-name/file name.txt & it didn't display the content on emulator.

My code is..

public class PhonememAct extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv=(TextView)findViewById(R.id.tv);

        FileOutputStream fos = null;
        try {
            fos = openFileOutput("Test.txt", Context.MODE_PRIVATE);
        } catch (FileNotFoundException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        try {
            fos.write("Hai..".getBytes());
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        FileInputStream fis = null;
        try {
            fis = openFileInput("Test.txt");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int c;

        try {
            while((c=fis.read())!=-1)
                    {
                        tv.setText(c);
                        setContentView(tv);

                        //k += (char)c;
                    }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    }
}

Thanks in adv.

推荐答案

You don't need to use input/output streams if you are simply trying to write/read text.

Use FileWriter to write text to a file and BufferedReader to read text from a file - it's much simpler. This works perfectly...

try {
    File myDir = new File(getFilesDir().getAbsolutePath());
    String s = "";

    FileWriter fw = new FileWriter(myDir + "/Test.txt");
    fw.write("Hello World");
    fw.close();

    BufferedReader br = new BufferedReader(new FileReader(myDir + "/Test.txt"));
    s = br.readLine();

    // Set TextView text here using tv.setText(s);

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

其他推荐答案

    //Find the directory for the SD Card using the API
    //*Don't* hardcode "/sdcard"
    File sdcard = Environment.getExternalStorageDirectory();

    //Get the text file
    File file = new File(sdcard,"file.txt");

    //Read text from file
    StringBuilder text = new StringBuilder();

    try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
    text.append(line);
    text.append('\n');
    }
    }
    catch (IOException e) {
    //You'll need to add proper error handling here
    }

    //Find the view by its id
    TextView tv = (TextView)findViewById(R.id.text_view);

    //Set the text
    tv.setText(text);

其他推荐答案

//To read file from internal phone memory
//get your application context:
Context context = getApplicationContext(); 

filePath = context.getFilesDir().getAbsolutePath();
File file = new File(filePath, fileName);
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));   
    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
}
catch (IOException e) {

}
return text.toString(); //the output text from file.