How to read a text file in Android studio when I have the path of that file
This Below one works for me… :)
- //Read File
- readButton.setOnClickListener(new View.OnClickListener(){
- @Override public void onClick(View arg0) {
- String filename=editTextFileName.getText().toString();
- StringBuffer stringBuffer = new StringBuffer();
- try {
- //Attaching BufferedReader to the FileInputStream by the help of
- InputStreamReader BufferedReader inputReader = new BufferedReader(new
- InputStreamReader(getContext().openFileInput(filename)));
- String inputString;
- //Reading data line by line and storing it into the stringbuffer
- while ((inputString = inputReader.readLine()) != null) {
- stringBuffer.append(inputString + "\n");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- if(stringBuffer.toString().length() == 0){
- Toast.makeText(getContext(),"File not Available...!",
- Toast.LENGTH_LONG).show();
- return;
- }
- else {
- //Displaying data on the toast Toast.makeText(getContext(), stringBuffer.toString(),
- Toast.LENGTH_LONG).show();
- return;
- }
- }
- });