FileOutputStream error "File not Found" (Android)

Source: Internet
Author: User

Problem Description:

With FileOutputStream, depending on the document, new FileOutputStream (path) is created automatically if the file below the path path does not exist.

But in the process of use,

Path = Environment.getexternalstoragedirectory (). GetPath () + "/document/mine/www/te.text";

At this point, a new fileoutputstream will report the exception of "File not found".

Problem Analysis:

Modify the path path,

Path = Environment.getexternalstoragedirectory (). GetPath () + "/document/te.text";

The new OutputStream object is then compiled correctly.

The cause of the exception mentioned earlier is that the file directory hierarchy is too deep.

Solution:

You create a directory path that does not exist.

When the directory level is greater than 2 o'clock (such as "/document/mine/te.text"), the Mkdirs () method returns False when it executes.

This is done by using the stitching method to create the catalog segments (such as dividing path into "/document/mine" and "/www/text") so that you can avoid the above problems, the example code is as follows

Copyassetstosd (Context, Environment.getexternalstoragedirectory (). GetPath () + file.separator + "Document",                "Mine" + File.separator + "Cordova.js");Private Static voidCopysingleassettosd (context context, string Sdpath, String assetpath) {InputStream InputStream=NULL; OutputStream OutputStream=NULL; Try{File Dirfile=NewFile (Sdpath); if(!dirfile.exists ()) {dirfile.mkdirs ();//First paragraph} File File=NewFile (Sdpath + file.separator +Assetpath); if(!File.getparentfile (). exists ()) {                //two times mkdirs, to avoid a situation where directory creation failed due to high directory hierarchyfile.getparentfile (). Mkdirs (); }            if(!file.exists ())            {File.createnewfile (); } outputstream=NewFileOutputStream (file);//Create an instanceInputStream =context.getassets (). open (Assetpath); byte[] buffer =New byte[1024]; intLength =inputstream.read (buffer);  while(Length > 0) {outputstream.write (buffer,0, length); Length=inputstream.read (buffer);        } outputstream.flush (); } Catch(IOException e) {e.printstacktrace (); } finally {            Try {                if(NULL!=InputStream)                {Inputstream.close (); }                if(NULL!=OutputStream)                {Outputstream.close (); }            } Catch(IOException e) {e.printstacktrace (); }        }    }

FileOutputStream error "File not Found" (Android)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.