1000 Java Tips ebook
Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java
programming articles and tips. Please take your copy here.
Take your copy of free "Java Technology Screensaver"!.
Easy Learn Java: Programming Articles, Examples and Tips - Page 475
1060 Stories (530 Pages, 2 Per Page)
How to open, read, write, close file(s) in Java? Examples on move, rename and de
Question: How can I open, read, write and close files in Java? Can I move, rename and delete file in Java? Examples? Can I filter files by file's extension?
Answer: Yes you can do all usual operations with files in Java: open, read, write, close, move, rename and delete.
You can even filter files by file's extension. All the file operations most easy to do in Java application. By default it is not for applets. If you try to open a file by applet you will get such or similar error (depends on Java version):
java.security.AccessControlException: access denied (java.io.FilePermission yourfilename read) at java.security.AccessControlContext.
checkPermission(AccessControlContext.java:270) at java.security.AccessController.
checkPermission(AccessController.java:401) at java.lang.SecurityManager.
checkPermission(SecurityManager.java:542) at java.lang.SecurityManager.
It is possible to open files with applet but user must explicitly permit it to Security manager (you write this functionality). So if you write a program which runs locally it is easier to write an application instead of applet.
File operations are often OS dependant. You code which works on Windows can stop to work on UNIX-like OS's due to higher security. Files which are in mounted directories (look like they are here on local file system, but are far away on the other side of network) also can cause many troubles.. So be ready to test all the cases if you write your code for different OS's and file systems.
During my latest work as Java programmer I collected a lot of useful examples for files related operations. They all working well and can give you good idea how to use file operations in Java.
By the way, I have more than 1000 Java examples on other areas of Java as well. My principle is: only working Java Code Examples. Some examples require few more classes, you can find them by powerful Java Code Search - supports boolean search, exact pattern and more.
So please take a look at File related code examples below:
You can also look at examples for FileReader, FileWriter, FileDescriptor, FileFilter, FilenameFilter, FileInputStream, FileOutputStream, FileNotFoundException
12655 bytes more | comments? | | Score: 0
Posted by Javaaddict on Wednesday, September 20, 2006 (18:00:00) (47973 reads)
How do I compare two dates and times, date between dates, time between times and
Question: How do I compare two dates and times, date between dates and so on.
Answer: Almost every month in my daily Java programming work I have this problem - to compare two or many dates or times.
Dates, Calendars are not an easy task to work with. Simple example: never do assume that all the days are 24 hours long. They are not. Some days are 25 and some 23 hours. Seems simple and obvious? Actually not. Most of people forget about it and get troubles twice a year
I collected more than 50 different only working Java code examples with Date class: everything from time, day, month, date, calendar to time synchronizing on computers by Java program (time servers and clients even). In the examples below you can see how this functionality can be used in different areas - applets, files, sound, beans, databases.
Sixth months ago the new section was started on this site - "Only working Java Code Examples". All the examples are grouped by Java technology (J2EE, J2SE, J2ME, etc.) and can be searched by powerful search engine of the site.
Look at all described above code examples here:
Date / Time Example
Getting and Setting the Modification Time of a File or Directory
Beep / Date Example
Java Statement Runner
Trivial class to show use of Date & Calendar objects
Show dates before 1970, in JDK1.0/1.1 when Date was broken
Show some date uses
Create SimpleDateFormats from a string read from a file
DateDiff -- compute the difference between two dates
An Applet to display the current time
The Date we are about to format
Read a file of reminders, run each when due using java.util.Timer
UDP Time Client
Dayclient and Dayserver - implement the Unix 'daytime' protocol.
Try Date Format - enter a time value and a SimpleDateFormat specification.
Last 24 (retrieves file if it's modified since ...)
Clock Label Test
And more examples here-> Date and here Calendar
2540 bytes more | comments? | | Score: 0
Posted by Javaaddict on Wednesday, September 20, 2006 (05:18:49) (63765 reads)