Java I/O Tutorial

Java comes with many handy I/O classes to support the input and output through bytes stream and file system. Here’s a list of the Java I/O examples including file, temporary file and directory manipulation, encoding, serialized and also compression with zip or Gzip.
Happy learning Java I/O :)
File
List of the File examples to show the use of Java I/O to create, read, write, modify file and get the file’s information.
- Create a file
- Construct file path
- Set the file permission
- Read file with BufferedInputStream
- Read file with BufferedReader
- Write file with FileOutputStream
- Write file with BufferedWriter
- Append content to file
- Delete a file
- Delete files with certain extension only
- Find files with certain extension only
- Rename a file
- Copy a file
- Move a file to another directory
- Get the file creation date
- Get the file last modified date
- Change the file last modified date
- Make a file read only
- Get file size
- Get the filepath of a file
- Get the total number of lines of a file
- Check if a file exists
- Check if a file is hidden
- Read UTF-8 encoded data from a file
- Write UTF-8 encoded data into a file
- Assign file content into a variable
- Generate a file checksum value
- Convert File into an array of bytes
- Convert array of bytes into File
- Convert String to InputStream
- Convert InputStream to String
- Convert File to Hex
- Get total of free disk space in your partition or volume
File Serialization
Take any object that implements the Serialization interface, convert it into bytes and store it into file; The file can be fully restore back to the original object later.
File Compression
File compression examples, ZIP and GZip.
- Compress files in ZIP format
- Decompress files from a ZIP file
- Compress a file in GZIP format
- Decompress file from GZIP file
- Compress serialized object into file
- Decompress serialized object from a Gzip file
Temporary File
List of the temporary file manipulation examples.
Directory
List of the directory manipulation examples.
- Create directory
- Delete directory
- Copy directory
- Traverse /List a directory structure
- Check if directory is empty
- Get the current working directory
Console IO
List of the Console IO examples.

the tutorial has been helpful to me, but i wanted to if it is possible to writes more than an object into a file and read them back successfully.
thank you.
Hi! Yong
Thanks to illustrate this examples.
I have one question
Q:I need to create a log file which contains all the details of query done by user1 in database1 operation in oracle editor even give time and date of each query details.And when i login to another db2 and capable to fetch that log file and fired them and it could easily be capable to stored each record as it was happening in db1.
note:columns name of each table is same either db1 or db2 or db3…..
hope u will provide me the whole code in full explanations in java and database oracle
Thanx Yong
How to prevent file path manipulation as it is security issue
Hi
For every thing we have an example but not much explanation.
Sir,i want the code for these questions.I tried in many ways and didn’t get the accurate result.Please do the needful.
1)Your input will given in a text file (input.txt).
Your program should create an output file (output.txt).
Every line in the file represents a test case for which you should have a corresponding line as result in the output file.
Every line will be a mathematical expression that will be given with braces. The operators that will be used are only +,*,/,- (bodmas priority). The braces that
will be used are [],() (in that order of priority). The program should provide the ouput for each experssion in the output file if the syntax of the expression is
correct. If incorrect syntactically, “invalid” should be written.
Ex:
[1+2*(3+5)/4] => 5
[1+2*(3+5/5] => invalid
[1+2*3/6] => 2
2)A text file is given as input.
You have to arrange the strings by reordering them in the following manner:
For every word that ends with a letter,
the next word should start with the same letter.
The words that do not match must be written at the end of the file.
3)Read the contents of a text file (input.txt). The words should be rearranged and written to output.txt.
First find the number of occurances of each word. Then write to the file in the descending order of the number of
occurances. If any word occurs prime number of times, then reverse the strings and write it to the file.
Also print the words that contain non-repeating letters to the console.
This is an amazing site. lot of examples.
Can you also post some collection related examples.
Thanks,
Gopi.
How to print output in different color on console using java program ?
Very impressive java tutorial.
Thanks
So hello i have one question about nexttoken look
String test = st.nextToken();
String test2 = st.nextToken();
if test2 is empty how can i check if test 2 is empty and put it something like test2 = “.”;
Please help me…..
is this what you are looking for ?
Awesome .. i have become fan of u … yong
Great posts for IO tutorials, I found another link with great java io tutorials.
http://www.journaldev.com/942/java-io-tutorial
ThanQ sir,it is very helpful to me and
can you please provide me the ORACLE & FORMS understandable website…..
try dba-oracle.com
Ya This is very useful to Develope in my “PROJECT”
THANKS TO:MKYONG
Regards:
V.G.Fernandas.BE
(SOftware Engg)
This is a very helpful site .. Thank You to help us.
hi
i have to encoded chines charactor but its can not decode its. any idea
Can I get more examples in regular expressions with I/O operations
Hi,
Really a good site.Just want, your answers or post should be more elaborative.
Thnks
hello Mr.
i want to do how can get the file size that stored in internet Before download
pleas email it for me.
Hi… i’m just a beginner of java. i don’t know how to start it from your website. (Heard that, mkyong is a best website for learning programming skills)
these are very valuable examples…easy to follow
thanks sir
Hi, I want the code which meets the following requirement.
If I give the username as password, it should able to give write (access) permission to the folders in my D: drive to that user.
Could you please help me as soon as possible.
Thank you in advance.
hi i need to different between spring and hibernt
Hi Sridhar
Hibernate is a ORM(Object Relational Mapping) tool used to deal with database,to overcome the problems associated with JDBC such as result set iteration,remembering column name ,connection pooling and so on.
Spring-core is business core framework which deals with dependency injection ,aspect oriented programming.One of the main features of spring is ,it supports JDBC template to deal with database.
Spring core also focuses on Transaction Management.
Respected sir,
I have searched very much but i did not come to know yet that how to read a audio file in java.
thanking you,
Playing audio clips in applets is quite simple and involves the following steps:
Create an AudioClip object
Load .au sound file into AudioClip
Play sounds once or loop continuously
Stop playback
This site is awesome for learning java.. Great Man !!
you can check it for basic learning in java
Hi. i have interest in java. but i have no knowledge about it. can u pls put me through.Thanks
How could i help?
I have basic knowledge of C++, Should I start practicing java basics
Yes, you should :)
Hi,
I want to create a java file for inserting a tag into xml file like below
hhh
i want the output as like above and i don’t know how to attach my code to dis can u plz help me
you can learn java easily but you should practice………….for it
Hi,
As a novice of Java, the articles in your website are useful to me. Can I translate and publish them in my network?
Thinks.
Yours.
Yes, you are free to translate it. But, with a credit link back to original post.
Hi Yong,
http://www.mkyong.com is my favourite site and I strictly followed and learned Spring AOP 2.5. As per my current project I need to convert single threaded application to multithreading. can you please add some notes/recommendations for that. I’m a beginner for threading and never implemented in my whole career
Thanks
Sudar
This are valuable example as well as valuable tutorial.
Hi Yong,
Thanks for uploading all the example programs on the java.
Is that possible for you to upload Collection framework examples.
So that it helpful to every one to understand the some real time applications.
Thanks in advance.
Regards,
Satya.