Java I/O Tutorial

Java I/O Tutorials

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.

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.

Temporary File

List of the temporary file manipulation examples.

Directory

List of the directory manipulation examples.

Console IO

List of the Console IO examples.

Java I/O Reference

93 comments on “Java I/O Tutorial

  1. Link to JAVA I/O Documentation is no longer available. Please fix this.

    Reply
  2. Awesome, this post has almost everything covered around file I/O along with a lot of supplement stuff. However, addressing a few more topics like md5/sha1 hashing, retrieving file time-stamp would have made it more meaningful.

    Reply
  3. Thanks for your tutorials , I wanna say that they are useful for me

    Reply
  4. Thank you very much mkyong! You helped me and other guys so much!

    Reply
  5. Hi all,

    I am a new to java. I am trying to set up the environment with Java, git, Jenkins and maven by mentioning the IP address of linux VM. I have written a code to connect to VM, create folders, and move files to folders. Now I want to install all Java and others software programmatically. Ib tried to install Java using yum -y install java-1.7.0. There is no error but then Java is not installed. Please suggest what needs to be done so that Java gets installed and java_home path is set programmatically.

    Any help is appreciated.

    Thanks and regards,
    Karthik

    Reply
  6. please share a code for saerch a file (in which given “string” would be available) by given string and then copy searched file to destination folder.

    Reply
  7. You site is really helpful but I am looking for something else. I have a text file. In this file, I have to read upto certain symbol break that input file into multiple output file ex

    >T0646 OR188, , 93 residues

    MQEERKKLLEKLEKILDEVTDGAPDEARERIEKLAKDVKDELEEGDAKNMIEKFRDEMEQ

    MYKDAPNAVMEQLLEEIEKLLKKAGSLVPRGSY

    >T0647 HR5514A, , 157 residues

    MTERLSAEQIKEYKGVFEMFDEEGNGEVKTGELEWLMSLLGINPTKSELASMAKDVDRDN

    KGFFFNCDGFLALMGVYHEKAQNQESELRAAFRVFDKEGKGYIDWNTLKYVLMNAGEPLN

    EVEAEQMMKEADKDGDRTIDYEEFVAMMTGESFKLIQ

    you can see “>” symbol which I need to start and end for 1 and another file for another.
    How can Ido with java

    Reply
  8. pleas send any project which is don by java programing by my email address

    Reply
  9. Hi Can u please tell me how we can convert a word file to pdf using java

    Reply
  10. I need to display the tiff and pdf image file on web browser using jsp servlet. The specific image selection manual to the jsp page. For example. one.tif or five.pdf etc., please anyone give me the code.

    Reply
  11. Hello,
    I have one doubt about OutputStream in java.
    I want to use single object of fileoutputstream into multiple java files.

    Please help me to use single object of fileoutputstream into multiple java files.

    Reply
  12. Do You Know How To Create A Shortcut File(Windows-.lnk) Using Java??

    Reply
    1. Ternary operator is best alternative for if-else
      For example:

      int age=19;
      String canvote=(age>=18)?”yes,you can vote”:”you cant vote”;
      System.out.println(canvote);
      OUTPUT:

      There are three part of ternary operator:
      (Condition)?truepart:falsepart

      1. Condition- any boolean expression
      2. True part- if condition evalute true so assign this value left hand side.
      3. False part- if condition evalute false so assign this value left hand side.

      Reply
  13. Hi , i have two doubts in exception handling

    1).which scenario we use try/catch block and throws in same program?

    2)suppose catch cann’t handle the exception what we do?

    Please help me this.

    Reply
    1. throws are used that we know the exception can occur but doesn’t catch the exception

      Reply
  14. Your site is absolutely wonderful…..very helpful indeed..thanks a ton!!!

    Reply
  15. Thank you for share all those tutorials. I’m just getting started with Java Netbeans and this is the best that I’ve found . . .

    Reply
  16. thank you sir for giving nice lessons like this..long live mr yong

    Reply
  17. Hi MKYONG sir,
    Could you please upload an example for printing application.
    For example: if we want to print particular Bill in a restaurant!!!

    Thank you,

    Reply
  18. The best tutorial i have ever seen:)
    just awesome!!!

    Reply
  19. am new in programming and i want to learn and understand more programming please help me

    Reply
    1. Try to learn from the basics..read books…use video tutorials…you can…aim for a exam(some kind of sun java or something)will help you to keep on target..and finally you will have a certificate too….try to learn user interfaces(GUI) at final levels..you may not have to code them ..can use..netbeans or eclipse…I recommend eclipse..there is much support for study the language..best of luck

      Reply
  20. 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.

    Reply
    1. create list of your custome object and write the list into a file and the read the list from file using deserailization ,iterate the list and get ack your object.

      Reply
  21. 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

    Reply
  22. How to prevent file path manipulation as it is security issue

    Reply
  23. Hi

    For every thing we have an example but not much explanation.

    Reply
  24. 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.

    Reply
  25. This is an amazing site. lot of examples.

    Can you also post some collection related examples.

    Thanks,
    Gopi.

    Reply
  26. How to print output in different color on console using java program ?

    Reply
  27. 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…..

    Reply
    1.  
      if(test2==null)
      test2=".";
       

      is this what you are looking for ?

      Reply
  28. ThanQ sir,it is very helpful to me and
    can you please provide me the ORACLE & FORMS understandable website…..

    Reply
  29. hi

    i have to encoded chines charactor but its can not decode its. any idea

    Reply
  30. Can I get more examples in regular expressions with I/O operations

    Reply
  31. Hi,

    Really a good site.Just want, your answers or post should be more elaborative.

    Thnks

    Reply
  32. hello Mr.
    i want to do how can get the file size that stored in internet Before download
    pleas email it for me.

    Reply
  33. 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)

    Reply
  34. these are very valuable examples…easy to follow
    thanks sir

    Reply
  35. 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.

    Reply
  36. hi i need to different between spring and hibernt

    Reply
    1. 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.

      Reply
  37. 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,

    Reply
    1. 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

      Reply
  38. Hi. i have interest in java. but i have no knowledge about it. can u pls put me through.Thanks

    Reply
      1. I have basic knowledge of C++, Should I start practicing java basics

        Reply
          1. 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

  39. 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.

    Reply
    1. Yes, you are free to translate it. But, with a credit link back to original post.

      Reply
  40. 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

    Reply
      1. 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.

        Reply

Leave a Comment

Your email address will not be published. Required fields are marked *