File Handling in Java

Ads
 

File Handling in Java

Hi,

While opening a file in Java developers are using following type of code:

File myFile=new File("myfile.txt");

I was trying to find some tutorials about handling file in Java on net. I have also seen many examples where developers were using the following classes:

  • FileInputStream

  • DataInputStream

  • BufferedReader

I am trying to find the good information about these classes but unable to understand the correct use of these classes. I don't know when and how to use these classes.

Can anyone explain me what are the use of these classes?

Is there example code for explaining the File Handling process in Java?

Thanks

View Answers

August 30, 2013 at 9:09 PM

Hi,

Java programming language comes with many Interfaces and Classes packaged in the java.io package for easy handling of files. Classes and Interfaces of java.io package can be used to read any type of file.

Java also cares about the performance of the application. It provides BufferedReader class for reading the text file/stream using the buffer. So, The BufferedReader class is used when there is requirement of reading the stream using the buffer. The BufferedReader class increases the performance of the application.

FileInputStream class is used to FileInputStream class is used to read the data from byte steam. It is used to read the data into bytes from the Stream.

The DataInputStream class is used to read primitive Java data types from the input stream.

Read the tutorial Java Read File Line by Line for detailed description and example code.

Hope above explanation will help you understanding the concepts.

Thanks


August 30, 2013 at 9:14 PM

Hi,

Let me explain you about the File class. The File class is used to get the handle of the file. It does not any method to read the data from file itself.

We have to use the other classes to read the file data. The File class is used to just read the information metadata about the file like size, created date etc.

See the example of Reading data from file.

Thanks


August 30, 2013 at 9:39 PM

Hi,

Here is the example code of reading file one line at a time and then printing the data on console.

import java.io.*;

public class  ReadFileExample
{
    public static void main(String[] args) 
    {
        System.out.println("This is an example of reading file one line at a time");

        //Create new file.
         File file = new File("filetoread.txt");

         //Declare the
         BufferedReader bufferedReader = null;

         try {
          bufferedReader = new BufferedReader(new FileReader(file));
          String line;

          //Read one line and then print
          while ((line = bufferedReader.readLine()) != null) {
           System.out.println(line);
          }
          /* Exception handling */
         } catch (IOException e) {
          e.printStackTrace();
         } catch (Exception e) {
          e.printStackTrace();
         } finally {
          try {
           //Finally try to close the reader
           bufferedReader.close();
          } catch (Exception ex) {
           ex.printStackTrace();
          }
         }

    }
}

Thanks


August 30, 2013 at 9:44 PM

Hi,

Here is very simple example of reading file line by line:

import java.io.*;

