イム:ファイルのこのコードでファイルが作成されないのはなぜですか?これらの方法でファイルを作成しようとしている
private boolean createFileMain(String path){
File file = new File(path);
if(file.isDirectory()){
return this.createDirectory(file);
} else if(file.isFile()) {
return this.createFile(file);
} else {
return false;
}
}
private boolean createFile(File file){
if(!file.exists()){
if(file.getParentFile().exists()){
try{
if(file.createNewFile()){
return true;
}
}catch(IOException e){
return false;
}
} else {
if(this.createDirectory(file)){
this.createFile(file);
} else {
return false;
}
}
}
return true;
}
private boolean createDirectory(File file){
if(!file.exists()){
if(file.mkdirs()){
return true;
}
return false;
}
return true;
}
パス:
/ユーザ/ユーザ名/ディレクトリ/アカウント/
/ユーザ/ユーザ名/ディレクトリ/ SRCS/FILE1.TXT
/Users/username/Directory/file2.txt
このメソッドを実行しようとすると、StackOverFlowError
がスローされます。
public void writeInFile(String path, List<String> content) {
if ((new File(path)).exists()) {
try {
writer = new PrintWriter(path, "ASCII");
for (String contentItem : content) {
writer.println(contentItem);
}
writer.close();
} catch (FileNotFoundException e1) {
//DO STUFF
} catch (UnsupportedEncodingException e) {
//DO STUFF
}
} else {
this.createFileMain(path);
this.writeInFile(path, content);
}
なぜファイルが作成されないのですか?
デバッグしましたか?また、キャッチブロックにロギングを追加してください。何か問題が発生した場合は少なくとも通知を受けます – Stultuske