본문 바로가기

자바

소수점 올림 자바에서, 소수점 자르기 사용. 반올림을 하는 것이 아니라, 그냥 자르고 싶을 경우 사용한다. * 소수점 올리기. numberObj는 잘라줄 변수를 넣어준다. private String roundInSecond(Double numberObj){ return String.format("%.2f", numberObj); } 자바의 String.format을 사용하면, 깔끔하고 쉽게 코드를 사용 할 수 있기에, 공부가 필요하다. * 여기서 응용 소수점 셋째자리에서 자르고 싶다면? 다음과 같이 쓰면 되겠다. 적절히 응용해서 사용하자. 물론, String.format도 공부하는 시간을 가집시다. private String roundInSecond(Double numberObj){ return String.form.. 더보기
디렉토리 지우기 자바의 File에서 디렉토리를 지우게 될경우, 하위 디렉토리가 있다면, 지워지게 되지 않는다. 그래서, 디렉토리를 지워줄때엔 재귀호출을 이용하여, 지워주어야 한다. public boolean deleteFile(String filePath){ File file = new File(filePath); String[] subDirs = null; String path = filePath; if(file.isDirectory()){ subDirs = file.list(); int subDirsLenght = subDirs.length; for(int i=0; i 더보기
Blank Final 이란? 자바에서 변수를 생성할때, 다음과 같이 생성하지요, private String name; 만약, 변수가 바뀌는걸 원하지 않는다면, 선언부에 final을 적게 되지요, private final String name; 여기서, 용어가 하나 나오는데, Blank Final이라는 것입니다. final 변수를 선언과 동시에 초기화를 해주는 것이 아닌, 생성자에서 초기화를 해주는 것을 말합니다. 즉, private final String name = "멍보"를 하는 대신, 생성자에서 초기화 시켜주는 겁니다. 예로는 다음과 같겠지요, class BlankFinal { private final String name; public BlankFinal(String name){ this.name = name; } } 다음 .. 더보기
inputStream to String inputStream을 String으로 출력한다. public String isToString(InputStream is) throws IOException { if (is != null) { Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { Reader reader = new BufferedReader( new InputStreamReader(is, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { is.close(); } return writer.toString(); } else { ret.. 더보기