Skip to main content

Java Program to edit Files

import java.awt.*;
import java.io.*;
import java.util.*;
public class EditFile {
    Scanner scan=new Scanner(System.in);
    String filePath;
    byte error=0;
    public void input(){
        try{
            String defaultPath=System.getProperty("user.home")+"\\";
            System.out.printf("File Path: "+defaultPath);
            filePath=scan.nextLine();
            filePath=defaultPath.concat(filePath);
        }
        catch(InputMismatchException e){
        System.err.println("Error Occured!\n"+e.getMessage());
        System.exit(0);
        }
    }
    public void compute(){
         Process pro;
        try{                    
            File file=new File(filePath);
            Desktop desktop=Desktop.getDesktop();
            if(desktop.isDesktopSupported())
                desktop.edit(file);
                   }
            catch(IOException e){
            error++;
            try{
            pro=Runtime.getRuntime().exec("write.exe "+filePath);
            }
            catch(IOException er){
                System.err.println("Error Occured!\n"+er.getMessage());
                System.exit(0);
            }
            if(error>1){
            System.err.println("Error Occured!\n"+e.getMessage());
            System.exit(0);
            }
        }
    }
    public static void main(String[] args){
        EditFile edit=new EditFile();
        edit.input();
        edit.compute();
    }
}

Comments