I'm trying to do an assignment to implement a unix shell and run commands using java. commands include read file, copy file, remove file and find substring in a file. I have managed to create the file and try out all commands individually but I've failed to run the threads. Below is a suggestion and code for individual commands:

Suggestion
The purpose of this assignment is to introduce you to Java programming. You are to implement a simple shell (command interpreter) that behaves similarly to the UNIX shell. When you type in a command (in response to its prompt), it will create a thread that will execute the command you entered.
Multiple commands can be chained together on a single line, The commands can either be separated by `&' (ampersand) or `;'.

The public static void main() procedure in your primary class will be quite simple. It will be an infinite loop that prints a prompt, reads a line (in other courses, a program with an infinite loop is considered a bad thing, but in Operating systems, it's the norm!), parses it (breaks it up into its constituent commands), either starts a new thread to handle each of the different commands if they are chained by `&', or starts the threads one by one if they are chained by ';', and then waits for all the threads to finish before printing the next prompt.


code:

//To create and write text to a file:
import java.lang.*;
import java.io.*;
 
public class Create
{
    String command="";
    BufferedReader br;
    BufferedWriter out;
    File file = new File("f1");
 
    public void createFile()
    {
        try {
            //File file = new File("f1");
 
            // Create file if it does not exist
            boolean success = file.createNewFile();
            if (success)
            {
            writeFile();
            }
            else
            {
                 System.out.println("file already exists");
 
            }
        } catch (IOException e) {}
    }
 
 
    public void writeFile()
    {
        try {
 
            out = new BufferedWriter(new FileWriter(file));
            out.write("You have passed  \n");
            out.write("The Exams");
            out.close();
        } catch (IOException e) {}
    }
 
}
//To read file
import java.lang.*;
import java.io.*;
 
public class Read
{
    BufferedReader br;
    BufferedWriter out;
    public void readFile()
    {
        try {
            br = new BufferedReader(new FileReader("f1"));
            String Str;
            while ((Str = br.readLine()) != null)
            {
                   System.out.println(Str);
            }
            br.close();
 
        } catch (IOException e) {}
    }
}
//To copy file
import java.lang.*;
import java.io.*;
 
public class Copy
{
    public void copyFile()
    {
        try{
            InputStream in = new FileInputStream("f1");
            OutputStream out = new FileOutputStream("f2");
 
            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int length;
            while ((length = in.read(buf)) > 0)
            {
                out.write(buf, 0, length);
            }
            in.close();
            out.close();
            System.out.println("File copied.");
        }catch(IOException e) {}
    }
}
//To remove file
import java.lang.*;
import java.io.*;
 
public class Remove
{
    public void removeFile(File file)
    {
        boolean success = file.delete();
        if (!success)
        {
            System.out.println("File not deleted");
        }
    }
}
//To check for substring
 
import java.lang.*;
import java.io.*;
 
public class Sub
{
 
    BufferedReader br;
    BufferedWriter out;
 
    public void Search()
    {
        try {
            br = new BufferedReader(new FileReader("f1"));
            String Str;
            while ((Str = br.readLine()) != null)
            {
                   System.out.println(Str);
 
                   boolean found = true;
 
                   int index = Str.indexOf("have");
                   if(found)
                   {
 
                    System.out.println("Substring found");
                    System.out.println(br.readLine());
                }
                else
                    System.out.println(" ");
            }
            br.close();
 
        } catch (IOException e) {}
    }
 
 
}

Please help me to put all this together using threads. I've tried the main class but its really giving problems. Im looking forward to any one's reply.Thanks in advance.

Well, since it is homework, I'll just give a general tip and let you figure the rest. Make each of your operation classes extend Thread. This means they must all implement a "run()" method. I think you can figure out how that applies to what you are aiming for.

I would also suggest that the one thing all of these do is operate on a file, so you may consider this a good thing to pass into the constructor.

For the main method, read the tutorials on basic thread handling. It should be fairly straight forward from there.

commented: Nice advice, but I think its to early for threads :) +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.