File 입출력의 기본 예제.


public void initDNS(){

try {

FileReader mFile = new FileReader("dns.txt");

BufferedReader mBuffer = new BufferedReader(mFile);

String str = null;

do{

str = mBuffer.readLine();

System.out.println(str);

}while(!(str == null));

mBuffer.close();

mFile.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}



 각종 Parsing 하는 예제.

// return map for serving DNS

public Map<String,String> ParsingDNS() throws FileNotFoundException{

Map<String,String> sDNS = new HashMap<String,String>();

Scanner mainScanner =null;

String line;

int IndexFirst = -1;

int IndexSecond = -1;

if(mFileName == null) return null;

else{

mainScanner = new Scanner(new BufferedReader(new FileReader(mFileName)));

mainScanner.useDelimiter("\n");

while(mainScanner.hasNext()){

line = mainScanner.next();

IndexFirst = line.indexOf(" ");

if(IndexFirst >= 0){

System.out.println(line.substring(0, IndexFirst));

}

IndexSecond = line.indexOf(" ", IndexFirst+1);

if(IndexSecond >= 0){

System.out.println(line.substring(IndexFirst+1, IndexSecond));

}

}

return sDNS;

}

}



'Computer Science > Java' 카테고리의 다른 글

예제로 설명하는 java File 입출력  (0) 2013.05.26

+ Recent posts