java - Text document is becoming file folder -
here class, doing wrong. why text document becoming file folder. please explain going on , how can correct it. thank you
public class inputoutput { public static void main(string[] args) { file file = new file("c:/users/crypticdev/desktop/file/text.txt"); scanner input = null; if (file.exists()) { try { printwriter pw = new printwriter(file); pw.println("some data have stored"); pw.println("another data stored"); pw.close(); } catch(filenotfoundexception e) { system.out.println("error " + e.tostring()); } } else { file.mkdirs(); } try { input = new scanner(file); while(input.hasnext()) { system.out.println(input.nextline()); } } catch(filenotfoundexception e) { system.out.println("error " + e.tostring()); } { if (input != null) { input.close(); } } system.out.println(file.exists()); system.out.println(file.length()); system.out.println(file.canread()); system.out.println(file.canwrite()); system.out.println(file.isfile()); system.out.println(file.isdirectory()); } }
thanks. above java class.
you mistakingly assume text.txt
not directory name.
mkdirs()
creates directory (and directories needed create it). in case 'text.txt'
see here: https://docs.oracle.com/javase/7/docs/api/java/io/file.html#mkdirs().
it fine directory have . in it.
you use getparentfile()
directory want create , use mkdirs()
on that.
Comments
Post a Comment