728x90

 

 

 

 

 

 

import java.util.Scanner;

public class practice_everyday13 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		// 삼항연산자 (2중) + do while문
		int num1;
		int num2;
		do { 
			System.out.print("수1 입력 : ");
			num = sc.nextInt();
			System.out.print("수2 입력 : ");
			num2 = sc.nextInt();
			System.out.println(num1 == num2 ? "같아 멈춰":(num1>num2 ? "1이 커":"2가 커"));
			
		}while(num1 !=num2);
			
		// for문 초기화 생략
		int num3 = 5;
		int i = 0;
		for(; i < num3; i+=2) { // 초기화 생략 : int i +=0;
			System.out.println(i);
		}	
    }
}
728x90
반응형
728x90
728x90
반응형

'small steps > 운동 - 체력이 국력' 카테고리의 다른 글

22.03.01 운동기록  (0) 2022.03.02
22.02.28 운동기록  (0) 2022.03.01
22.02.26 운동기록  (0) 2022.02.27
22.02.25 운동기록  (0) 2022.02.26
22.02.24 운동기록  (0) 2022.02.25
728x90

아래 이미지는 1번 파일을 0번 파일이 상속했다

자식 클래스 부모 클래스의 이름을 눌러보면 부모클래스와 연결되어있는 코드들의 색깔이 진해진다

이 코드들은 중복이기에 삭제하여도 부모와 자식 클래스의 정보가 연동된다

 

삭제 전

삭제 후

0번 parent 

1번 child1

2번 child2

상속관계가 0 -> 1 -> 2

1번이 0번을 상속하고 2번이 1번을 상속한 것

 

1번 child1에서 parent에 받았던 a,b부분들을 주석 처리해서 2번 child2에서 보면 정보가 그대로인 걸 알 수 있다

 

public class practice_everyday04 {
	public static void main(String[] args) {
		
		// run
		practice_everyday04_0 pe1 = new practice_everyday04_0();
		pe1.println();
		
		practice_everyday04_1 pe2 = new practice_everyday04_1();
		pe2.println();
		
		practice_everyday04_2 pe3 = new practice_everyday04_2();
		pe3.println();
		
	}	
}
public class practice_everyday04_0 {
	
	// Parent
	private int a;
	private int b;
	
	public practice_everyday04_0() {}
	public practice_everyday04_0(int a, int b) {
		this.a = a;
		this.b = b;
	}
	//getter & setter
	public int getA() {
		return a;
	}
	public void setA(int a) {
		this.a = a;	
	}
	public int getB() {
		return b;
	}
	public void setB(int b) {
		this.b = b;
	}
	
	
	public void println() {
		System.out.println("parent");
	}
	
}

 



public class practice_everyday04_1 extends practice_everyday04_0 {
		
	// child1
//	private int a;
//	private int b;
	private int c;
	
	public practice_everyday04_1() {}
	public practice_everyday04_1(int a, int b, int c) {
//		this.a = a;
//		this.b = b;
		this.c = c;
	}
	
	// getter & setter
	// setter : 데이터를 변수에 저장하는 메소드
	// getter : 저장된 데이터를 불러오는 메소드
//		public int getA() {
//			return a;
//		}
//		public void setA(int a) {
//			this.a = a;	
//		}
//		public int getB() {
//			return b;
//		}
//		public void setB(int b) {
//			this.b = b;
//		}
		public int getC() {
			return c;
		}
		public void setC(int c) {
			this.c = c;
		}
		
	@Override	
	public void println() {
		System.out.println("child1");
	}
	
}

 

// 상속 & overriding
public class practice_everyday04_2 extends practice_everyday04_1{
	
	// child2
	private int a;
	private int b;
	private int c;
	private int d;
	
	public practice_everyday04_2() {}
	public practice_everyday04_2(int a, int b, int c, int d) {
		this.a = a;
		this.b = b;
		this.c = c;
		this.d = d;
	}
	
	// getter & setter
		public int getA() {
			return a;
		}
		public void setA(int a) {
			this.a = a;	
		}
		public int getB() {
			return b;
		}
		public void setB(int b) {
			this.b = b;
		}
		public int getC() {
			return c;
		}
		public void setC(int c) {
			this.c = c;
		}
		public int getD() {
			return d;
		}
		public void setD(int d) {
			this.d = d;
		}
	@Override
	public void println() {
		System.out.println("child2");
	}
		

	
	
		
}

 

728x90
반응형
728x90


728x90
반응형

'small steps > 운동 - 체력이 국력' 카테고리의 다른 글

22.02.28 운동기록  (0) 2022.03.01
22.02.27 운동기록  (0) 2022.02.28
22.02.25 운동기록  (0) 2022.02.26
22.02.24 운동기록  (0) 2022.02.25
22.02.22 운동기록 -2시간 운동 오버트레이닝일지도…  (0) 2022.02.23
728x90

 

try with resource

자동으로 close() 해주는 구문

