'java파일명뽑기'에 해당되는 글 1

  1. 2011.06.24 java 폴더 파일 명과 디렉토리 명 뽑아오기
2011. 6. 24. 17:13 JAVA

cls
javac srcList.java
java srcList
PAUSE


import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;

/*
 * java srcList D:\KTF_WIPIC\Pipe\src skt
 * java srcList D:\KTF_WIPIC\Pipe\src lgt
 * skt일때는 파일 명만 lgt일때는 경로 까지 포함한다
 * 파일을 리스트를 뽑아내 텍스트 파일을 만든다
 */
public class srcList
{
    boolean DEBUG = false;
    int cursor = 0, cursor2 = 0;
    String szFileNameSKT[] = new String[128];
    String szFileNameLGT[] = new String[128];
    String szSKTName = "srcList2_skt.txt";
    String szLGTName = "srcList2_lgt.txt";
    String szFileNameSKTFolder[] = new String[128];
    String szSKTFolderName = "srcList2_sktfolder.txt";

    public static void main(String args[])
    {
        srcList mix = new srcList();
        String dir = System.getProperty("user.dir");
        mix.filterFile(dir);
        mix.writeFile(dir);
    }

    public void writeFile(String _curPath){
        String _szSKT = "", _szLGT = "", _szSKTFolder = "";
        for(int i =0; i < cursor; i++){
        //    if(DEBUG)System.out.println("["+i+"]"+szFileNameSKT[i]);
            _szSKT += szFileNameSKT[i]+"\\\n";
            _szLGT += szFileNameLGT[i]+"\\\n";
        }


        for(int i =0; i < cursor2; i++){
        //    if(DEBUG)System.out.println("["+i+"]"+szFileNameSKTFolder[i]);
            _szSKTFolder += szFileNameSKTFolder[i]+"\\\n";
        }

        _szSKT = _szSKT.substring(0, _szSKT.length() - 2);
        _szLGT = _szLGT.substring(0, _szLGT.length() - 2);

        _szSKT += "#";
        _szLGT += "#";

    //    if(DEBUG)System.out.println("SKT:"+_szSKT);
    //    if(DEBUG)System.out.println("LGT:"+_szLGT);

        byte bufNames[] = _szSKT.getBytes();
        save(_curPath, szSKTName, bufNames);

        bufNames = _szLGT.getBytes();
        save(_curPath, szLGTName, bufNames);

        bufNames = _szSKTFolder.getBytes();
        save(_curPath, szSKTFolderName, bufNames);
    }


    public void filterFile(String _curPath)
    {
        if(DEBUG)System.out.println("_curPath:" + _curPath);
        File _fileList = new File(_curPath);
        String _fileNameList[] = _fileList.list();
        String _fullPathFile;
        File _curFile;
        int _len, _idx;
        for(int i = 0 ; i < _fileNameList.length; i ++){
            _fullPathFile = _curPath+"\\"+_fileNameList[i];
            _curFile = new File(_fullPathFile);
            if(DEBUG)System.out.println("["+i+"]:"+_curFile);

            if(_curFile.isFile()){
                _len = _fileNameList[i].length();
                _idx = _fileNameList[i].indexOf(".c");
                if(_idx > 0 && _idx == _len - 2 ){
                    //if(DEBUG)System.out.println(" > 파일:" + _fileNameList[i]);
                    pushName(_curPath, _fileNameList[i]);
                }else{
                    //if(DEBUG)System.out.println(" > 일반파일이라서 패스:" + _fileNameList[i]);
                }
            }else{
            //    if(DEBUG)System.out.println(" > 디렉토리:" + _curFile);
            //    if(DEBUG)System.out.println(" > 디렉토리:" + _curFile.getName());
                pushFolderName(_fullPathFile);
                filterFile(_fullPathFile);
            }
        }
    }

    public void pushName(String _szParentPath, String _szFileName){
        szFileNameSKT[cursor] = _szFileName;
       
       
        int i ;
        int length = _szParentPath.length();
       
        byte dirbuf[]  = new byte[length];
        dirbuf = _szParentPath.getBytes();
        for(i = 0;i< length; i ++)
        {
            if(dirbuf[i]=='\\')
                dirbuf[i] = '/';
        }
        String szParentPath = new String(dirbuf);           
       
        szFileNameLGT[cursor] = szParentPath +"/"+ _szFileName;
                   
        cursor++;
    }


    public void pushFolderName(String _fullPathFile){
        szFileNameSKTFolder[cursor2] = _fullPathFile;
        cursor2++;
    }

    public void save(String directory, String fileName, byte data[]){
        FileOutputStream out = null;
        File file = new File(directory,fileName);
        try{
            out = new FileOutputStream(file);
            out.write(data);
        }catch(Exception e){
            if(DEBUG)System.out.println(fileName+"이 쓰다 오류:"+e);
        }

    }

}

'JAVA' 카테고리의 다른 글

자바 한글 있나 확인하기 (java string 한글 영어 구분)  (0) 2011.12.01
extra operand `/nologo' 에러  (0) 2011.07.15
MFC CBUTTON에 단축키 설정하기  (0) 2011.01.21
java파일 압축하기  (0) 2010.08.23
에러 수정방법  (0) 2010.08.20
posted by 욱이다
prev 1 next