Thursday, 14 September 2017

Java - Files

Create and new file and write content to file


package fileExamples;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class CreateFile {

public static void main(String args[])
{
String path="W:/Java/test.txt";
String content ="Hello world";
File file1 =new File(path);

if(!file1.exists()){
try {
file1.createNewFile();
System.out.println(file1.getAbsolutePath()); //W:\Java\test.txt
FileWriter fw =new FileWriter(file1.getAbsolutePath());
BufferedWriter bw=new BufferedWriter(fw);
bw.write(content);
bw.close();

} catch (IOException e) {
System.out.println(e);
}
}

}

}
---------------------------------------------
import java.io.File;
import java.io.IOException;


public class CreateWrite {

public static void main(String args[]) throws IOException{
String path="W:/Java/new.txt";

File file =new File(path);
if(file.createNewFile()){
System.out.println("success");
}
else System.out.println("fail");
}

}


--------------------------------------------------------------------------------------------------------


package fileExamples;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class CreateFile1 {

public static void main (String args[]) throws IOException{
String path ="W:/Java/helloWorld.txt";
String content = "Hello World";
BufferedWriter output=null;
try{
File file =new File(path);
output=new BufferedWriter(new FileWriter(file));
output.write(content);
}catch(IOException e){
System.out.println(e);
}
finally{
if(output!=null){
output.close();
}
}
}
}

--------------------------------------------------------------------------------

How to compare paths of two files


package fileExamples;

import java.io.File;

public class CompareFiles {

public static void main(String args[]){
File file1 = new File("W:/Java/a/test.txt");
File file2 = new File ("W:/Java/b/test.txt");

if(file1.compareTo(file2)==0)
System.out.println("both files same");
else
System.out.println("not same");
}
}


--------------------------------------------------------------------------

How to get last modification date of a file


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;


public class LastModified {

public static void main(String args[]){

String path="W:/Java/helloWorld.txt";
File file =new File(path);
//System.out.println(file);

/*
Long lastModified= file.lastModified();
System.out.println(lastModified);
Date date =new Date(lastModified);
System.out.println(date);
*/

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
System.out.println(sdf.format(file.lastModified()));
}
}


------------------------------------------------------------------

import java.io.File;
import java.io.IOException;


public class FilePerm {

public static void main(String args[]) throws IOException{
File file =new File("W:/Java/perm.txt");
System.out.println("Read perm: "+file.canRead());     //false
System.out.println("Write perm : "+file.canWrite());  //false
System.out.println("Exec perm : "+file.canExecute()); //false
file.createNewFile();
System.out.println("Read perm: "+file.canRead()); //true
System.out.println("Write perm : "+file.canWrite()); //true
System.out.println("Exec perm : "+file.canExecute()); //true
// set permissions 
file.setReadOnly();
file.setWritable(false);
file.setExecutable(false);
System.out.println("Read perm: "+file.canRead());     //true
System.out.println("Write perm : "+file.canWrite());  //false
System.out.println("Exec perm : "+file.canExecute()); //false
}
}

-------------------------------------------------------------------------------------------
Create a new directory and a file within in


       File f1 =new File("W:/Java/c/apple.txt");
System.out.println(f1.getParentFile());  //W:\Java\b
f1.getParentFile().mkdir();
   f1.createNewFile();


----------------------------------------------------------------------------------

 File Permissions 


import java.io.File;
import java.io.IOException;


public class FilePerm {

public static void main(String args[]) throws IOException{
File file =new File("W:/Java/perm.txt");
System.out.println("Read perm: "+file.canRead());     //false
System.out.println("Write perm : "+file.canWrite());  //false
System.out.println("Exec perm : "+file.canExecute()); //false

file.createNewFile();

System.out.println("Read perm: "+file.canRead()); //true
System.out.println("Write perm : "+file.canWrite()); //true
System.out.println("Exec perm : "+file.canExecute()); //true

// set permissions

file.setReadOnly();
file.setWritable(false);
file.setExecutable(false);

System.out.println("Read perm: "+file.canRead());     //true
System.out.println("Write perm : "+file.canWrite());  //false
System.out.println("Exec perm : "+file.canExecute()); //false



}
}

--------------------------------------------------------------------------------------

Rename File

import java.io.File;


public class Rename {

public static void main(String args[]){

File oldFile =new File("W:/Java/test.txt");
File newFile = new File("W:/Java/test1.txt");
   if(oldFile.renameTo(newFile))
    System.out.println("rename success");
   else System.out.println("failed to rename");
 
}
}

--------------------------------------------------------------------------------------

File Size

import java.io.*;

public class FileSize {

public static long getFileSize(String fileName){

File file =new File(fileName);

if(!file.exists()| !file.isFile()){
System.out.println("File doesn't exist");
}
return file.length();
}

public static void main(String args[]){

long size= getFileSize("W:/Java/test1.txt");
     System.out.println("Size in bytes: "+size);
}


}

----------------------------------------------------------------------------------

Read File

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class ReadFile {

public static void main(String args[]) throws IOException{
BufferedReader br =new BufferedReader(new FileReader("W:/Java/helloWorld.txt"));

String str;

while((str=br.readLine())!=null){
System.out.println(str);
}
}
}


----------------------------------------------------------------------------------
Append the text into file


import java.io.*;
public class Append {

public static void main(String args[]) throws IOException{

// Creates a file if it is not there
// If file is there then it will append
       BufferedWriter br =new BufferedWriter(new FileWriter("W:/Java/helloWorld.txt",true));
br.write("\nThis is the end of the file");
br.close();
}

}

--------------------------------------------------------------------------------------

File path

import java.io.*;

public class Append1 {
 
public static void main(String args[]) throws IOException{
 
String data="Welcome to Java programming";
File f=new File("W:/Java/append.txt");
if(!f.exists()){
f.createNewFile();
}
System.out.println(f.getAbsolutePath());   //string
System.out.println(f.getCanonicalPath());  // string
System.out.println(f.getPath());           // string
System.out.println(f.getAbsoluteFile());  // retuns File
System.out.println(f.getName());          // string
System.out.println(f.getParent());        // string
System.out.println(f.getParentFile());    // file
System.out.println(f.getClass());         // class
/*
W:\Java\append.txt
    W:\Java\append.txt
    W:\Java\append.txt
    W:\Java\append.txt
    
    append.txt
    
    W:\Java
    W:\Java
    
    class java.io.File

*/
if(f.getAbsolutePath() instanceof String)
System.out.println("string");
if(f.getCanonicalPath() instanceof String)
System.out.println("string");
if(f.getPath() instanceof String)
System.out.println("string");

}
}

 ---------------------------------------------------------------------


No comments:

Post a Comment