Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 4
Registered: ‎10-02-2010
My Device: Not Specified
Accepted Solution

Reading file from same directory

hi

i have an application which uses a config file

i put the config file in the same directory as the  application(one folder below src and res)

when i start my application on the simulator i want to try and read the file

how can i read the file just by giving its name

is it possible with fileConnection to do that.

and if not i have no idea what is the full path of my application in the simultor

how can i know it?

thx

Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Reading file from same directory

I put my config files in teh same directory where a class need it (So I have only one class that consume the file, exposing its content as properties of the class). The code I use is this:

 

InputStream is = null;
try {
  is = this.getClass().getResourceAsStream("config.txt");
  LineReader lineReader = new LineReader(is);  
 _url = new String(lineReader.readLine());
} catch (IOException e) {
  e.printStackTrace();
} finally {
  if ( is != null) {
     is.close();
  }
}
			 

 

 

New Contributor
Posts: 4
Registered: ‎10-02-2010
My Device: Not Specified

Re: Reading file from same directory

that helped thx

anyway to do it with fileConnection?

Developer
Posts: 1,805
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.997 Dev OS, Z30 (STA100-5)-10.3.1.997 Dev OS, Passport (SQW100-1)-10.3.0.1418, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Reading file from same directory

No, BB apps are not really "stored" in a directory. If your project has a file in it then it will be included with the COD file but you can only read it and you can only access it with getResourceAsStream.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Reading file from same directory

I have some files in the SDCard and I read using this code:

 

// Carga imagen de SDCard
String filename = file:///SDCard/BlackBerry/pictures/lb/1.png"; 
FileConnection fc = null;
InputStream input = null;
try {
	fc = (FileConnection) Connector
			.open(filename, Connector.READ_WRITE);
	if (fc.exists()) {
		input = fc.openInputStream();
		byte[] data = new byte[(int) fc.fileSize()];
		input.read(data);
		image = EncodedImage.createEncodedImage(data, 0, data.length);
	}
} catch (Exception e) {
	System.out.println("MLS:" + e);
} finally {
	
	if (input != null) {
		try { 
			input.close();
		} catch (IOException e) {}
	}
	
	if (fc != null) {
		try {
			fc.close();
		} catch (Exception e) {
		}
	}
}

 

 

New Contributor
Posts: 4
Registered: ‎10-02-2010
My Device: Not Specified

Re: Reading file from same directory

thx all for the reply

i will use get resource as stream form now on

 

 

New Contributor
Posts: 4
Registered: ‎10-02-2010
My Device: Not Specified

Re: Reading file from same directory

hi

on the same subject

how do i get on output stream to write to a file in my directory?

i only get inout stream with this method

thx

BlackBerry Development Advisor
Posts: 15,075
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: Reading file from same directory

There is no way to write a file within the COD file of your application (getResourceAsStream method).  But you can create a file using a FileConnection.  Have a look at the fileexplorerdemo included with the BlackBerry Java SDK and BlackBerry JDE for an example.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker