Android Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsMobile ProgrammingAndroid Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 1st, 2012, 10:23 AM
Cep's Avatar
Cep Cep is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 296 Cep User rank is Sergeant (500 - 2000 Reputation Level)Cep User rank is Sergeant (500 - 2000 Reputation Level)Cep User rank is Sergeant (500 - 2000 Reputation Level)Cep User rank is Sergeant (500 - 2000 Reputation Level)Cep User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 58 m 28 sec
Reputation Power: 14
Check, create and read from file in internal storage

Hi,

I am trying to get my android app to do the following:

1. Check to see if a file exists within the application folder on internal storage (NOT SD! NOT EXTERNAL!).

2. If the file does not exist, create it by copying the file from resources folder into the application folder.

3. Read the new file.

The following code will check and create the file if necessary. FileData is just a basic structure for holding the name and byte array data.

Code:
final static String VERSION_FILE = "content.xml";

File versionFile = new File(VERSION_FILE);

if (!versionFile.exists()) {
new CreateFile().execute(new FileData(VERSION_FILE, getResources().getXml(R.xml.content).toString().getBytes()));
}

private class CreateFile extends AsyncTask<FileData, Integer, Boolean> {
		@Override
		protected Boolean doInBackground(FileData... fd) {
			FileOutputStream fos = null;
			
			try {
				fos = openFileOutput(fd[0].getFilename(), Context.MODE_PRIVATE);
				fos.write(fd[0].getData());				
				return true;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (NotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			return false;
		}
	}


I then create an input stream for an XML parser to handle by doing the following and passing it to an input source:

Code:
InputStream is;
File f = new File(VERSION_FILE);
is = new FileInputStream(f);


However once this reaches the parser an exception is thrown to say the file does not exist.

I cannot see what I am doing wrong since the documentation on the android site and pretty much every google search I have made suggests that the file should be created or overwritten by my first few lines of code.

Reply With Quote
  #2  
Old June 1st, 2012, 04:53 PM
ServerKid ServerKid is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 4 ServerKid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 20 m 46 sec
Reputation Power: 0
i think its because you didn't specify where the file should be written. or the current location it is currently writing in doesn't have a write permission.

Reply With Quote
  #3  
Old July 5th, 2012, 02:19 AM
john29 john29 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 17 john29 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 16 m 15 sec
Number of bans: 1
Reputation Power: 0
i think its because you didn't specify where the file should be written

Reply With Quote
Reply

Viewing: Dev Shed ForumsMobile ProgrammingAndroid Development > Check, create and read from file in internal storage

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap