[英]How can I make a directory in Java?
我正在嘗試創建一個新的文件目錄,但是功能mkdir()
不起作用, mkdirs()
也mkdirs()
。
這是我的代碼:
...
while (leitor.hasNext()){
String [] plv = LerPalavras(tamMem, leitor);
Arrays.sort(plv);
String nomeTemp = "/temp/temp" + contador + ".txt"; // I need to create this directory
try{
escritor = new FileWriter(nomeTemp);
for (int i = 0; i < tamMem; i++) {
escritor.write(plv[i] + " ");
}
escritor.close();
} catch (IOException e){
System.out.println(e.getMessage());
}
contador++;
}
...
編輯:我進行了編輯,現在可以使用了!
File pastaTemp = new File("/temp/temp");
pastaTemp.mkdirs();
while (leitor.hasNext()){
String [] plv = LerPalavras(tamMem, leitor);
Arrays.sort(plv);
File arqTemp = new File (pastaTemp, contador + ".txt");
try{
escritor = new FileWriter(arqTemp);
for (int i = 0; i < tamMem; i++) {
escritor.write(plv[i] + " ");
}
escritor.close();
} catch (IOException e){
System.out.println(e.getMessage());
}
contador++;
}
嘗試分兩個步驟進行。 首先,如有必要,調用File.mkdirs()
創建整個目錄結構,然后創建傳遞給FileWriter
的文件:
try {
File folder = new File("/temp/temp");
folder.mkdirs();
// then create a file object at this location
File file = new File(folder, contador + ".txt");
escritor = new FileWriter(file);
// the rest of your code
}
catch (Exception e) {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.