Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6376303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:43:38+00:00 2026-05-25T01:43:38+00:00

trying to get length of file which from local storage. File exists 100% (because

  • 0

trying to get length of file which from local storage. File exists 100% (because I even tried to create it straight before getting the length (and checked it exists). Code is as simple as:

    try {
        InputStream is = new FileInputStream("errorlog2.txt");
        // Get the size of the file
        long length = file.length();
        // Close the input stream and return length
        is.close();
        return length;
    }
    catch (IOException e) {
         Log.w(BaseHelper.TAG_MAIN_ACTIVITY, "bad stuff: ", e);
        return 0;

in 100% cases it throws an exception. What might be the problem? What`s correct way to get length of local storage files?

thanks a lot!

UPDATE (full code) – file exists and readable, but no length 🙁

//creating file
String someFileName = "errlogtest.log";   
try {
   FileOutputStream fOut = openFileOutput(someFileName, Context.MODE_APPEND);
   OutputStreamWriter osw = new OutputStreamWriter(fOut);
   osw.write("dsgaadfg0df9g0sdf90sg9058349 sdf");
   osw.flush();
   osw.close();    
} catch (IOException ioe) {
   ioe.printStackTrace();
}

//reading file 

try {
  FileInputStream fileToOpen = openFileInput(someFileName);
  byte[] readerByte = new byte[fileToOpen.available()];
  while (fileToOpen.read(readerByte) != -1) {
  }
  String fileContents = new String(readerByte);
  fileContents.toString();
  // next line works fine
  Toast.makeText(getApplicationContext(), "FILE CONTENTS: " + fileContents, Toast.LENGTH_LONG).show();                      
  } catch (FileNotFoundException e) {
      Log.w("Reading file", "Unable to open file");
  } catch (IOException e) {
  Log.w("Reading file", "Error reading file");
  } 

 // trying to get length
 // simply not working:
 File file = new File(someFileName); 
 long length2 = file.length();
 Toast.makeText(getApplicationContext(),    "FILE LENGTH:" + length2, Toast.LENGTH_LONG).show();

 // shows zero and filenotfoundexception: /errlogtest.log
try {
  RandomAccessFile raffile = new RandomAccessFile(new File(someFileName), "r"); 
  long length = raffile.length();
  Toast.makeText(getApplicationContext(),   "FILE LENGTH:" + length, Toast.LENGTH_LONG).show();
catch (IOException e) {
  Log.w("Reading file", "Error reading file", e);
}

help please

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:43:38+00:00Added an answer on May 25, 2026 at 1:43 am

    Depending on where you are creating the file then you’ll need to provide a full path and not just a file name.

    For example, if the file is being created using Context.openFileOutput(...) then you would use something like…

    long length = new File(getFilesDir().getAbsolutePath() + "/errorlog2.txt").length();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get data from file. The file first has three lines of
I am trying to get data from xml file and then filter using datatable.Select()
I have 2 equal-length lists and I am trying to get scalar product of
So i am trying get 2 div-containers which both should contain centered text (Both
I'm trying get the visible portion of UIImage from an UIImageView . UIImageView takes
I'm trying to load a variable from a file using javascript. I've found some
I'm trying to get a static image of a hiking trail from google maps
I have a situation where there is a corrupt WAV file from which I'm
From PostgreSQL, am trying to read an image file into an Imagick object -
I'm trying to receive a variable length stream from a camera with python, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.