• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Stream » File Streams
Next →
← Prev

File Streams

By Dinesh Thakur

A file can be created using File class.

                          File;

                          f = new File (string filename);

The File class is defined in the package java.io The File class is used to store the path and name of a directory or file. But this class is not useful in retrieving or storing data. The File object can be used to create, rename or delete file or directory it represents. A directory is treated as a file that contains a list of files. Each file (or directory) in Java is an object of the File class. A File object is used to manipulate the information associated with a disk file, such as last modification date and time, directory path and also to navigate through the hierarchies of sub-directories.

  The File class has three constructors that are used to create a file object. They are the following:

  • File(String pathname);                    II pathname could be file or a directory name

  • File(String dirPathname, String filename); II specify directory where the file is created

                                                                         II and file name

  • File(File directory, String filename);         II file object as the directory path

These constructors return the reference to the file object. The File class provides several methods that deal with the files, file system, properties of the file and tests on the files. Some of the important methods that serve these purposes are listed in Table.

                Some methods in the File class and their description.

Name of method

Description

string get Name ()

Returns the name of the file excluding the directory name

string get Path ()

Returns the path of the file.

string get Parent ()

Returns the parent directory of the file

Boolean exists ()

Returns true if the file exists and returns false if the file does not exist.

Boolean can Read ()

Returns true if the file is readable, otherwise returns false

Boolean can Write ()

Returns true if the file is writable, otherwise returns false

Boolean is File ()

Returns true if the object is a file, otherwise returns false.

Boolean is Directory ()

Returns true if the object is a directory, otherwise returns false.

Boolean is Absolute ()

Returns true if the file path is the absolute path, otherwise it returns false, indicating that it is a relative path.

long length ()

Returns the length of the file (in bytes).

long last Modified ()

Returns the date on which the file was last modified

String[] list ()

Returns the list of all files and directories that exist in the current directory

There are other methods also which are useful for handling file operations such as renaming a

the file and deleting a file. The syntax of these two methods is the following:

                           boolean renameTo(File new file name);

                           boolean delete();

The following methods are used to create a directory:

                          boolean mkdir(File newdirectoryname)

                          boolean mkdirs(File newdirectoryname)

The first method creates a directory and returns true if it is successful. If the directory cannot be created, it returns false. In addition to creating a directory, the second method creates all its parent directories. The path of the directly to be created will be passed as a parameter to the mkdirs method. If the program fails to execute the mkdirs() method fully, it may be successful in creating partial directories list from the user specified path. The method mkdirs() returns true if the directory was created with all the necessary parent directories, otherwise it returns false. Directory is the name of an object in the File class with an extra property referred to as ‘list’ which is examined by the List () method. This method returns the string array with all files that exist in the directory. Program illustrates the List () method.

Using the methods List () and is Directory ()

import java.io.*;

class ListOfiles

{

      public static void main(String a[ ])

     {

           String name = arg[0];

           Filefile1= new File(name);

           if ( file1.isDirectory() )

          {

                 string arr[] = file1.list();

                 for (int i = 0; i < arr.length; i++)

               {

                        if(arr[i]. isDirectory())

                             System.out.println(arr[i]+ “is directory”);

                       else

                           System.out.println(arr[i]+”is file”);

                }

          }

     }

}

Program illustrates the is Directory () method, which returns the value true if the in voting file object is a directory and returns the value false if it is a file. This program is run with the command line arguments in which arg [0] specifies the name of the directory. The output of the program consists of a list of all the files and directories in the present directory that is, the directory given as the input.

You’ll also like:

  1. I/O Streams
  2. Filter Streams
  3. Copy Data From One File to Another File in Java Example
  4. What is Streams?
  5. Write A C++ Program To Demonstrate The Use of Input And Output Streams.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW