Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help: I need a program Srm.java which will delete all files written in th

ID: 3589070 • Letter: I

Question

I need help:

I need a program Srm.java which will delete all files written in the command line if it possible with option ‘i’ or without option i. We have to use absolute path for files.

But before delete we have to compress file and copy them to the .recyclebin.jar in order to restore them if necessary. After we can delete file.

The Srm program create the .RecycleBin.jar (it should be invisible jarfile) automatically. This easily can be done by checking if the jar file does not exist, then create it.

After delete we have to check by using program that file deleted and will produce output on the screen (like ls –la).

For example:

java Srm file1 file2

Will delete file1 and file2 if they exist, writable….

Java Srm file1

Will delete file1

Java Srm – i file 1 file 2

Will ask user ‘do they want to delete file1’ if “yes” delete and go to the second file and ask again. If no, skip this file and go to the second file etc.

Explanation / Answer

Hi Please check the below code once and run in your local. Thank you.

Srm.java

package com.files;

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.List;

import java.util.Scanner;

import java.util.jar.Attributes;

import java.util.jar.JarEntry;

import java.util.jar.JarOutputStream;

import java.util.jar.Manifest;

public class Srm {

/**

* @param args

* @throws IOException

*/

public void writeFile(File f,String targetLocation) throws IOException{

InputStream in = new FileInputStream(f);

OutputStream out = new FileOutputStream(targetLocation);

// Copy the bits from instream to outstream

byte[] buf = new byte[1024];

int len;

while ((len = in.read(buf)) > 0) {

out.write(buf, 0, len);

}

in.close();

out.close();

}

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

// TODO Auto-generated method stub

Srm srm = new Srm();

File[] files=new File[args.length];

Scanner sc = new Scanner(System.in);

if(args[0].equalsIgnoreCase("-i")){

System.out.println("Ask,copy and delete");

//Syout

for(int i=1;i<args.length;i++) {

String argument=args[i];

System.out.println(argument);

File f = new File(argument);

if( f.exists()){

if(f.isFile() && f.canWrite()){

String fname = f.getName();

System.out.println("do they want to delete "+fname);

String answer = sc.nextLine();

System.out.println(answer);

if(answer.equalsIgnoreCase("yes")){

srm.writeFile(f,"D:/app/output/"+fname);

f.delete();

System.out.println("copied");

}else{

System.out.println("Skipped::"+fname);

}

}

}

}

}else{

System.out.println("copy and delete");

for(int i=0;i<args.length;i++) {

String argument=args[i];

System.out.println(argument);

File f = new File(argument);

if( f.exists()){

if(f.isFile() && f.canWrite()){

String fname = f.getName();

System.out.println("Copy and delete");

srm.writeFile(f,"D:/app/output/"+fname);

f.delete();

}

}

}

}

System.out.println("Deleted Successfully");

//Creating jar file

//prepare Manifest file

String version = "1.0.0";

String author = "Cheggg";

Manifest manifest = new Manifest();

Attributes global = manifest.getMainAttributes();

global.put(Attributes.Name.MANIFEST_VERSION, version);

global.put(new Attributes.Name("Created-By"), author);

//create required jar name

String jarFileName ="D:/app/recyclebin.jar";

JarOutputStream jos = null;

try {

File jarFile = new File(jarFileName);

OutputStream os = new FileOutputStream(jarFile);

jos = new JarOutputStream(os, manifest);

} catch (IOException e) {

e.printStackTrace();

}

//Collect all file and class names to iterate

List<String> fileList = new ArrayList<String>();

String rootLocation ="D:/app/output/";

File folder = new File(rootLocation);

File[] listOfFiles = folder.listFiles();

for(File f1:listOfFiles){

System.out.println("File Name::"+f1.getName());

fileList.add(f1.getName());

}

//start writing in jar

int len =0;

byte[] buffer = new byte[1024];

for(String file : fileList ){

//create JarEntry

JarEntry je = new JarEntry(file);

je.setComment("Craeting Jar");

je.setTime(Calendar.getInstance().getTimeInMillis());

System.out.println(je);

jos.putNextEntry(je);

//write the bytes of file into jar

InputStream is = new BufferedInputStream(new FileInputStream(rootLocation+file));

while((len = is.read(buffer, 0, buffer.length)) != -1) {

jos.write(buffer, 0, len);

}

is.close();

jos.closeEntry();

}

jos.close();

System.out.println("Done");

}

}