요점/Java(3)
-
File 객체를 만들 때 Path 객체를 이용하는 이유
private static final String APPLICATION_FILENAME = "config.txt"; ... trt { String classPath = getClassPath(); Path appPath = Paths.get(classPath, APPLICATION_FILENAME); File appFile = new File(appPath.toString()); if (appFile.isFile()) { System.out.println("It is a regular file."); List lines = Files.readAllLines(appPath, StandardCharsets.UTF_8); ... // 파일에서 줄로 읽어서 처리할 수 있다. } else { System.out...
2023.06.23 -
LinkedList poll() vs removefirst()
LinkedList에서 poll()과 removeFirst()은 모두 리스트의 첫 번째 요소를 제거하고 반환하는 기능을 수행한다. - poll(): LinkedList에서 첫 번째 요소를 제거하고 반환한다. 만약 LinkedList가 비어있을 경우에는 null을 반환한다. LinkedList list = new LinkedList(); list.add("foo"); list.add("bar"); String firstElement = list.poll(); System.out.println(firstElement); // Prints "foo" - removeFirst(): LinkedList에서 첫 번째 요소를 제거하고 해당 요소를 반환한다. 만약 LinkedList가 비어있을 경우에는 NoSuchEl..
2023.05.24 -
Class 개체
class 개체(Instance of the class Class) 어떤 클래스의 개체인 class 개체는 클래스의 메타데이터를 가지고 있다. 즉 클래스의 구조(상속 등), 멤버 변수, 메서드 등에 대한 정보를 가지고 있다. class 개체는 reflection API의 일종으로 클래스의 정보를 동적으로 조작할 수 있다. 즉 실행 중인 프로그램의 클래스의 정보를 조작할 수 있다. class 개체를 사용하려면 예약어인 class 리터럴을 사용하면 된다. URL resourceUrl = MyClass.class.getResource("data.txt"); Class 개체는 클래스 로더가 클래스를 생성할 때 자동으로 같이 생성한다. getResource(String name) class 개체의 getResou..
2023.05.22