Languages/JAVA 81

File 클래스

import java.io.File; public class FileTest01 { public static void main(String[] args) { File file = new File("c:/tmp/data01.zip"); String fileName = file.getName(); int pos = fileName.lastIndexOf("."); //6 System.out.println("경로를 제외한 파일이름: " + fileName); System.out.println("확장자를 제외한 파일이름: " + fileName.substring(0,pos)); System.out.println("파일의 종류(확장자): " + fileName.substring(pos+1)); System.out...

Languages/JAVA 2022.08.24

[파일 Copy] 읽고 쓰기

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileInOutStreamTest01 { public static void main(String[] args) { FileInputStream fis = null; FileOutputStream fos = null; try { // 1번 - 문서 파일 (용량이 아주 작음) /* fis =new FileInputStream("c:/tmp/goods01.txt"); //직접 텍스트파일 생성 'When there's a will, there's a way.'내용입력. fos = new FileOutputStream("c:..

Languages/JAVA 2022.08.24

[파일 입출력] 파일 출력

1. 바이트 기반 스트림 - FileInputStream, FileOutputStream - jpg, mp3, avi, mp4, exe ... 2. 문자 기반 스트림 - FileReader, FileWriter - txt - C드라이브에 'tmp' 폴더 생성 - 텍스트 문서 새로 만들기 파일명은 'test01' import java.io.FileInputStream; // io; input Output import java.io.FileNotFoundException; //입출력에 관한 예외로 io안에 있음 import java.io.IOException; public class FileInputStreamTest01 { public static void main(Str..

Languages/JAVA 2022.08.24

Account 계좌의 입출금 예외처리

// 은행 계좌 클래스 class Account { private String name; // 계좌 명의 private String no; // 계좌번호 private int balance; //예금잔고 public Account(String name, String no, int balance) { this.name = name; this.no = no; this.balance = balance; } // 입금 → 문제 발생: 음수를 입력하여 입금을 하게 되는 경우 // 1번 방법 - 논리적인 방법으로 해결 /* public void deposit(int money) { if(money

Languages/JAVA 2022.08.24

예외처리 문제

문제) 1. 이름(name)과 나이(age)를 멤버변수로 갖는 Member 클래스를 생성하시오. 2. names와 scores를 사용하여 members에 저장하시오. 3. members에 저장된 내용을 출력하시오. 4. 출력 중에 발생하는 예외를 확인하고, 예외처리를 하시오. 5. 예외의 발생유무와 관계없이 회원점수의 총점과 평균을 구하시오. class Member { private String name; private int score; public Member(String name, int score) { this.name = name; this.score = score; } public String getName() { return name; } public int getScore() { retur..

Languages/JAVA 2022.08.23

예외를 던지는 방법

public class ExceptionTest10 { public static void main(String[] args) throws Exception { // 3. throws Exception 적으면 에러 해결. // 예외를 던지는 방법 - 자신의 책임을 전가하는 것으로 좋은 방법이 아님. method1(); } public static void method1() throws Exception { // 2. throws Exception 적으면 main()에서 호출한 method1에러 method2(); } public static void method2() throws Exception { // 1. throws Exception 적으면 method1()에서 호출한 method2에러 Except..

Languages/JAVA 2022.08.23