4
My program allows user to define a Folder, and later the program will create a New File in this Folder.
However, the program is not able to create a File in any Folder, for example:
- Creates New File in Folder Normally:
new FileOutputStream("C:\\Users\\Public\\Documents\\novoArquivo.txt");
- Casts an Exception: java.io.Filenotfoundexception: C: newFile.txt (Access denied):
new FileOutputStream("C:\\novoArquivo.txt");
I nay I am trying to make the program have permission to create Files in any folder, just need to know with advance (before trying to create the File) whether or not the program will be able to create the File in the Folder chosen by the user.
If the user chooses a Folder in which the program is unable to create Files, the user will be notified immediately and will not be able to proceed until he chooses another Folder.
I thought I’d use one try/catch
as if/else
to know if the File can be created or not, placing inside this try/catch
one new FileOutputStream(pathDaPastaEscolhida);
, the problem is that if Do Not Launch Exception, the File is immediately created (without asking for confirmation from the user and without giving him the opportunity to choose another Folder before effectively creating the File).
The File should only be created when the user clicks "Next", and the "Next" button should be disabled until the program is sure that it is able to create the File in the Folder chosen by the user.
I created a compileable sample code to help you get an idea of what I need:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class CriadorDeArquivo {
public static void main(String[] args) {
File novoArquivo1 = new File("C:\\Users\\Public\\Documents\\novoArquivo.txt");
criarArquivoSeForPossivel(novoArquivo1); //Cria o Arquivo como esperado
File novoArquivo2 = new File("C:\\novoArquivo.txt");
criarArquivoSeForPossivel(novoArquivo2); //ERRO: Lança uma Exception ao invés de mostrar a "Mensagem2"
}
public static void criarArquivoSeForPossivel(File novoArquivo) {
if (isPodeSerCriado(novoArquivo)) {
System.out.println("Com certeza é possível criar o Arquivo neste local, ele será criado..."); //Mensagem0
criarArquivo(novoArquivo);
System.out.println("O Arquivo foi Criado!"); //Mensagem1
} else {
System.out.println("O Arquivo não pode ser criado nesse local, escolha outro local."); //Mensagem2
}
}
public static void criarArquivo(File novoArquivo) {
try {
new FileOutputStream(novoArquivo); //Cria o Novo Arquivo na Pasta
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static boolean isPodeSerCriado(File arquivo) {
//O que colocar aqui para determinar se esse arquivo pose ser criado ou não?
return true;
}
}
I had already tried this method, but it always returns false because it checks if the File already exists, as described in the documentation:
return true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise.
; I mean, it doesn’t work to check if I can create a file, only works to check if I can overwrite an existing file.– Douglas
@Douglas see the issue, try it with her.
– user28595
System.out.println(new File("C:\\").canWrite());
returnedtrue
instead of returningfalse
. That is, he says that "you can write in C:" but when creating a file in "C:" he launches an Exception.– Douglas
@Douglas I edited the answer, try with edited code, this previous solution was quite flawed even.
– user28595
Files.isWritable(new File("C:\\Users\\Public\\Documents\\").toPath());
returned true correctly andFiles.isWritable(new File("C:\\").toPath());
returned false correctly. It looks like this will solve the problem, I will do more tests later and I accept your reply if you confirm. Thank you very much.– Douglas
isPodeSerCriado
- I don’t know if you killed the Portuguese language or the English language with this. Maybe both. However, keep my +1 anyway. :)– Victor Stafusa
@Victorstafusa I just copied the method in the question code, I thought to comment, but I was afraid of sinning for excess of preciousness in fixing it.
– user28595
@Sorry, I read the question in a hurry, because it was already answered.
– Victor Stafusa