자바로 라인에디터 만들기

lfs의 이미지

import java.lang.*;
import java.io.*;
import java.util.*;
 
public class lineeditor{
    public static void main(String args[]) throws Exception{
        Command command=new Command();
        Console console=System.console();
 
        while(true){
            System.out.print("명령:");
            String inputChar=console.readLine();
 
            if(inputChar.equals("fopen"))
                command.fopen();
            else if(inputChar.equals("fclose"))
                command.fclose();
            else if(inputChar.equals("fsave"))
                command.fsave();
            else if(inputChar.equals("list"))
                command.list();
            else if(inputChar.equals("mods"))
                command.mods();
            else if(inputChar.equals("help"))
                command.help();
            else if(inputChar.equals("exit"))
                break;
            else if(inputChar.equals("quit"))
                break;
            else{
                System.out.println("잘못입력하셨습니다.");
                continue;
            }
 
        }
 
    }
 
}
 
class Command{
    static final int SIZE=1000000;
    TreeMap<Integer,String> map=new TreeMap<Integer,String>();
    Map.Entry<Integer,String> entry;
 
    Scanner scanner=new Scanner(System.in);
    InputStream is=System.in;
    String Kfilename=null;
 
    public void fopen() throws Exception{
 
        System.out.print("파일명 입력>>");
 
        byte[] dataf=new byte[128];
        int fileBytes=is.read(dataf);
        String filename=new String(dataf,0,fileBytes-2);
 
        this.Kfilename=filename;
 
        File file=new File(this.Kfilename);
        boolean isExist=file.exists();
        if(isExist==false){
            System.out.println("파일이 없습니다.");
            return ;
        }
 
        FileReader fr=new FileReader(file);
 
        Integer iNo=1;
        int readCharNo;
        char[] cbuf=new char[SIZE];
 
        while((readCharNo=fr.read(cbuf)) != -1){
            String iData=new String(cbuf,0,readCharNo);
 
            String[] Datasp=iData.split("\n");
            for(String Da:Datasp){
                map.put(iNo,Da);
                iNo+=1;
            }
        }
        fr.close();
 
        System.out.println("메모리입력되었습니다...");
    }
 
    public void fclose() throws Exception{
        map.clear();
        System.out.println("메모리해제되었습니다...");
 
    }
 
    public void fsave() throws Exception{
        File file=new File(this.Kfilename);
        FileWriter fw=new FileWriter(file);
 
        NavigableMap<Integer,String> descendingMap=map.descendingMap();
        NavigableMap<Integer,String> ascendingMap=descendingMap.descendingMap();
        Set<Map.Entry<Integer,String>> ascendingEntrySet=ascendingMap.entrySet();
        for(Map.Entry<Integer,String> en:ascendingEntrySet){
            fw.write(en.getValue()+"\n");
        }
        fw.flush();
        fw.close();
 
        System.out.println("저장되었습니다...");
    }
 
    public void list() throws Exception{
        NavigableMap<Integer,String> descendingMap=map.descendingMap();
        NavigableMap<Integer,String> ascendingMap=descendingMap.descendingMap();
        Set<Map.Entry<Integer,String>> ascendingEntrySet=ascendingMap.entrySet();
        for(Map.Entry<Integer,String> en:ascendingEntrySet){
            System.out.println(en.getKey()+"\t"+en.getValue());
        }
 
        System.out.println("목록이 표시되었습니다...");
    }
 
    public void mods() throws Exception{
        System.out.print("편집할 라인수 입력:");
        int LineNo=scanner.nextInt();
        /*
        if(LineNo.equals("\0")){
            System.out.println("잘못 입력하셨습니다.m");
            return ;
        }
        */
        entry=map.floorEntry(LineNo);
        System.out.println("편집라인:"+entry.getKey()+"\t"+entry.getValue());
 
        System.out.print("편집문자입력:");
 
        byte[] datas=new byte[100];
        int nameBytes=is.read(datas);
        String ModLine=new String(datas,0,nameBytes-2);
 
        map.put(entry.getKey(),ModLine);
 
        System.out.println("라인 편집되었습니다...");
    }
 
    public void help() throws Exception{
        System.out.println("★라인에디터★");
        System.out.println("도움말 0.1버젼");
        System.out.println("fopen: 화일을 열기");
        System.out.println("fclose:화일을 닫기");
        System.out.println("fsave: 화일을 저장");
        System.out.println("list:  모든행 표시");
        System.out.println("mods:  지정행 수정");
        System.out.println("exit:quit:  끝마침");
        System.out.println("help:  도움말 표시");
        System.out.println("명령어 도움말입니다.");
    }
}

이상입니다.
소스 올려달라는 사람이 있어서 올렸습니다.

Forums: