Friday, October 31, 2014

File in Java Pro



Compare two file paths

import java.io.*;
public class CompareTwoFilePaths
{
public static void main(String[] args)
{
File file1 = new File("C://FileIO//demo1.txt");
File file2 =  new File("C://FileIO//demo1.txt");
if(file1.compareTo(file2) == 0)
{
System.out.println("Both paths are same!");
}
else
{
System.out.println("Paths are not same!");
     }
  }
}

Construct file path

import java.io.*;
public class ConstructFilePath
{
 public static void main(String[] args)
 {
String filePath = File.separator + "JavaExamples" + File.separator + "IO"; File file = new File(filePath);
System.out.println("File path is : " + file.getPath());
       }
}

Create directory

import java.io.*;
 public class CreateDirectory
{
  public static void main(String[] args)
{
File dir = new File("C://FileIO//DemoDirectory");
boolean isDirectoryCreated = dir.mkdir();
if(isDirectoryCreated)
System.out.println("Directory created successfully");
else
System.out.println("Directory was not created successfully");
    }
}
Create directory along with required nonexistent parent directories

import java.io.*;
public class CreateDirParentDir
 {
public static void main(String[] args)
{
File dir = new File("C://FileIO//Parent1//Parent2//DemoDir");
boolean isDirCreated = dir.mkdirs();
if(isDirCreated)
System.out.println("Directory created along with required nonexistent
parent directories");
else
System.out.println("Failed to create directory");
   }
}

No comments:

Post a Comment