Free Data Structures and Algorithms Course









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










OR



Subscribe to all free courses

How to read and write a file in Java 7 using java.nio API ?


Program to demonstrate how to read and write a file in Java 7 using java.nio API.

package com.hubberspot.java7;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class ReadAndWriteFiles {

 public static void main(String[] args) {

  // Create a path to the file which is
  // to be written and read later
  // here "D:\\books\\java\\" is the address 
  // to reach the file created by name "hubberspot.txt" 
  Path filePath = Paths.get("D:\\books\\java\\hubberspot.txt");

  try { 

   // First we check whether file already exists
   // at the filePath by using Files class static
   // method by name exists()
   if (!Files.exists(filePath)) {

    try {

     // Second we create a file at 
     // the specified filePath by using
     // Files class createFile() method.
     Files.createFile(filePath);

    } catch (IOException e) {

     e.printStackTrace();
    }
   }


   // Create a BufferedWriter instance by calling 
   // Files class newBufferedWriter() method which
   // returns back BufferedWriter object
   // This method takes in three parameters such as:
   // 1. Path to the file created above
   // 2. Charsets for the format of the file
   // 3. Open options which defines the standard open options
   BufferedWriter writer = Files.newBufferedWriter(filePath, 
     StandardCharsets.UTF_16, StandardOpenOption.WRITE);

   // Here we use write() method to write some content
   // to the file at the path specified above
   writer.write("Hello World !!! \n");
   writer.write("Welcome to Hubberspot.com.\n");
   writer.write("Learn Java By Examples !!!!... \n");

   // close the writer 
   writer.close();

  } catch(Exception e) { 

   e.printStackTrace();

  }

  try { 

   // Create a BufferedReader instance by calling 
   // Files class newBufferedReader() method which
   // returns back BufferedReader object
   // This method takes in two parameters such as:
   // 1. Path to the file created above
   // 2. Charsets for the format of the file
   BufferedReader reader = Files.newBufferedReader(filePath,
     StandardCharsets.UTF_16);

   String line;

   // To read contents of file line by line 
   // we use BufferedReader readLine()
   // method and prints contents to console
   while((line = reader.readLine()) != null) {

    System.out.println(line);

   }

  }
  catch(Exception e) { 

   e.printStackTrace();

  }

 }
}


Output of the program :




How to Copy and Move a file in Java 7 using java.nio ?.


Program to implement how to Copy and Move a file in Java 7 using java.nio

package com.hubberspot.java7;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardCopyOption.*;

public class CopyMoveFile {

 public static void main(String[] args) {

  // Let say we have a file placed at the directory
  // D:\books\java by the name CopyAndMove.txt and 
  // we want to copy or move it to a different directory
  // by name D:\books\CopyAndMove. 
  // In order to do that we create two Path instances 
  // in which one path represent the source and other
  // represents the target
  Path source = Paths.get("D:\\books\\java\\CopyAndMove.txt");
  Path target = Paths.get("D:\\books\\CopyAndMove\\CopyAndMove.txt");

  // In order to take a backup we usually use copy()
  // method of Files class by passing three parameters
  // such as source from where it picks the file
  // the target where it tranfers the file and 
  // lastly the CopyOption which tells that 
  // whether to replace existing file if it exists 
  // by the name provided already.
  try {

   Files.copy(source, target, REPLACE_EXISTING);

  } catch (IOException e) {

   e.printStackTrace();
  }

  // In order to move a file completely we usually 
  // use move() method of Files class by passing 
  // three parameters such as source from 
  // where it picks the file the target where 
  // it tranfers the file and lastly the 
  // CopyOption which tells that whether 
  // to replace existing file if it exists 
  // by the name provided already.

  try {

   Files.move(source, target, REPLACE_EXISTING);

  } catch (IOException e) {

   e.printStackTrace();
  }

 }

}



How to create and delete a file in Java 7 using java.nio ?

Program to implement how to create and delete a file in Java 7 using java.nio ?.

package com.hubberspot.java7;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateAndDeleteFile {

 public static void main(String[] args) {

  // Create a path to the file which is
  // to be created and deleted later
  // here "D:\\books\\java\\" is the address 
  // to reach the file created by name "hubberspot.java" 
  Path filePath = Paths.get("D:\\books\\java\\hubberspot.java");

  // First we check whether file already exists
  // at the filePath by using Files class static
  // method by name exists()
  if (!Files.exists(filePath)) {

   try {

    // Second we create a file at 
    // the specified filePath by using
    // Files class createFile() method.
    Files.createFile(filePath);

   } catch (IOException e) {

    e.printStackTrace();
   }
  }

  // In order to delete there must be a file,
  // here we check whether file already exists
  // at the filePath by using Files class static
  // method by name exists()
  if (Files.exists(filePath)) {

   try {

    // here we delete a file at 
    // the specified filePath by using
    // Files class delete() method.
    Files.delete(filePath);

   } catch (IOException e) {

    e.printStackTrace();
   }
  }
 }
}



 
© 2021 Learn Java by Examples Template by Hubberspot