Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Creating objects and methods list of File class in Java

Introduction :-

Hello friends, In this hub, I will be creating and demonstrating the working of File objects in Java. Before we go further, lets go in the basics of importance of files in computers. Generally, data stored in variables and objects are not permanent. They are not persistent. They are normally temporary data which remains in memory as long as the scope of variable. Once a local variable gets out of scope the data is lost. So to keep the data for long time use generally computer uses files. These files are been stored in secondary storage devices such as hard disk, flash drives etc. The data stored in them is permanent.In terms of programming language such as Java, terms like Files and Streams have their own meaning. Java actually sees a file as a stream of bytes which is sequential.
Java provides us a package called as java.io package. This package consist of class File which helps in retrieving information about various files and various directories in disk. Generally they don't provide capabilities such as opening or closing a file and basic processing of a file or directory. However File objects are frequently used with other objects in java.io package to do the above processing of the files and directories.

Creating File Objects :-

File Objects are created with new operator, which calls constructor with four different arguments. One of the constructor requires string passed to it as argument. This string provides the constructor name of the file or directory. The name usually is the path given to constructor where File object can locate the file or directory. The path information provided into the constructor can be either relative or absolute path. The relative path deals with current directory from where the application has began. The absolute path starts from a root directory and proceed to name of the file or directory specified. Another constructor has two arguments passed to it, first argument contains the relative or the absolute path to file directory and the second argument contains the name of the file or directory , which we want to associate to File object. Another constructor with File and String argument is used. Here we use existing File object which points to file or directory specified by the String argument. The last constructor uses the URI as an argument to point to the file or directory. Here by URI we mean Uniform Resource Identifier which are like Uniform Resource Locators. They use URL like pattern to locate the files, e.g. file://C:/java-programs.txt.

Constructors of File :-
File (String path);
File (String path, String name);
File (File obj, String name);
File (URI);

