java - Can't make Files and Folders -
so i'm working on simple windows explorer replacement. want add ability create folders , files. reason, works when i'm in root or c:/ folder, it's somewhere else (for example c:\program files (x86)) doesn't work. either java.io.ioexception: access denied when create file , when try create folder, no exception comes up, no folder created.
this code new file:
string location = getpath(); string name = joptionpane.showinputdialog("fill in name of new file. \ndon't forget add file type (.txt, .pdf).", null); if(name == null){ } else { file newfile = new file(location + "\\" + name); boolean flag = false; try { flag = newfile.createnewfile(); } catch (ioexception io) { jframe messagedialog = new jframe("error!"); joptionpane.showmessagedialog(messagedialog, "file creation failed following reason: \n" + io); } }
this code new folder:
string location = getpath(); string name = joptionpane.showinputdialog("fill in name of new folder.", null); if(name == null){ } else { file newfolder = new file(location + "\\" + name); boolean flag = false; try { flag = newfolder.mkdir(); } catch (securityexception se) { jframe messagedialog = new jframe("error!"); joptionpane.showmessagedialog(messagedialog, "folder creation failed following reason: \n" + se); } }
i'm stuck right , have no idea i'm doing wrong rid of access denied error.
short explenation of how program works: program shows list of folders , files selected file. file field in class jxplorefile called "currentfile", behaves same file. when browsing through folders, currentfile set new jxplorefile, containing new folder in file. when creating new folder/file, program ask path user browsing in method getpath().
thanks help!
image of program:
before try make i/o operation check if have permission
go parent directory (your case location
)
then like
file f = new file(location); if(f.canwrite()) { /*your full folder creation code here */ } else { }
Comments
Post a Comment