Can anyone pointed out what is wrong with this code. I got an annoying error something with Scanner method.
import java.util.*;
import java.io.*;
//import java.lang.System.*;
public class cTedit implements ICommand{
@Override
public void Execute() {
InputStream istream;
OutputStream ostream = null;
int x;
final int EOF = -1;
istream = System.in;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name of the file");
String filename = scan.nextLine();
File outFile = new File(cShell.Currentpath+"\\"+filename);
System.out.println("Edit a file - Press Ctrl+z to end");
try {
ostream = new FileOutputStream(outFile);
while((x = istream.read())!= EOF)
ostream.write(x);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Error: " + e.getMessage());
}
finally{
try{
istream.close();
ostream.close();
}
catch(IOException e)
{
System.out.println("File did not close");
}
}
}
}
I also have another class where the main is:
import java.util.HashMap;
import java.util.Scanner;
public class cShell {
static String Currentpath="C:\\";
public String Current = Currentpath;
static HashMap<String, ICommand> myhashData=new HashMap<String, ICommand>();
public static void main(String[] args)
{
myhashData.put("ls", new cLS());
myhashData.put("gd", new cGD());
myhashData.put("md", new cMD());
myhashData.put("rnd", new cRND());
myhashData.put("del", new cDEL());
myhashData.put("hd", new cHD());
myhashData.put("uhd", new cUHD());
myhashData.put("ltf", new cITF());
myhashData.put("nbc", new cNBC());
myhashData.put("gdb", new cGDB());
myhashData.put("Tedit", new cTedit());
System.out.print(Currentpath+"> ");
Scanner scan = new Scanner(System.in);
String Input = scan.nextLine().trim();
if(Input.equals("exit")){
System.exit(0);
}
if(myhashData.containsKey(Input))
{
ICommand myCommand=myhashData.get(Input);
myCommand.Execute();
}
else
{
System.out.println("Invalid Command");
}
}
}