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)