• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: cocoa-dev digest, Vol 1 #468 - 14 msgs
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cocoa-dev digest, Vol 1 #468 - 14 msgs


  • Subject: Re: cocoa-dev digest, Vol 1 #468 - 14 msgs
  • From: Mark Morris <email@hidden>
  • Date: Sun, 26 Aug 2001 11:56:52 -0500

Rolando,

There might be a better way, but here's a method that performs a usually useful subset of
dictionaryWithContentsOfFile:. It makes use of java.util.PropertyResourceBundle. (This was
done in WO 4.5 Java, but I'm assuming it would still work.)

Regards,
Mark

--
import com.apple.yellow.foundation.*;
import java.io.*;
import java.util.*;

public static NSDictionary dictionaryFromFile( String path ) throws IOException {
// Will do its best to convert a file given my "path" into a dictionary, with each line
translated into
// a key/value pair. Examples of valid lines:
//
// aKey=aValue
// anotherKey=A longer value.
//
// As far as I can tell, the values are all treated as Strings.

File file;
FileInputStream content = null;
PropertyResourceBundle myBundle = null;

try {
file = new File( path );
content = new FileInputStream( file );
myBundle = new PropertyResourceBundle( content ); // get the bundle containing the info

}
catch (FileNotFoundException e1) {
System.out.println( "File not found for path " + path + "." );
throw e1;
}
catch (IOException e2) {
System.out.println( "I/O error for path " + path + ": " + e2.toString() );
throw e2;
}

NSMutableDictionary returnDictionary = new NSMutableDictionary();

// for each bundle key, transfer the key/value pair to the return dictionary
Enumeration myEnum = myBundle.getKeys();
while( myEnum.hasMoreElements() ) {
String aKey = (String)myEnum.nextElement();
returnDictionary.setObjectForKey( myBundle.handleGetObject( aKey ) , aKey );
}

content.close(); // close the file

return new NSDictionary( returnDictionary );

}

> --__--__--
>
> Message: 1
> Date: Sat, 25 Aug 2001 16:41:28 -0400
> To: email@hidden
> From: Rolando Abarca <email@hidden>
> Subject: NSDictionary and Java
>
> I looked in the documentation for a way to initialize a NSDictionary
> from a plist (like the Objective-C equivalent
> dictionaryWithContentsOfFile:) but I couldn't find anything... is
> there a way to do this with java?
> thanks a lot,
> --
> Rolando Abarca
> Tel: (56 9) 8641496 / (56 2) 3216427
> FAX: (56 2) 3216451
> ------


  • Prev by Date: Re: Port and runloop problem
  • Next by Date: Re: Docs, questions and stuff
  • Previous by thread: Re: NSThread
  • Next by thread: Testing if a folder is a bundle
  • Index(es):
    • Date
    • Thread