삼성SDS_멀티캠퍼스
-
17일 차 입출력을 통해 파일복사삼성SDS_멀티캠퍼스/Java 2015. 10. 1. 10:40
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; public class Test2 { public static void main(String[] args) {FileInputStream in = null;FileOutputStream out = null; try { in = new FileInputStream("ocjp덤프.pdf");out = new FileOutputStream("ocjp덤프_복사본.pdf");//ocjp덤프.pdf의 모든 바이트를 읽어서 ocjp덤프_복사본.pdf에 그대로 써보기byte[] buf = ne..
-
17일 차 파일 입출력삼성SDS_멀티캠퍼스/Java 2015. 10. 1. 09:27
그런데 in, out객체에는 현재 null값이 있기 때문에 만약 아무런 값이 안생겼을 경우 nullPoint익셉션을 발생시켜 런타임에러가 뜰 것이다. 이렇게 그것을 방지하기 위해 if문을 넣어서 null값이 아닐 경우 clsoe메소드를 사용하도록 명령해야 한다. 하지만 이렇게 해도 파일을 찾을 수 없다고 에러를 발생시킬 것이다. 왜냐면 존재하지 않는 파일을 쓰기도 전에 읽었기 때문인데 그렇다면 먼저 쓰고나서 읽으면 될 것이다. 먼저 쓰고 읽게 하면 된다. out객체에 0부터 9까지 쓰고 (바이트라서 -128~127밖에 못읽지만 그냥 int로 처리하자) in객체로 읽은 값을 c에 저장한다. 그런데 read메소드는 0~255까지밖에 읽지를 못한다. 그래서 값이 없을 경우 -1을 반환하게 하면된다 (어차피 표..
-
16일 차 Thread(쓰레드)를 이용하여 발사 카운트 시키기삼성SDS_멀티캠퍼스/Java 2015. 9. 30. 13:39
public class Ct implements Runnable { @Overridepublic void run() {// TODO Auto-generated method stub try {for (int i = 20; i > 0; i--) {Thread.sleep(1000);System.out.println(i + "초");}} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} } } public class Balsa implements Runnable { @Overridepublic void run() {// TODO Auto-generated method stub try {System.out...
-
16일 차 Thread(쓰레드)로 1초에 한번씩 현재시간과 인삿 말 출력삼성SDS_멀티캠퍼스/Java 2015. 9. 30. 13:37
import java.util.Date; class CH implements Runnable { @Overridepublic void run() {// TODO Auto-generated method stub try { while (true) {Thread.sleep(1000);System.out.println(new Date() + " 안녕하세요");}} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}} } public class Hello { public static void main(String[] args) { CH ch = new CH(); ch.run();} }
-
16일 차 Thread(쓰레드) 동기화 문제삼성SDS_멀티캠퍼스/Java 2015. 9. 30. 11:21
public class BankAccount { private int balance; public void deposit(int account) {balance += account;} public void withdraw(int account) {balance -= account;} public int getBalance() {return balance;}} public class User implements Runnable { private BankAccount account; public User(BankAccount account) {this.account = account;} public void run() {// TODO Auto-generated method stubfor (int i = 0;..
-
15일 차 도서 대여 프로그램삼성SDS_멀티캠퍼스/Java 2015. 9. 25. 16:56
package book; public abstract class Book { private int id;private String name;private String author;private boolean rental; public boolean equlas(int id) { if (this.id == id)return true;elsereturn false; } public Book() {id = 0;name = "제목없음";author = "저자미상";rental = false;} public Book(int id, String name, String author) {this.id = id;this.name = name;this.author = author;rental = false;} public..
-
15일 차 Map삼성SDS_멀티캠퍼스/Java 2015. 9. 25. 11:28
import java.util.HashMap;import java.util.Map; class Student {int number;String name; public Student(int number, String name) {this.number = number;this.name = name; } public String toString() {return name;}} public class Test8 { public static void main(String[] args) { Map st = new HashMap();st.put("2009001", new Student(2009001, "구준표"));st.put("2009002", new Student(2009002, "금잔디"));st.put("20..
-
15일 차 HashSet을 이용한 로또 번호 추출기삼성SDS_멀티캠퍼스/Java 2015. 9. 25. 10:37
import java.util.HashSet;import java.util.LinkedList;import java.util.List; public class Test6 { public static void main(String[] args) { HashSet set = new HashSet(); while (set.size() < 6) {set.add((int) (Math.random() * 45) + 1);} System.out.println(set); } }