Känner mig hyfsat nöjd nu med mitt första "riktiga" program, bara lite små grejer som inte riktigt funkar. Jag postar källkoden om någon är intreserad av att kolla resultatet och eventuellt ge feedback. Har dock inte lyckats få igång programmet i ubuntu (eftersom jag har problem med att få till hur man fixar sina classpath variabler), ni får väl se till att byta namn på klasserna om det behövs, om någon nu är intresserad.
main programmet:
Kod: Markera allt
import ajava.edu.*;
import java.util.*;
import java.io.File;
import static java.lang.System.*;
public class commandpromt
{
public static File currentdir = new File ("/."); // initiate root directory, and begin with root directory as current directory, globally known
public static void main (String [] args) throws Exception
{
Scanner in = new Scanner (System.in);
in.useLocale (Locale.US);
while (true)
{
out.print (currentdir.getCanonicalFile () + "> "); // show prompt with current directory
String command = in.nextLine (); // enter commands with or without arguments
Scanner scanner = new Scanner (command); // split command and let the arguments be handled by respective if-case
command = scanner.next ();
String arg1, arg2; // initiate arguments varibles
out.println ();
/********************* check below for commands and call their respective method with arguments if avablie *********************/
if (command.equals("ls"))
{
ls ();
}
else if (command.equals ("pwd"))
{
pwd ();
}
else if (command.equals ("mkdir"))
{
try
{
arg1 = scanner.next();
mkdir (arg1);
}
catch (NoSuchElementException e)
{
catchMess ();
}
}
else if (command.equals ("rm"))
{
try
{
arg1 = scanner.next();
rm (arg1);
}
catch (NoSuchElementException e)
{
catchMess ();
}
}
else if (command.equals ("cd"))
{
try
{
arg1 = scanner.next();
cd (arg1);
}
catch (NoSuchElementException e)
{
catchMess ();
}
}
else if (command.equals ("mv"))
{
try
{
arg1 = scanner.next();
arg2 = scanner.next();
mv (arg1, arg2);
}
catch (NoSuchElementException e)
{
catchMess ();
}
}
else if (command.equals ("cp"))
{
try
{
arg1 = scanner.next(); // send filename you want to copy
arg2 = scanner.next(); // send filename of the copy
cp (arg1, arg2);
}
catch (NoSuchElementException e)
{
catchMess ();
}
}
else if (command.equals ("exit") || command.equals ("logout") || command.equals ("logoff"))
break; // quit program
else
{
out.println ("wrong command or filename\n");
}
}
}
/************************* methods for the commands ***********************************/
public static void ls () // implements the list command
{
String[] catFiles = currentdir.list (); // list files in vectorform
for (int i = 0; i < catFiles.length; i++) // loop for printing out filelist
{
System.out.println (catFiles[i] + " ");
}
System.out.println ();
}
public static void pwd () throws Exception // shows current directory
{
System.out.println (currentdir.getCanonicalFile ());
System.out.println ();
}
public static void mkdir (String argument) // implements the makedirectory command
{
String curstring = currentdir.toString (); // makes current dirname to a string
File kat = new File (curstring, argument); // make a file object of the dir to make
if (kat.exists ()) // check if directory alredy exists and write mess
{
System.out.println ("The directory alredy exists");
System.out.println ();
}
else // else make directory and write mess
{
kat.mkdirs ();
System.out.println ("A directory with the name " + argument + " was created");
System.out.println ();
}
}
public static void rm (String argument) // implements the remove command, removes files and empty directorys
{
String curstring = currentdir.toString (); // makes current dirname to a string
File fd = new File (curstring, argument); // make a file object of the file or dir to delete
if (fd.exists ()) // check if file or dir exists and delete
{
fd.delete ();
System.out.println (argument + " was deleted");
System.out.println ();
}
else
{
System.out.println (argument + " doesnt exists");
System.out.println ();
}
}
/* if you write in runtime "cd /" or "cd \" the prompt will show ex. "C:\test\>" instead of "C:\test>" gonna see if i can fix this later*/
private static File cd (String argument)
{
File nextdir = new File (currentdir, argument); // make File object for the next dir
if (nextdir.isFile ()) // error managment if you try to point to a file
{
out.println ("not a directory"); // do nothing
out.println ();
}
else if (nextdir.exists ()) // check if directory exists
{
currentdir = nextdir; // point to the changed directory as the current
}
else // otherwise the dir doesent exists
{
out.println ("the directory doesent exists");
out.println ();
}
return currentdir;
}
public static void mv (String argument1, String argument2) throws Exception // implements the move/rename command, moving dirs is currently not working
{
File oldFile = new File (currentdir, argument1);
File newFile = new File (currentdir, argument2);
String curstring = currentdir.toString ();
if (!oldFile.exists ()) // if the source file doesent exists write mess
{
nofileMess ();
}
else if (newFile.exists ()) // if destination file alredy exists write mess
{
System.out.println ("The file or directory alredy exists");
System.out.println ();
}
else // else copy and delete file (move file)
{
afile.copy (argument1, argument2, curstring);
oldFile.renameTo (newFile);
oldFile.delete ();
System.out.println ("the file " + argument1 + " has been moved, and renamed to " + argument2);
System.out.println ();
}
}
/*throws ioException doesnt work for some reson, but Exception does the work :)*/
public static void cp (String argument1, String argument2) throws Exception // implements the copy command, reads one file an creates and writes a new file, copying dirs is currently not working
{
File oldfile = new File (currentdir, argument1); // make objects of the files you want to copy
File newfile = new File (currentdir, argument2);
String curstring = currentdir.toString ();
if (!oldfile.exists()) // if the file you want to copy dont exists write mess
{
nofileMess ();
}
else if (newfile.exists ()) // makes shure you dont copy over an existing file
{
System.out.println ("The targetfile alredy exists");
System.out.println ();
}
else // otherwise go on and copy :)
{
afile.copy (argument1, argument2, curstring);
System.out.println (argument1 + " was copied to " + argument2);
System.out.println ();
}
}
/************************** some methods for text messages ****************************/
public static void catchMess ()
{
System.out.println ("wrong argument(s), try again");
out.println ();
}
public static void nofileMess () // justs writes a message in some other methods where files dont exists
{
System.out.println ("The file or directory doesnt exists");
System.out.println ();
}
}
min klass som används till kopiering av filer:
Kod: Markera allt
package ajava.edu;
import java.io.*;
import java.util.*;
public class afile
{
public static byte[] readbytes (int filesize, String filename, String directory) throws IOException // method that reads bytes from a file an store it in a variable
{
File file = new File (directory, filename);
FileInputStream end = new FileInputStream (file);
byte[] bv = new byte[filesize]; // fills a vector with bytes from a file
int nrOfbytes = end.read (bv); // read an store bytes to the vector
end.close ();
return (bv); // return vector to the calling program
}
public static void writebytes (byte[] bv, int filesize, String filename, String directory) throws IOException // method that writes bytes to a file
{
File file = new File (directory, filename);
FileOutputStream fout = new FileOutputStream (file); // open filestream
fout.write (bv); // write vector
fout.close ();
}
public static void copy (String argument1, String argument2, String directory) throws IOException // copy an old file and creates the copy in a new file, directorys is currently not working
{
File oldfile = new File (directory, argument1);
File newfile = new File (directory, argument2);
int filelength = (int) oldfile.length (); // get length of file to copy
newfile.createNewFile (); // make newfile
byte[] bytesTocopy = new byte[filelength];
bytesTocopy = readbytes (filelength, argument1, directory); // read the old file
writebytes (bytesTocopy, filelength, argument2, directory); // write the new file with data from the old file
}
}
Hoppas ni orkar klippa och klistra