Important methods of File class
  1. boolean isFile() :- This method return either true or false. It return true if the File object created by passing a String object to it, is an existing file.
  2. boolean isDirectory() :- This method return either true or false. It return true if the File object created by passing a String object to it, is an existing directory.
  3. boolean exists() :- This method returns true if the file / directory represented by the object exists.
  4. boolean canWrite() :- It returns true if the current application can write the file.
  5. boolean canRead() :- It returns true if the current application can read the file.
  6. boolean isAbsolute() :- It returns true if the path specified as String in the File object constructor is absolute.
  7. String[ ] list() :- If you want directory's content in the form of array of string this method is used. It returns null if the file specified in the argument is not directory.
  8. long lastModified() :-It provides us with the time, that when a file was last modified.
  9. long length() :- It returns the length of the file in bytes. If its a directory, an unusual value is returned.
  10. String getPath() :- It basically returns the path of the file in use.
  11. String getParent() :- It basically returns the name of parent directory holding the file .
  12. String getName() :- It return name of the file used at the time of creating File object
  13. String getAbsolutePath() :- It basically returns the absolute path of file or directory.
  14. String getCanonicalPath() :- Returns the path of the file object but separators in the path name are system-dependent separators such as / or \.
  15. Boolean isHidden() :- Returns true if the file is hidden and false otherwise.
  16. Boolean renameTo(File name) :- Renames the file (or directory) represented by this File instance to the new name specified by Name.
  17. boolean delete() :- Deletes the file or directory associated with the File object.
  18. boolean createNewFile() :- Creates a new file with the specified name if it does not exists.
  19. boolean mkdir() :- Creates a directory by using the abstract path name associated with this File instance.
  20. boolean mkdirs() :- Creates a directory by using the abstract path name associated with this File instance. Also creates any non-existent parent directories appearing in the given path.
  21. File [ ] listFiles() :- Returns an array that contains the names of files only contained in the directory represented by this File instance. 


    How to sort array elements using Bubble Sort algorithm in Java ?


    How Bubble Sort is implemented in Java ?. 
    Bubble Sort is a special sorting technique, which sorts elements by making multiple passes. While making each pass neighboring elements are compared in pairs. Generally the order is important, if the comparison comes out in decreasing order the numbers are swapped. If the order comes out in ascending order the values remain unchanged. It is called as Bubble Sort technique because the large value in each pass sinks to bottom while the smaller value bubbles out to top. After first successful pass the largest elements becomes the last element and after second pass, second largest elements becomes the second last element. Bubble Sort is implemented in Java by below small program :    

    package com.hubberspot.sort.example;
    
    public class BubbleSort {
      
      public static void main(String[] args) {
        int[] numbers = {3, 4, 5, 6, 7, 2, -3, 4, 16, 14};
        sort(numbers);
        for (int i = 0; i < numbers.length; i++)
          System.out.print(numbers[i] + " ");
      }  
      
      public static void sort(int[] list) {
        boolean nextPass = true;
        
        for (int k = 1; k < list.length && nextPass; k++) {
          nextPass = false;
          for (int i = 0; i < list.length - k; i++) {
            if (list[i] > list[i + 1]) {
          
              int temp = list[i];
              list[i] = list[i + 1];
              list[i + 1] = temp;
              
              nextPass = true; 
            }
          }
        }
      }  
    } 



    Video tutorial to demonstrate how to sort an array using Bubble Sort algorithm in Java.








    Java Tools and Utilities for Java Programming Language

    Java Tools and Editors
    The Java SDK (Software Development Kit) comes with various utilities which helps programmer to work with Java platform efficiently. Java SDK comes with set of such powerful tools. Let us look into some of widely used tools or utilities one by one :

    1.    javac – the java compiler

    The javac tool is most widely used tool in Java. It provides compilation power to Java. It basically reads program containing classes and interfaces, compiles them to Java bytecodes. These bytecodes are java .class files. Let us look into the syntax of this command-line tool :

    Syntax:
    javac [options] [source_files] [@list_files] 

    where,
    options: Command-line options.
    source_files: Source files to be compiled.
    @list_files: A file containing a list of source file to be compiled.

    Let us look into few examples:

    1.    javac TestClass.java
    This command  compiles TestClass.java into bytecodes named as TestClass.class .

    2.    javac @list_files
    This command compiles all java source files listed in the file called as list_files.
         
     2.   java – the application launcher
    The java tool launches a Java application. It starts Java Runtime Environment, loads the specified .class file and invokes main method in the class.

    Syntax:
    java [options] [class_name] [argument list]
    java [options] –jar JarName.jar [argument list]

    After the option this tool contains name of the class file to be execute, which has the main method. If we use –jar option than we need to specify jar name containing all the classes and resource files. The tool searches main method class in the jar by reading manifest file.
     Let us look into few examples:

    1. java TestClass
    This command executes TestClass.class file by calling main method in it.

    2. java –jar JarName.jar
    This command launch main method of file listed in jars manifest file.

    3. jdb – the java debugger
    The Java Debugger tool jdb is used to help developer to find and fix bugs in java programs.

    Syntax:
    jdb [options] [class_name] [arguments]

    Let us look into few examples:

    1. jdb TestClass
    This command executes TestClass.class and help programmer to debug the code. 

    4. javap – Disassembling classes
    The javap tool allows you to query any class and find out list of methods and constants or find out commands executed by the underlying JVM.

    Syntax:
    javap [options] [class_name]

     Let us look into few examples:

     1. javap java.lang.String
    This command list all the methods and class members of String class. 

    5. javah – Header file generator
    This tool connects Java programs to C and C++ programs. It is used when Java wants to use legacy libraries or Java can’t do something because of platform needs. javah generates C header and source files that are needed to implement native methods. The generated header and source files are used by C programs to reference an object’s instance variables from native source code.

    6. jar – the java archive manager
    The jar tool is a java application that combines multiple files into a single JAR archive file. jar is general purpose archiving and compression tool, based on ZIP compression format. jar allows packaging of all files related to java applets or applications into single archive.

    Syntax:
    jar [options] [manifest] [input-files]

    Let us look into few examples:
    jar –cf JarName TestClass1.class TestClass2.class


     
    © 2021 Learn Java by Examples Template by Hubberspot