Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners File Handling in Java

Have Programming Question? Ask it here!
 
 
 


Java Coder
File Handling in Java
4 Answer(s)      10 months ago
Posted in : Java Beginners


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


Advertisement
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



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
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  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
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 - Java Beginners
manner. now write a progam in java and create another file which is having the amount required to purchase each item and at last read the newly created file...file handling  assume two files are there- 1. with the quanity of he
File Handling - Java Beginners
File Handling  Q. Write a java prg which accepts a list of existing text files from command line args & concatinate contents of all files...(e.getMessage()); } } }   its prg of copying file Check code
File Handling - Java Beginners
File Handling  Thanks for replying me.. but i am sorry to say...; System.out.print("Enter the file name : "); name = br.readLine(); name = "d...{ File f; f= new File(name); if(f.exists()){ RandomAccessFile rand
File Handling - Java Beginners
File Handling  I have written one program by which we can create a file , store any information in that,and resume that file using a user name and password.now my problem is that whenever i write datas into the file the previous
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
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
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 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
unicode handling in java
unicode handling in java  reading a unicode text file in java
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 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
exception handling code for file not found error..
exception handling code for file not found error..  How to do exception handling for file not found error
Java exception handling
Java exception handling  what are the constraints imposed by overriding on exception handling
PHP File Handling
PHP File Handling: Every language supports File handling, file handling has... file handling. PHP supports this feature using the following functions: i)    fopen: To open a file in a particular mode, like r: read, w
Java event handling
Java event handling  Which java.util classes and interfaces support event handling
Exception Handling - Java Beginners
handling module i.e. the message which we want to be printed. For instance, throw... keyword. The Java compiler knows about the exceptions thrown by some methods so...,visit the following link: http://www.roseindia.net/java/exceptions/making
Exception Handling in java
Exception Handling in java  what is advantage to catch smaller exception first and then normal exception. I mean we normally catch SQLException first and then Exception.   Hi, The exceptions should be from
Java exception handling
Java exception handling  What is the difference between throw and throws keywords
Java exception handling
Java exception handling  What is the catch or declare rule for method declarations
Java event handling
Java event handling  What is the purpose of the enableEvents() method
Java event handling
Java event handling  What event results from the clicking of a button
Java exception handling
Java exception handling  What classes of exceptions may be caught by a catch clause
Java exception handling
Java exception handling  How does a try statement determine which catch clause should be used to handle an exception
Java exception handling
Java exception handling  Does the order of placing catch statements matter in the catch block
Java exception handling
Java exception handling  How does a try statement determine which catch clause should be used to handle an exception
Java exception handling
Java exception handling  What happens if a try-catch-finally statement does not have a catch clause to handle an exception that is thrown within the body of the try statement
Error handling - Java Beginners
Error handling  Hello, Help me write code as instructed bellow for a Java program. 1. Create a new Movie class which has two properties, title...://www.roseindia.net/java
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 Exception handling - Java Beginners
java Exception handling  what is the difference between throw and throws keywords
java Exception handling - Java Beginners
java Exception handling  what is the difference between throw and throws keywords  Hi Friend, The keyword throw is used to throw..., visit the following link: http://www.roseindia.net/java/exceptions/how
java exception handling - Java Beginners
java exception handling  hi while i am compailing the program given below i am getting an error please help me as soon as possible import java.io.*; class insufbal extends Exception { public insufbal(String str) { super
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
JComboBox Event Handling - Java Beginners
JComboBox Event Handling  Hi, I have problem with event handling here.There are Two JComboBox and a JTextField.When I select an item in first combo(Select,First,Second in the below program) the items in the second combo change
java exception handling - Java Interview Questions
java exception handling   hi to all , this afsar i have one doubt.please clarify it. while handling exception write try catch and finally blocks. the scenario is P v S main(---) throws exception { try
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
How to make a design document for exception handling in java
How to make a design document for exception handling in java  How to make a design document for exception handling
Error handling - Java Server Faces Questions
Error handling  How to handle error handling in JavaScript? Say we have this index.jsp file and we like to add some JavaScript into it based on these rules: 1. We like check a name has been entered or not 2. We like to check
Using throw keyword in exception handling in Core Java
Description: Core Java Throw Function is used for throwing the exception... exception need to be thrown from the calling method. Code for Java Throw Exception Handling: package exceptionh; import java.io.*; class ExcepHandling1
Advertisements
 

 

 

DMCA.com