//************************************************************* // Java console application // 「FileOperation」 // //          作 成 者:ルート高菜    //          作成開始月:2006/4 //          最終更新月:2006/4 [TN075/J65] //************************************************************* import java.io.*; import java.text.SimpleDateFormat; import java.util.*; public class FileOperation { public static void main(String[] args) throws IOException { // String command[]; //標準入力 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); //タイトル System.out.println(""); System.out.println("Java console apprication"); System.out.println("DOS-like Character User Interface"); System.out.println("\"FileOperation\""); System.out.println("(C) Copyright 2006 TakanaFactory"); System.out.println(""); //起動時のディレクトリ取得 File file=new File("."); file=file.getCanonicalFile(); // while(true){ // System.out.print(file.getAbsolutePath()+">"); //コマンドとパラメーターを分ける command=br.readLine().split(" "); if(command.length==0 || command[0].length()==0){ continue; } //コマンド別の処理 if(command[0].equals("binary")){ if(command.length>=2){ File tmpFile=composePath(file,command[1]); if(tmpFile.exists()){ if(tmpFile.isFile()){ System.out.println(""); commandBinary(tmpFile); System.out.println(""); }else{ System.out.println("ファイル名が無効です。\n"); } }else{ System.out.println("指定されたファイルが見つかりません。\n"); } }else{ System.out.println("コマンドの構文が誤っています。\n"); } }else if(command[0].equals("cd")){ if(command.length>=2){ File tmpFile=composePath(file,command[1]); if(tmpFile.exists()){ if(tmpFile.isDirectory()){ // file=tmpFile; System.out.println(""); }else{ System.out.println("ディレクトリ名が無効です。\n"); } }else{ System.out.println("指定されたパスが見つかりません。\n"); } }else{ System.out.println(file.getAbsolutePath()+"\n"); } }else if(command[0].equals("copy")){ if(command.length>=3){ File tmpFileSource=composePath(file,command[1]); File tmpFileDestination=composePath(file,command[2]); if(tmpFileSource.exists()){ if(tmpFileSource.isFile()){ // commandCopy(tmpFileSource,tmpFileDestination); System.out.println(""); }else{ System.out.println("ファイル名が無効です。\n"); } }else{ System.out.println("指定されたファイルが見つかりません。\n"); } }else{ System.out.println("コマンドの構文が誤っています。\n"); } }else if(command[0].equals("del")){ if(command.length>=2){ File tmpFile=composePath(file,command[1]); if(tmpFile.exists()){ if(tmpFile.isFile()){ // if(tmpFile.delete()){ System.out.println(""); }else{ System.out.println("ファイル "+command[1]+" の削除に失敗しました。\n"); } }else{ System.out.println("ファイル名が無効です。\n"); } }else{ System.out.println(command[1]+" が見つかりませんでした。\n"); } }else{ System.out.println("コマンドの構文が誤っています。\n"); } }else if(command[0].equals("dir")){ if(file.isDirectory()){ System.out.println(""); commandDir(file); System.out.println(""); } }else if(command[0].equals("exit")){ System.exit(0); }else if(command[0].equals("md")){ if(command.length>=2){ File tmpFile=composePath(file,command[1]); if(!tmpFile.exists()){ if(tmpFile.mkdirs()){ System.out.println(""); }else{ System.out.println("ディレクトリ "+command[1]+" 作成に失敗しました。\n"); } }else{ System.out.println("サブディレクトリまたはファイル "+command[1]+" は既に存在します。\n"); } }else{ System.out.println("コマンドの構文が誤っています。\n"); } }else if(command[0].equals("list")){ if(command.length>=2){ File tmpFile=composePath(file,command[1]); if(file.isDirectory()){ commandList(file,tmpFile); System.out.println(""); } }else{ System.out.println("コマンドの構文が誤っています。\n"); } }else if(command[0].equals("propaty")){ if(command.length>=2){ File tmpFile=composePath(file,command[1]); if(tmpFile.exists()){ if(tmpFile.isFile()){ System.out.println(""); commandProperty(tmpFile); System.out.println(""); }else{ System.out.println("ファイル名が無効です。\n"); } }else{ System.out.println("指定されたファイルが見つかりません。\n"); } }else{ System.out.println("コマンドの構文が誤っています。\n"); } }else if(command[0].equals("ren")){ if(command.length>=3){ File tmpFileBefore=composePath(file,command[1]); File tmpFileAfter=composePath(file,command[2]); if(tmpFileBefore.exists()){ if(tmpFileBefore.isFile()){ // if(tmpFileBefore.renameTo(tmpFileAfter)){ System.out.println(""); }else{ System.out.println(command[1]+" のファイル名変更に失敗しました。\n"); } }else{ System.out.println("ファイル名が無効です。\n"); } }else{ System.out.println("指定されたファイルが見つかりません。\n"); } }else{ System.out.println("コマンドの構文が誤っています。\n"); } }else if(command[0].equals("type")){ if(command.length>=2){ File tmpFile=composePath(file,command[1]); if(tmpFile.exists()){ if(tmpFile.isFile()){ System.out.println(""); commandType(tmpFile); System.out.println(""); }else{ System.out.println("ファイル名が無効です。\n"); } }else{ System.out.println("指定されたファイルが見つかりません。\n"); } }else{ System.out.println("コマンドの構文が誤っています。\n"); } }else{ if(command[0].length()>=2 && command[0].charAt(1)==':'){ File tmpFile=new File(command[0]); if(tmpFile.exists()){ // file=tmpFile.getCanonicalFile(); System.out.println(""); }else{ System.out.println("指定されたドライブが見つかりません。\n"); } }else{ System.out.println("'"+command[0]+"'は、内部コマンドまたは外部コマンド、"); System.out.println("操作可能なプログラムまたはバッチ ファイルとして認識されていません。\n"); } } } } //-------------------------------------------------------------------- //コマンド処理 //-------------------------------------------------------------------- //バイナリファイルのバイナリエディタ風表示 static void commandBinary(File file) throws IOException { // int data; String sdata; char cc[]=new char[20]; // FileInputStream fs=new FileInputStream(file); long size=file.length(); // int n=0; for(int b=0;b<(int)size;b++){ //1バイトづつ読み込み、16進数へ変換 data=fs.read(); if(data<16){ sdata="0"+Integer.toHexString(data); }else{ sdata=Integer.toHexString(data); } //ASCII文字のみテキストに変換 if(data>31 && data<127){ cc[n]=(char)data; }else{ cc[n]='-'; } // System.out.print(sdata+" "); n++; if(n>=20){ //画面右のテキスト表示 n=0; System.out.println(cc); } } //端数の処理 if(n>0){ for(int i=n;i<20;i++){ System.out.print(" "); } for(int i=0;i\t"); System.out.print("\t"); System.out.println(fileList[f]); } } //ファイルのみ for(int f=0;f