file handling.

 package File_Handling;

import java.io.*;
public class CreateFile {
// public static void main(String[] args) {
// File f= new File("C:\\Users\\vedva\\OneDrive\\Desktop\\ved.txt");
// try{
// if(f.createNewFile()){
//     System.out.println("file successfully created");
// }else{
//     System.out.println("file already exixts..");
// }
// }catch(IOException e){
//     System.out.println("ioexception handled..");
// }
//}
public static void main(String[] args) throws IOException{
File f= new File("C:\\Users\\vedva\\OneDrive\\Desktop\\ved.txt");
if(f.createNewFile()){
  System.out.println("file successfully created");
}else{
    System.out.println("file already exixts..");
}
}
}

package File_Handling;
import java.io.*;
public class FileInfo {
public static void main(String[] args) {
File f = new File("C:\\Users\\vedva\\OneDrive\\Desktop\\ved.txt");
if(f.exists()){
    System.out.println("File name : "+f.getName());
    System.out.println("File location : "+f.getAbsolutePath());
    System.out.println("File Writable : "+f.canWrite());
    System.out.println("File Readable : "+f.canRead());
    System.out.println("File size : "+f.length());
    System.out.println("file delete : "+f.delete());
}else{
    System.out.println("file not exists");
}
}
}


package File_Handling;
import java.io.*;
public class Filereader {
public static void main(String[] args) {
    try{
    FileReader f = new FileReader("C:\\Users\\vedva\\OneDrive\\Desktop\\ved.txt");
    try{
    int i;
    while((i=f.read())!=-1){
        System.out.print((char)i);
    }
    }finally{
        f.close();
    }
    }
    catch(IOException e){
        System.out.println(e);
    }
}
}

package File_Handling;
import java.io.*;
public class Filewriter {
public static void main(String[] args) {
try{
FileWriter f = new FileWriter("C:\\Users\\vedva\\OneDrive\\Desktop\\ved.txt");
try{
    f.write("java is best programming language");
}
finally{
    f.close();
    System.out.println("successfully data wrote in file");
}
}
catch(IOException e){
    System.out.println(e);
}
}
}


package File_Handling;
import java.io.*;
public class FileRename {
public static void main(String[] args) {
File f1 = new File("C:\\Users\\vedva\\OneDrive\\Desktop\\ved.txt");
File f2 = new File("C:\\Users\\vedva\\OneDrive\\Desktop\\dev.txt");
if(f1.exists()){
System.out.println(f1.renameTo(f2));
}else{
System.out.println("file not exists");
}
}  
}

package File_Handling;
import java.io.*;
public class CopyFileContent {
    public static void main(String[] args) throws IOException {
    FileInputStream f = new FileInputStream("C:\\Users\\vedva\\OneDrive\\Desktop\\dev.txt");
    FileOutputStream r = new FileOutputStream("C:\\Users\\vedva\\OneDrive\\Desktop\\dev_copy.txt");
    int i;
    while((i=f.read())!=-1){
        r.write((char)i);
    }    
    System.out.println("data copied successfully");
}
}

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.