개발이 좋아서/Java가 좋아서

    File

    이전 버전이다. Files 사용을 먼저 고려하자File file = new File("temp/example.txt");File directory = new File("temp/exampleDir");// 1. exists(): 파일이나 디렉토리의 존재 여부를 확인System.out.println("File exists: " + file.exists());// 2. createNewFile(): 새 파일을 생성boolean created = file.createNewFile();System.out.println("File created: " + created);// 3. mkdir(): 새 디렉토리를 생성boolean dirCreated = directory.mkdir();System.out.printl..

    I/O - XML, JSON

    객체 직렬화의 한계버전 관리의 어려움플랫폼 종속성성능 이슈유연성 부족크기 효율성XML- 유연하고 강력했지만, 복잡성과 무거움 id1 name1 20  JSON- 가겹고 간결하며, 자바스크립트와의 자연스러운 호환성- 사실상 표준 기술{ "member": { "id": "id1", "name": "name1", "age": 20 } }

    I/O 기본2 - 기타 스트림

    - PrintStreampublic static void main(String[] args) throws FileNotFoundException { FileOutputStream fos = new FileOutputStream("temp/print.txt"); PrintStream printStream = new PrintStream(fos); printStream.println("hello java!"); printStream.println(10); printStream.println(true); printStream.printf("hello %s", "world"); printStream.close();}hello java!10truehello world  - D..

    I/O 기본2 - 문자 다루기 (BufferedReader)

    public static final String FILE_NAME = "temp/hello.txt"; - OutputStreamWriter / InputStreamReader 사용public static void main(String[] args) throws IOException { String writeString = "abc"; System.out.println("write String: " + writeString); // 파일에 쓰기 FileOutputStream fos = new FileOutputStream(FILE_NAME); OutputStreamWriter osw = new OutputStreamWriter(fos, UTF_8); osw.write(wri..

    I/O 기본1 - 파일 입출력과 성능 최적화 (buffered)

    public static final String FILE_NAME = "temp/buffered.dat"; public static final int FILE_SIZE = 10 * 1024 * 1024; // 10MB public static final int BUFFER_SIZE = 8192; // 8KB buffered 스트림 - 파일 쓰기public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream(FILE_NAME); BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER_SIZE); lon..

    I/O 기본1 - InputStream / OutputStream

    메모리 스트림public static void main(String[] args) throws IOException { byte[] input = {1, 2, 3}; // 메모리에 쓰기 ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(input); // 메모리에서 읽기 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); byte[] bytes = bais.readAllBytes(); System.out.println(Arrays.toString(bytes));} 문자 스트림public static void m..

    I/O 기본1 - 스트림

    - new FileOutputStream("temp"/hello.dat")파일에 데이터를 출력하는 스트림이다.파일이 없으면 파일을 자동으로 만들고, 데이터를 해당 파일에 저장한다.폴더를 만들지는 않기 때문에 폴더는 미리 만들어두어야 한다.- write()byte 단위로 값을 출력한다.- new FileInputStream("temp/hello.dat")파일에서 데이터를 읽어오는 스트림이다.- read()파일에서 데이터를 byte 단위로 하나씩 읽어온다.파일의 끝에 도달해서 더는 읽을 내용이 없다면 -1 을 반환한다. 파일의 끝(EOF, End of File) 부분으로 나누어 읽기public static void main(String[] args) throws IOException { FileOutp..

    문자 인코딩

    1. ASCII 문자 집합65-90 : A-Z 대문자97-122 : a-z 소문자 2. UTF-1616bit(2byte) 기반단점: ASCII 영문도 2byte를 사용한다. ASCII와 호환되지 않음. 3. UTF-88bit(1byte) 기반, 가변길이 인코딩장점: ASCII 문자는 1바이트로 표현, ASCII 호환-현대의 사실상 표준 인코딩 기술  private static final Charset EUC_KR = Charset.forName("EUC-KR"); private static final Charset MS_949 = Charset.forName("MS949"); public static void main(String[] args) { System.out.prin..

    Exception

    1) ExceptionExam.java public class ExceptionExam{ public int get50thItem(int []array) throws IllegalArgumentException{ if(array.length < 50){ throw new IllegalArgumentException(); } return array[49]; } } ExamExam.java //아래는 실행을 위한 코드입니다. 수정하지 마세요. public class ExamExam{ public static void main(String[]args){ ExceptionExam ex = new ExceptionExam(); } }

    내부 클래스

    1) 클래스 안에 인스턴스 변수, 즉 필드를 선언하는 위치에 선언되는 경우. 보통 중첩클래스 혹은 인스턴스 클래스라고 한다. public class InnerExam1{ class Cal{ int value = 0; public void plus(){ value++; } } public static void main(String args[]){ InnerExam1 t = new InnerExam1(); InnerExam1.Cal cal = t.new Cal(); cal.plus(); System.out.println(cal.value); } } 2) 내부 클래스가 static으로 정의된 경우, 정적 중첩 클래스 또는 static 클래스라고 한다. public class InnerExam2{ static ..