public class  ReadFileExample
{
    public static void main(String[] args) 
    {
        System.out.println("This is an example of reading file one line at a time");
        try
        {
            BufferedReader br = new BufferedReader(new FileReader("filetoread.text"));
            String lineData;
            while( (lineData = br.readLine()) != null )
            {
                  System.out.println(lineData);
            }
            br.close();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

You can add the Exception handling logic yourself in your program.

View more tutorials at Learn how to handle files in Java with Examples and Tutorials.

Thanks

Ads









Related Tutorials/Questions & Answers:
File handling in Java
handling in Java? Thanks   Hi, File handling is very important... handling features in their api. You can read more at File Handling in Java. Examples of file handling in Java: Java I/0 Examples Working With Files
File Handling in Java
File Handling in Java  Hi, While opening a file in Java developers... was trying to find some tutorials about handling file in Java on net. I have also seen... are the use of these classes? Is there example code for explaining the File Handling
Advertisements
java file handling
java file handling  enter string from keyboard and then read a file if this string is present in file then print message already exists else write this string to file
Excel file Handling in Java
Excel file Handling in Java  Hello Sir, I am new to Java, I have Started Java Core by myself. I want to make a project which include 3-4 Excel file...://www.roseindia.net/tutorial/java/poi/readExcelFile.html   Now to create an excel file
java file handling
java file handling  program to get values from textbox array and the write them to text files
java file handling
java file handling  Hi friends, My situation is i have file named "sample.conf" which contains more IP Adressess.Eg: the file contains "IP=255.255.255.255" at some lines in the file. what i need to do is. i need to search
File Handling In Java
File Handling in Java In this section we will discuss about the file handling in Java. This section describes you about how to perform file handling in Java. In this section you will read what is file handling in Java, how to perform
File Handling Classes in Java
In this section, you will get to know about file handling classes in java to handle the file input output operations
Java File Handling Test Results
Java File Handling Test Results   Is somebody able to help me with this Write a program to read each name from the file and prompt the user... FileNotFoundException { new File("team.html").delete(); Scanner scan = new
File Handling
File Handling  how we do file handling of parsed XML code bu DOM in java
Java File Handling
File Handling in Java       File Handling in Java For file handling.... The File Class In spite of classes available to support file I/O, Java has a class
File handling
File handling  how do i search for an occurance of a word in a file and replace with the user input in java?   Java Replace a word in a file import java.io.*; import java.util.*; class FileOperations{ public static
unicode handling in java
unicode handling in java  reading a unicode text file in java
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT  Prompt the user to enter the name of a text file to search for. If the name does not end in a .txt extension, display an error message. Search for this file in the current directory. If the file does
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT  Write a program to read and write the details of an employee like year of joining, Department code, salary using some byte stream classes
File handling in Java, Java Files
Java File Handling Tutorials In this tutorial we are providing many different examples of handling file from Java program. The Java File Handling Tutorials will help you learning the file handling concepts in Java. The Java
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT   Write a program that reads input from the console in the form of a string. When the user enters a word called end, the program should stop reading from the console and print out the values that have been
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT   Write a program that reads input from the console in the form of a string. When the user enters a word called end, the program should stop reading from the console and print out the values that have been
File Handling In Java
File Handling In Java In this section we will discussed about How files can be handled in Java. In this tutorial we will learn the various aspect of file handling in Java. Most of the software's important feature is how to read data
Java null pointer exception handling
Java null pointer exception handling   What... pointer exception handling What is Java NullPointerException? In Java...; java.lang.NullPointerException Handling NullPointerException in Java
Java : Exception Handling
Java : Exception Handling In this section we will discuss about  JAVA Exception Handling with simple example. Exception Handling : An exception... such file which doesn't exist. By default it is handled by java runtime
Java write to file
Handling examples in Java Java Input/Output Examples How to read file in Java...Java write to file  How to write to a file in Java? Is there any good example code here? Thanks   Hi, Java is one of the best
What are events? Explain how Event handling in Java?
What are events? Explain how Event handling in Java?  What are events? Explain how Event handling in Java?   Hi, The Events..., and introduces us to AWT event handling. For Details about Event Handling in Java
Exception handling in Java Programming Examples
Exception handling in Java Programming with many Examples In this section we will explain you about the exception handling in Java and teach you how.... In this section we are presenting many examples of exception handling in Java
Exception handling in Java Programming Examples
Exception handling in Java Programming with many Examples In this section we will explain you about the exception handling in Java and teach you how.... In this section we are presenting many examples of exception handling in Java
Event Handling In Java
Event Handling In Java In this section you will learn about how to handle events in Java. Events in any programming language specifies the external effects.... In Java event handling may comprised the following four classes : Event Sources
String file in to word file in java
String file in to word file in java  How to convert a String format notepad file into Ms word file in java
calling a java file in java
calling a java file in java  how to call a another java file in java
String file in to word file in java
String file in to word file in java  how to convert a String format word file into Ms Word file using java? please can anyone of you help me
Java File Programming
Java File Programming articles and example code In this section we will teach you about file programming in your Java applications. Java File Programming tutorial will teach you how to create, read and write in a file from Java
Exception handling in java
Exception handling in java We are going to discus about Exception handling in java. Java program many provides exception. We are handle of error in program... is displayed when java run-time System fails to find the specified class

Ads