Asked  7 Months ago    Answers:  5   Viewed   37 times

I want to read the text from a text file. In the code below, an exception occurs (that means it goes to the catch block). I put the text file in the application folder. Where should I put this text file (mani.txt) in order to read it correctly?

    try
    {
        InputStream instream = openFileInput("E:\test\src\com\test\mani.txt"); 
        if (instream != null)
        {
            InputStreamReader inputreader = new InputStreamReader(instream); 
            BufferedReader buffreader = new BufferedReader(inputreader); 
            String line,line1 = "";
            try
            {
                while ((line = buffreader.readLine()) != null)
                    line1+=line;
            }catch (Exception e) 
            {
                e.printStackTrace();
            }
         }
    }
    catch (Exception e) 
    {
        String error="";
        error=e.getMessage();
    }

 Answers

95

Try this :

I assume your text file is on sd card

    //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');
    }
    br.close();
}
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.toString());

following links can also help you :

How can I read a text file from the SD card in Android?

How to read text file in Android?

Android read text raw resource file

Tuesday, June 1, 2021
 
Blur
answered 7 Months ago
61

You need to make sure that both the service and the reader open the log file non-exclusively. Try this:

For the service - the writer in your example - use a FileStream instance created as follows:

var outStream = new FileStream(logfileName, FileMode.Open, 
                               FileAccess.Write, FileShare.ReadWrite);

For the reader use the same but change the file access:

var inStream = new FileStream(logfileName, FileMode.Open, 
                              FileAccess.Read, FileShare.ReadWrite);

Also, since FileStream implements IDisposable make sure that in both cases you consider using a using statement, for example for the writer:

using(var outStream = ...)
{
   // using outStream here
   ...
}

Good luck!

Thursday, June 3, 2021
 
Gilko
answered 7 Months ago
46

The problem was that my file was too big, and was being compressed because of it's ".txt" extension. By renaming the file to a format that is normally compressed, ".mp3", there was no issue

Thursday, August 12, 2021
 
phpmeh
answered 4 Months ago
22

OK, I got it. I had to pass the context of the main activity to this class, then use context.getAssets.open("twentyone.txt");

To anyone who has the same problem, do this: Put this in the onCreate function of the class with activity: Context context=getApplicationContext();

Pass context to the new class's function(in my case "process(String a,Context context)") Then type this in the process function:

InputStream is = context.getAssets().open("twentyone.txt");

Took me 4 hours to figure such a silly thing out.

Wednesday, September 1, 2021
 
Chris Herrera
answered 3 Months ago
22

As I mentioned in my comment, the scanf family of functions have simple pattern matching which could be used to read your input:

char text[32];
int value;

while (fscanf(file, " (%32[^,], %d)", text, &value) == 2)
{
    printf("Got (%s, %d)n", text, value);
}

Explanation of the scanf format used:

  • " " matches any leading white-space
  • "(" matches the opening parenthesis
  • "%32[^,]" matches (at most 32) characters except the comma
  • "," matches the comma
  • "%d" matches the integer value
  • ")" matches the closing parenthesis
Wednesday, October 27, 2021
 
Chvanikoff
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share