close()를 위해 finally 구문을 사용할 필요도 없어진다 

 

 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class practice_everyday10 {
		
		// 1.바이트 기반+보조 : 입력
	public void Stream_outputByte() {
		// 목적 : 파일에 바이트기반으로 데이터를 빠르게 쓰고 싶다
		try (BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream("D:\\test\\test.txt"))){
			
			bos.write(65);
			byte[] arr = {66,67,68,69};
			bos.write(arr);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void Stream_inputByte() {
		// 목적 :파일에 있는 데이터를 바이트 기반으로 빠르게 읽어오고 싶다
		try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\test\\test.txt"))) {
			
			int val;
			while((val=bis.read()) != -1) {
				System.out.println(val);
			}	
		} catch (IOException e) { //IOException이 FileInputStream의 부모이기에 따로 예외처리하지않아도 같이 처리
			e.printStackTrace();
		}
		
	}	
}

 

 

728x90
반응형
728x90


728x90
반응형
728x90

 

기반 스트림 + 보조 스트림

성능향상

 

 

 

public class practice_everyday09_Run {
	public static void main(String[] args) {
		practice_everyday09 pe9 = new practice_everyday09();
		
		pe9.Stream_outputByte();
		pe9.Stream_inputByte();

	}	
}

 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class practice_everyday09 {
		
		// 1.바이트 기반+보조
		// 2.문자 기반+보조
		
		
		// 1.바이트 기반+보조 : 입력
	public void Stream_outputByte() {
		// 목적 : 파일에 바이트기반으로 데이터를 빠르게 쓰고 싶다
		FileOutputStream fos;
		BufferedOutputStream bos = null;
		try {
			fos = new FileOutputStream("D:\\test\\test.txt");
			bos = new BufferedOutputStream(fos);
			
			bos.write(65);
			byte[] arr = {66,67,68,69};
			bos.write(arr);
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void Stream_inputByte() {
		// 목적 :파일에 있는 데이터를 바이트 기반으로 빠르게 읽어오고 싶다
		FileInputStream fis;
		try {
			fis = new FileInputStream("D:\\test\\test.txt");
			BufferedInputStream bis = new BufferedInputStream(fis);
			
			int val;
			while((val=bis.read()) != -1) {
				System.out.println(val);
			}	
		} catch (IOException e) { // IOE
			e.printStackTrace();
		}
		
		

	// 2.문자 기반+보조

		
		
	}	
}
728x90
반응형
728x90


728x90
반응형
728x90

 

 

 

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class practice_everyday07 {
	public static void main(String[] args) throws IOException {
		
		// 문자 기반 스트림
		// FileReader
		// FileWriter
		
		FileWriter fw = new FileWriter("D:\\file\\file2\\first.txt");
		fw.write("initiate\n");
		fw.append("2\n");
		char[] cArray = {'c','h','e','e','r'};
		char[] sArray = {'u','p'};
		fw.write(cArray);
		fw.write(sArray);
		fw.close();
		
		FileReader fr = new FileReader("D:\\file\\file2\\first.txt");
		int i;
		while((i=fr.read()) != -1) {
			System.out.print((char)i);
		}
		fr.close();
		
	}	
}
728x90
반응형
728x90

 

File class의 많이 쓰는 메소드

f1.mkdir() : 해당 디렉토리 생성. 생성하려는 폴더 직전까지의 경로가 다 생성되어있어야 생성하려는 폴더 생성가능

경로 생성이 제대로 안되어있으면 생성이 안될 뿐 에러발생은 안난다

f1.mkdirs() : 해당 디렉토리 생성. 생성하려는 폴더 직전까지 없는 경로도 같이 생성

f1.creatNewFile()

f1.delete

f1.exists() : 존재하냐?

f1.isFile() : 파일이냐?

f1.getName() : 파일명 get

f1.getAbsolutePath() : 절대경로 반환

f1.getPath() : 상대경로 반환

f1.length() : 파일용량

f1.getParent() : 상위 폴더 경로 반환

 

 

 

 

import java.io.File;
import java.io.IOException;

public class practice_everyday06 {
	
	public static void main(String[] args) {
		
	File f = new File("D:\\file\\file1.txt");		// 경로지정 방법1
//	File f = new File("D:/file.txt");  		// 경로지정 방법2
//	File f = new File("D:/file.txt",true);  // ,true : 파일 내용 추가
		
		f.mkdir();  // 해당 디렉토리 생성. 생성하려는 폴더 직전까지의 경로가 다 생성되어있어야 생성하려는 폴더 생성가능
					// 경로 생성이 제대로 안되어있으면 생성이 안될 뿐 에러발생은 안난다
		f.mkdirs(); // 해당 디렉토리 생성. 생성하려는 폴더 직전까지 없는 경로도 같이 생성
		
		f.exists(); // true
		f.isFile(); // false
		System.out.println(f.exists());
		System.out.println(f.isFile());
		
	File ff = new File("D:\\file\\file2");
		System.out.println(ff.exists()); // false
		System.out.println(ff.isFile()); // false
		
		ff.mkdirs();	// 파일 생성 
		System.out.println(ff.exists()); // true
		System.out.println(ff.isFile()); // false
		System.out.println();
		
		System.out.println(ff.getName()); // file2
		System.out.println(ff.getAbsolutePath());  // D:\file\file2
		System.out.println(ff.getPath()); //상대경로 // D:\file\file2
		System.out.println(ff.length()); 		  // 0
		System.out.println(ff.getParent()); 	  // D:\file
		
		
		
		
		
		
		try {
			f.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		
		
	}	
}

 

 

728x90
반응형

+ Recent posts