Algorithmics 3 Files November 14th, 2006

advertisement
Algorithmics 3
Algorithmics 3
Files
November 14th, 2006
Algorithmics 3
The story so far
The story so far
A quick word about comments
File management
File access
Conclusion
Algorithmics 3
The story so far
What we've discussed
Input streams
Output streams
File-related exceptions
Algorithmics 3
The story so far
What we've discussed
Input streams For reading the contents of a le
Output streams
File-related exceptions
Algorithmics 3
The story so far
What we've discussed
Input streams For reading the contents of a le
Output streams For writing something inside a le
File-related exceptions
Algorithmics 3
The story so far
What we've discussed
Input streams For reading the contents of a le
Output streams For writing something inside a le
File-related exceptions In case of problem
Algorithmics 3
The story so far
Where do les live ?
File system
Virtual File System
Pseudo-les
Algorithmics 3
The story so far
Where do les live ?
File system i.e. on your hard drive, your disk, your mp3 player. . .
Virtual File System
Pseudo-les
Algorithmics 3
The story so far
Where do les live ?
File system i.e. on your hard drive, your disk, your mp3 player. . .
Virtual File System i.e. inside another le (iso images, .zip. . . ), around
the network . . .
Pseudo-les
Algorithmics 3
The story so far
Where do les live ?
File system i.e. on your hard drive, your disk, your mp3 player. . .
Virtual File System i.e. inside another le (iso images, .zip. . . ), around
the network . . .
Pseudo-les keyboard, text screen, low-level network, even the
mouse. . .
Algorithmics 3
The story so far
Where do les live ?
File system i.e. on your hard drive, your disk, your mp3 player. . .
Virtual File System i.e. inside another le (iso images, .zip. . . ), around
the network . . .
Pseudo-les keyboard, text screen, low-level network, even the
mouse. . .
TCP/IP web sites, e-mail, . . .
Algorithmics 3
The story so far
Where do les live ?
File system i.e. on your hard drive, your disk, your mp3 player. . .
Virtual File System i.e. inside another le (iso images, .zip. . . ), around
the network . . .
Pseudo-les keyboard, text screen, low-level network, even the
mouse. . .
TCP/IP web sites, e-mail, . . .
The point is that you can use the same methods for all of these entities.
Algorithmics 3
The story so far
What for ?
Q By the way, what do you wish to do with les ?
Algorithmics 3
The story so far
What for ?
Q By the way, what do you wish to do with les ?
A Broadly, two categories:
File management Copying, moving, renaming, erasing, sharing. . .
File access Reading the contents, writing the contents of a le. . .
Algorithmics 3
The story so far
Exceptions
Remember ?
// i n s i d e
your
try
method
{
// do
something
// do
something
// k e e p
}
catch
}
catch
}
catch
}
finally
else
it
until
( ExceptionType1
// i n
e_1 )
exceptional
( ExceptionType2
// i n
something
exceptional
exceptional
any
case ,
do
do
something
2,
do
something
3,
do
something
{
case
{
// i n
1,
{
case
e_3 )
f a i l s
{
case
e_2 )
( ExceptionType1
// i n
}
doing
this
Algorithmics 3
The story so far
Today's program
I Commenting your source.
I Managing les.
I Accessing les.
Algorithmics 3
A quick word about comments
The story so far
A quick word about comments
File management
File access
Conclusion
Algorithmics 3
A quick word about comments
Commenting is good
Commenting is a good practice. Really.
Algorithmics 3
A quick word about comments
Commenting is good
Commenting is a good practice. Really.
Let's take a look at some uncommented source. . .
Algorithmics 3
A quick word about comments
Types of comments
There are typically two kinds of comments:
Algorithmics 3
A quick word about comments
Types of comments
There are typically two kinds of comments:
public documentation What someone needs to know to use your classes
and methods.
Algorithmics 3
A quick word about comments
Types of comments
There are typically two kinds of comments:
public documentation What someone needs to know to use your classes
and methods.
maintainer documentation What someone needs to know to modify your
algorithms.
Algorithmics 3
A quick word about comments
Maintainer documentation
Typically, add a comments column, around column 60 or so, starting each
line of the column with
//.
Algorithmics 3
A quick word about comments
Maintainer documentation
Typically, add a comments column, around column 60 or so, starting each
line of the column with
//.
I Anything you don't consider trivial should be documented.
I If you had to make a choice, document it.
I If something doesn't work, document it.
I If you have found a non-trivial workaround, document it.
I If you think you're going to forget something, document it.
I If whoever reads the code needs to understand how something else
works, document it.
Algorithmics 3
A quick word about comments
Maintainer documentation
Typically, add a comments column, around column 60 or so, starting each
line of the column with
//.
I Anything you don't consider trivial should be documented.
I If you had to make a choice, document it.
I If something doesn't work, document it.
I If you have found a non-trivial workaround, document it.
I If you think you're going to forget something, document it.
I If whoever reads the code needs to understand how something else
works, document it.
I Don't forget, maintainer documentation should make things easier
to understand and modify !
Algorithmics 3
A quick word about comments
Public documentation
Public documentation (or API documentation) is what you're reading on
JavaSoft's website.
Algorithmics 3
A quick word about comments
Public documentation
Public documentation (or API documentation) is what you're reading on
JavaSoft's website.
They should contain everything a programmer needs to know to use your
code without reading it.
Algorithmics 3
A quick word about comments
Public documentation
Public documentation (or API documentation) is what you're reading on
JavaSoft's website.
They should contain everything a programmer needs to know to use your
code without reading it.
It's often a good idea to write these comments before actually writing the
code. It's a good manner of organising your thoughts, or of nding out
you need something.
Algorithmics 3
A quick word about comments
JavaDoc
JavaDoc is a tool used to transform your public documentation in web
pages. It's the tool used by Sun to document Java's libraries.
Algorithmics 3
A quick word about comments
JavaDoc
JavaDoc is a tool used to transform your public documentation in web
pages. It's the tool used by Sun to document Java's libraries.
To use JavaDoc, read its manual or use Dr.Java's
Tools
menu.
Algorithmics 3
A quick word about comments
Writing JavaDoc
JavaDoc comments start with
/**
and end with
*/.
They should be placed immediately before the name of the
class/interface/method/constructor you're documenting.
Algorithmics 3
A quick word about comments
JavaDoc contents
JavaDoc understands
Algorithmics 3
A quick word about comments
JavaDoc contents
JavaDoc understands
I Paragraphs (the rst paragraph should be a general description, the
following paragraphs should be more precise).
I
I
I
I
@author to give the name of the author
@param to explain the role of a parameter of a method/constructor
@return to explain the return value of a method
@throws to explain any exception that a method/constructor may
throw
I
I
I
@see to invite the reader to also read some other documentation
@version to document changes between versions
@deprecated to tell programmers not to use your
method/constructor/... anymore.
Algorithmics 3
A quick word about comments
More about JavaDoc
I It's a good idea to
always
document your code.
I It's a good idea to
always
document each parameter, return
argument and thrown exception.
I There's almost always something pertinent to write. Write that
down rather than something trivial.
Algorithmics 3
A quick word about comments
More about JavaDoc
I It's a good idea to
always
document your code.
I It's a good idea to
always
document each parameter, return
argument and thrown exception.
I There's almost always something pertinent to write. Write that
down rather than something trivial.
Documentation is not necessary for the code to compile or to run.
Algorithmics 3
A quick word about comments
More about JavaDoc
I It's a good idea to
always
document your code.
I It's a good idea to
always
document each parameter, return
argument and thrown exception.
I There's almost always something pertinent to write. Write that
down rather than something trivial.
Documentation is not necessary for the code to compile or to run. It's
necessary for the brain to compile and run.
Algorithmics 3
File management
The story so far
A quick word about comments
File management
File access
Conclusion
Algorithmics 3
File management
Managing les
Before doing anything with a le, you need to build a
representing your le.
File
object
Algorithmics 3
File management
Managing les
Before doing anything with a le, you need to build a
representing your le.
See the documentation. Note that
Files
live in
File
java.io.
object
Algorithmics 3
File management
Managing les
Before doing anything with a le, you need to build a
representing your le.
See the documentation. Note that
See the code.
Files
live in
File
java.io.
object
Algorithmics 3
File management
Now what ?
Base operations:
createNewFile
Creating a directory mkdir
Destroying a le delete
Determining whether the le exists exists
Getting the containing directory getParentFile
Moving or renaming renameTo
Creating an empty le
Algorithmics 3
File management
More operations
Directories are specic les.
Algorithmics 3
File management
More operations
Directories are specic les.
isDirectory
Getting all les from the directory listFiles
Listing all root directories listRoots
Determining whether a le is a directory
Algorithmics 3
File management
More operations
Directories are specic les.
isDirectory
Getting all les from the directory listFiles
Listing all root directories listRoots
Determining whether a le is a directory
See example.
Algorithmics 3
File management
Choosing les !
You can also open a le from the UI, with
JFileChooser.
Algorithmics 3
File access
The story so far
A quick word about comments
File management
File access
Conclusion
Algorithmics 3
File access
We're going to need streams
Let's take another look at the documentation.
Algorithmics 3
File access
What now ?
Let's look at an example.
Algorithmics 3
File access
Other kinds of streams
This was a simple stream for writing to a le.
Algorithmics 3
File access
Other kinds of streams
This was a simple stream for writing to a le.
We could have used
I network streams
I compression streams
I cryptographic streams
I ...
Algorithmics 3
File access
Other kinds of streams
This was a simple stream for writing to a le.
We could have used
I network streams
I compression streams
I cryptographic streams
I ...
For all this, read the documentation.
Algorithmics 3
File access
What about using the data ?
Input Streams can be used to read interesting data i.e. data you're
going to use in your application.
Output Streams can be used to write interesting data i.e. data you've
been using in your application.
Algorithmics 3
File access
What about using the data ?
Input Streams can be used to read interesting data i.e. data you're
going to use in your application.
Output Streams can be used to write interesting data i.e. data you've
been using in your application.
In particular, that's basically how OpenOce, Word and others load
documents and save the documents.
Algorithmics 3
File access
What about using the data ?
Input Streams can be used to read interesting data i.e. data you're
going to use in your application.
Output Streams can be used to write interesting data i.e. data you've
been using in your application.
In particular, that's basically how OpenOce, Word and others load
documents and save the documents.
See
java.util.Properties.
Algorithmics 3
Conclusion
The story so far
A quick word about comments
File management
File access
Conclusion
Algorithmics 3
Conclusion
Bottom line
We've seen
I how to comment your source code
I how to manipulate les
I how to read the contents of les
I how to write the contents of les
I how to use something of the contents of les.
Algorithmics 3
Conclusion
Next week
Collections !
Download