그것은 가능성이있다 : 여기
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()
을 변경했다 그리고
건배를 (물론, 이러한 가능성의 일부를 신속하게 ...이 질문의 맥락에서 제거 될 수 있습니다) 감사!!! – Nick
경로명의 "동적 인"부분에 두 개의 디렉토리가 있기 때문에 나는이 이론이 꽤 좋다고 말하고 싶습니다 ... –
코드를 복사하여 mkdirs()로 변경하면 효과가있었습니다. 관련 코드를 포함하도록 편집 –