2014-07-21 28 views
1

我在讀值從屬性文件如下:請與名稱的目錄保存在一個字符串

public class Backup { 
public static void main(String[] args) { 

    Properties prop = new Properties(); 
    try{ 
     //load properties file for reading 
     prop.load(new FileInputStream("src/com/db_backup/db-backup_config.properties")); 

     String password = prop.getProperty("db.password"); 
     String port = prop.getProperty("db.port"); 
     String name = prop.getProperty("db.name"); 
     String userid = prop.getProperty("db.userid"); 
     String tables = prop.getProperty("db.tables"); 
     String host = prop.getProperty("db.host"); 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 

    System.out.println(); 
} 

}

我想使存儲在字符串用戶ID名稱的目錄。我怎麼能這樣做?這也是閱讀屬性文件的最佳方式嗎?

回答

1

您可以創建使用Java這樣的目錄 -

File file = new File("C:\\dir"); 
    if (!file.exists()) { 
     if (file.mkdir()) { 
      // success 
     } else { 
      // failure 
     } 
    } 

和關於閱讀的特性,它的通常按照你所做的方式去做。

+0

我設法找出目錄,但仍然不確定屬性。謝謝! – idaWHALE

0

爲了讓您將創建一個使用用戶ID字符串像一個文件對象的目錄:

File f = new File(userid); 

現在你想一個目錄出來的,如果它不存在。

if(!f.exists()) { 
    f.mkdir(); 
} 
0

如果我理解你的問題,你可以使用File#mkdir()像這樣,

File f = new File(userid); 
if (f.exists()) { 
    if (f.isDirectory()) { 
    System.out.println(f.getPath() + " already exists"); 
    } else { 
    System.out.println(f.getPath() + " (non-directory) already exists"); 
    } 
} else { 
    if (f.mkdir()) { 
    System.out.println(f.getPath() + " created"); 
    } else { 
    System.out.println(f.getPath() + " not created"); 
    } 
} 
0

我繼續我的小提琴演奏,並能做到這一點的:

File theDir = new File(host); 
     if(!theDir.exists()) { 
      System.out.println("Creating Directory: " + host); 
      boolean result = false; 

      try{ 
       theDir.mkdir(); 
       result = true; 
      } catch (SecurityException se){ 
       //handle 
      } 
      if(result){ 
       System.out.println("DIR created"); 
      } 
     } 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 

感謝那些誰回答。

相關問題