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

  • Home
  • SEARCH
  • 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 8583367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:31:12+00:00 2026-06-11T21:31:12+00:00

I am trying to copy a large pdf-file (3.7 mb) from my raw-folder to

  • 0

I am trying to copy a large pdf-file (3.7 mb) from my raw-folder to the external cache directory.

I a using the following piece of code:

int i = 0;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) 
        {           
            InputStream input = getResources().openRawResource(pdfs[i]);
            File file = new File(Environment.getExternalStorageDirectory(), "/Android/data/eu.app/cache/" + pdfNames[i]);

            if(!file.exists())
            {
                try
                {
                    new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/eu.app/cache").mkdirs();
                    FileOutputStream fos = new FileOutputStream(file.toURI().getPath(), false);

                    OutputStream os = new BufferedOutputStream(fos);



                    byte[] buffer = new byte[1024];
                    int byteRead = 0;


                    while ((byteRead = input.read(buffer)) != -1) {

                            os.write(buffer, 0, byteRead);
                    }

                    fos.close();

                }
                catch(Exception ex)
                {
                    Log.d("storage", ex.getMessage()); 

                }
            }               
        }
        else
        {

        }

I don’t get any errors, but the output-file is a few bytes smaller than the original and cannot be opened.
What do I need to do to fix this?

  • 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-06-11T21:31:13+00:00Added an answer on June 11, 2026 at 9:31 pm

    I think the main issue is that you close fos while you should close os. You also need to put the close operation in a finally block.

    Update (now with a full keyboard ;)): You close the file stream (fos) before the buffered stream is flushed. What you should do is to close the buffered stream (os), and that will in turn flush its buffer and write those bytes that are missing, and then it will automatically close the underlying file stream. To fix it change fos.close() into os.close().

    In addition, to make sure that you always close the stream you should place the close operation in a finally block. A typical pattern is the following:

    BufferedInputStream in = null;
    try {
        in = new BufferedInputStream(anInputStream);
    
        BufferedOutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(aFile));
            // Read and write what you should write
        }
        finally {
            if (out != null) out.close();
        }
    } finally {
        if (in != null) in.close();
    }
    

    You can easily add an input stream, but be careful to make sure all streams are closed. This can be handled by nesting finally blocks or nesting try-catch blocks inside the finally block.

    Either you throw an IOException from this method and handle it outside (often preferred), or you wrap the above code in a new try-catch statement and handle it there. However, handling it within the method mixes UI with logic and the code is often clearer separating UI and logic.

    A final note: 1024 is rather small. Play with different values. On the other hand the buffered stream will handle the buffering for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to copy all format file (.txt,.pdf,.doc ...) file from source folder
I'm trying copy a single file from the Plugin directory inside of my Wordpress
I am trying to copy the generated program file to the parent directory after
I am trying to copy files from a directory that is in constant use
I am trying to copy and paste a folder structure on file system programmatically.
I'm currently trying to copy data from table into another by using a SELECT
I am trying to copy a file of about 80 megabytes from the assets
I'm trying to copy files to C:\Windows\System32 using cygwin bash. When I copy files
I'm trying to copy the <options> HTML Codes from one select list to another.
I am trying to copy all the methods and attributes from a class to

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.