Tôi đang cố gắng tạo một thư mục nhưng dường như nó thất bại mỗi lần? Tôi đã kiểm tra rằng nó không phải là một vấn đề cho phép quá, tôi có toàn quyền để ghi vào thư mục đó. Cảm ơn trước.Java - mkdir() không viết thư mục
Đây là mã:
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);
Đây là kết quả:
Directory is not created
Error: C:\Program Files\Sure Important\Report Cards\2012\7\4532.txt (The system cannot find the path specified)
Cheers điều đó dường như là vấn đề, tôi sẽ chấp nhận câu trả lời khi nó alows tôi :) CẢM ƠN!!! – Nick
Vì phần "động" của tên đường dẫn có hai thư mục, tôi muốn nói lý thuyết này là khá tốt ... –
Tôi đã sao chép mã của bạn, đổi nó thành mkdirs() và nó hoạt động. Chỉnh sửa để bao gồm mã có liên quan –