2012-03-12 10 views
18

Sto leggendo un file di proprietà dal database. Ho controllato java.util.Properties e non c'è alcun metodo per analizzare da un'istanza String. C'è modo di farlo?Analisi della stringa come proprietà

+0

Qual è il formato di file delle proprietà? – Ishmael

+1

Un file di proprietà o proprietà da un DB ?? – Dan

risposta

65

Hai ragione che java.util.Properties non dispone di un metodo per leggere da un String - ma in realtà ha metodi più generali che leggono da un InputStream o Reader.

Quindi è possibile chiamare load se si dispone di un modo di presentare il proprio String come uno di questi, vale a dire una fonte che itera efficacemente sui caratteri uno per uno. Questo sembra che dovrebbe esistere, e in effetti lo fa - java.io.StringReader.

Mettere insieme, quindi, è molto semplice:

public Properties parsePropertiesString(String s) { 
    // grr at load() returning void rather than the Properties object 
    // so this takes 3 lines instead of "return new Properties().load(...);" 
    final Properties p = new Properties(); 
    p.load(new StringReader(s)); 
    return p; 
} 
+1

Grazie andrzej, questo mi ha aiutato solo dopo ore di tentativi di conversione e file di oggetti Amazon S3 in modo dinamico con jets3t, fantastico e così dannatamente semplice. – oberger

0

Abbiamo avuto un problema simile, quanto sopra non ha funzionato per noi.

Il seguente, tuttavia, ha fatto.

def content = readFile 'gradle.properties' 

Properties properties = new Properties() 
InputStream is = new ByteArrayInputStream(content.getBytes()); 
properties.load(is) 

def runtimeString = 'SERVICE_VERSION_MINOR' 
echo properties."$runtimeString" 
SERVICE_VERSION_MINOR = properties."$runtimeString" 
echo SERVICE_VERSION_MINOR 
+0

Questo in realtà non risponde alla domanda. Se hai una domanda diversa, puoi richiederla facendo clic su [Invia domanda] (http://stackoverflow.com/questions/ask). Puoi anche [aggiungere una taglia] (http://stackoverflow.com/help/privileges/set-bounties) per attirare maggiormente l'attenzione su questa domanda una volta che hai abbastanza [reputazione] (http://stackoverflow.com/help/ che cosa è-la reputazione). - [Dalla revisione] (/ recensione/post di bassa qualità/13343062) –

+0

Quanto sopra risponde molto alla domanda di analizzare una stringa come proprietà. - Prendiamo un file di stringhe - analizzarlo - Impostare Strings nelle proprietà dell'oggetto pari a groovy oggetti che possono essere riutilizzati in qualsiasi parte del Pipeline – user3265317

1

Io uso questo codice per caricare oggetti di una singola colonna DB

public Properties buildProperties(String propertiesFromString, String entrySeparator) throws IOException { 
    Properties properties = new Properties(); 
    properties.load(new StringReader(propertiesFromString.replaceAll(entrySeparator, "\n"))); 
    return properties; 
} 

con un semplice test

@Test 
public void testProperties() throws Exception { 
    Properties properties = buildProperties("A=1;B=2;Z=x",";"); 
    assertEquals("1", properties.getProperty("A"));   
    assertEquals("2", properties.getProperty("B"));   
    assertEquals("3", properties.getProperty("C","3"));   
    assertNull(properties.getProperty("Y")); 
    assertEquals("x", properties.getProperty("Z")); 
} 
Problemi correlati