Office Hours — Today, November 20

Yesterday, November 19

Nov 20
9:55 AM
Mark M.
has entered the room
Mark M.
turned on guest access
10:00 AM
Guy
has entered the room
Mark M.
hello, Guy!
how can I help you today?
Guy
hi :-)
Mark M.
do you have a question?
10:05 AM
Guy
sorry about that
my internet went loopy
Mark M.
that happens from time to time :)
10:10 AM
Guy
well I rewrote my currency app. used as you advised FILE I/O
if I want to check the "freshness" of the file downloaded, should I check when was it last modifed?
Mark M.
do you modify the file yourself?
Guy
no
I download it from the internet
Mark M.
then the last-modified timestamp should be stable
if you are worried about something touching the file and updating that timestamp after the download, you will need to do something else
Guy
I use it as a satstic file
10:15 AM
Mark M.
if you have any other questions, go ahead -- it's a quiet chat room today
Guy
View paste
		File file = new File("currency.xml");
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.UK);
		Log.i(LOGTAG, "Last Modified " + sdf.format(file.lastModified()));
should it be like that?
Mark M.
well, lastModified() is how you get the last-modified time of a File
however, that returns a long
10:20 AM
Mark M.
format() does not take a long
it takes a Date
you would use format(new Date(file.lastModified())) AFAIK
and I am not sure what the SimpleDateFormat is for, other than for logging purposes
Guy
thanks. I'll figure that out later :-)
the next question is about closing the file stream
10:25 AM
Guy
(line 40) FileInputStream
do I need to close() it?
Mark M.
you should
I think the standard pattern would have your lines 35-40 above the try
and then use a finally {} block to close the stream
it may be you wind up needing two nested try {} blocks, as openFileInput() could fail with a FileNotFoundException
Guy
OK
Devin F.
has entered the room
10:30 AM
Guy
lines 35-40 eclipse gone red "Unhandled exception type XmlPullParserException" demannds try catcg
Mark M.
yeah, you will need nested try {} blocks there, then
let me take a question from Devin, and I'll be back with you in a bit
Devin: hi! do you have a question?
Guy
OK :-)
Devin F.
Mostly I was checking how the chat worked. The two comments I have currently are my subscription is still marked as one year too long. and teh second is that my book isn't opening from the PDF link.
I'm not sure this is teh correct forum for either
Mark M.
both are better handled via emails to wares@commonsware.com
Devin F.
excellent. Thanks.
Mark M.
with respect to the subscription length, for the few people who were double-billed before I improved the site, I just elected to give you the extra year for the annoyance and hassle I put you through
with respect to the PDF, if you can send me an email with more details, I can help you better there
the chats are for help with Android development questions
Devin F.
thanks heaps. I'll come by for office hour development questions as soon as I'm further along in the book.
Mark M.
Guy: do you have another question?
10:35 AM
Guy
not at the moment. Thank You :-)
Mark M.
OK, well, if either of you have a question, feel free to chime in
10:40 AM
Guy
View paste
how do I close 			FileInputStream stream = context.openFileInput("currency.xml");
Mark M.
stream.close()
Guy
i thought stream.close();
Mark M.
Guy
View paste
		} finally {
			stream.close();
		}
Mark M.
you need to make sure that stream is declared and initialized outside the try {} associated with the finally {}
10:45 AM
Guy
View paste
		FileInputStream stream = null;
 try...
View paste
		} finally {
			if (stream != null){
				try {
					stream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
like this
?
Mark M.
depending upon your overall structure, you may not need the try/catch around close(), if there is an outer try that would catch the exception
otherwise, generally yes
Guy
there is one
Mark M.
note that this is not Android-specific
other than how you got the stream in the first place (openFileInput())
this is standard Java file I/O
Guy
yes
View paste (27 more lines)
FileInputStream stream = null;
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser xpp = factory.newPullParser();

			// InputStream stream = context.getResources().openRawResource(R.raw.currency);
			stream = context.openFileInput("currency.xml");

			xpp.setInput(stream, null);

			int eventType = xpp.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {
					handleStartTag(xpp.getName());
...
Mark M.
unless there is a try around all of that, you will need the try inside the finally block
10:50 AM
Devin F.
has left the room
Guy
thanks.
see you soon.
11:00 AM
Mark M.
that's a wrap for today's office hours chat
the transcript will be posted to http://commonsware.com/office-hours/ shortly
the next chat is tomorrow at 7:30pm Eastern
have a pleasant day!
Guy
has left the room
Mark M.
turned off guest access

Yesterday, November 19

 

Office Hours

People in this transcript

  • Devin Fitzsimons
  • Guy
  • Mark Murphy