यह शायद क्योंकि File.mkdir
निर्देशिका केवल तभी मूल निर्देशिका मौजूद रहता है है:
private void writeTextFile(String v){
try{
String yearString = convertInteger(yearInt);
String monthString = convertInteger(month);
String fileName = refernce.getText();
File fileDir = new File("C:\\Program Files\\Sure Important\\Report Cards\\" + yearString + "\\" + monthString);
File filePath = new File(fileDir + "\\"+ fileName + ".txt");
writeDir(fileDir);
// Create file
FileWriter fstream = new FileWriter(filePath);
try (BufferedWriter out = new BufferedWriter(fstream)) {
out.write(v);
}
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
private void writeDir(File f){
try{
if(f.mkdir()) {
System.out.println("Directory Created");
} else {
System.out.println("Directory is not created");
}
} catch(Exception e){
e.printStackTrace();
}
}
public static String convertInteger(int i) {
return Integer.toString(i);
}
Calendar cal = new GregorianCalendar();
public int month = cal.get(Calendar.MONTH);
public int yearInt = cal.get(Calendar.YEAR);
यहाँ आउटपुट है:
यहाँ कोड है। File.mkdirs
का उपयोग करने का प्रयास करें जो सभी आवश्यक निर्देशिकाएं बनाता है।
यहां कोड है जो मेरे लिए काम करता है।
private void writeDir(File f){
try{
if(f.mkdirs()) {
System.out.println("Directory Created");
} else {
System.out.println("Directory is not created");
}
} catch(Exception e){
// Demo purposes only. Do NOT do this in real code. EVER.
// It squashes exceptions ...
e.printStackTrace();
}
}
केवल परिवर्तन मैंने बनाया f.mkdirs()
को f.mkdir()
बदलने के लिए था और यह
स्रोत
2012-08-26 01:30:10
चीयर्स कि समस्या लगती है, जब यह मेरे alows लिए :) मैं जवाब को स्वीकार करेंगे धन्यवाद!!! – Nick
चूंकि पथनाम के "गतिशील" भाग में दो निर्देशिकाएं हैं, इसलिए मैं कहूंगा कि यह सिद्धांत बहुत अच्छा है ... –
मैंने आपके कोड की प्रतिलिपि बनाई, इसे mkdirs() में बदल दिया और यह काम किया। प्रासंगिक कोड –