2014-07-21 30 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(); 
    } 

感谢那些谁回答。

相关问题