본문 바로가기
웹 풀스택 스터디(뉴렉처-학원)--> 이거 꼭 보세요

7. 자바 기본 문법

by 백엔드개발자0107 2021. 8. 6.

1. if 와 else 에 대해서 공부해보자

 

if는 조건문 else는 if의 조건에 배타적인 구문이다.

package res;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class hw {

	public static void main(String[] args) throws IOException {

		File data3 = new File("res/data-copy");
		
		FileInputStream fis = new FileInputStream(data3);
		
		File data4 = new File("res/data-copy2");
		
		FileOutputStream fos = new FileOutputStream(data4);
		
		int x; 
		
		while(true) {
			
			x = fis.read();
			
			if(x<0)
				break;
			
			fos.write(x);
			
			if(x==' ')
				fos.write('\n');
			
		}
		
		
		
		
	}

}

 

이런식으로 파일을 읽어와서 각 값을 수평으로 출력되는 숫자를 수직이 되도록 복사하게 할수 있다.

 

 

if(x=='0')
			fos.write('?');
		else
			fos.write(x);

자 , 이 자바 구문을 해석해보자,

 

위 구문의 결괏값은 0은 ?이걸로 바뀌고

 

0이 아닌값은 그대로 x의 값들을 저장할것이다.

 

 

package res;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

public class hw {

	public static void main(String[] args) throws IOException {

		File data3 = new File("res/data-copy");
		
		FileInputStream fis = new FileInputStream(data3);
		
		File data4 = new File("res/data-copy2");
		
		FileOutputStream fos = new FileOutputStream(data4);
		
		Scanner fscan = new Scanner(fis);
		PrintStream fout = new PrintStream(fos);
		
		
		int sum = 0;
		boolean done = false;
		
		
		
		while(!done) {
			
			
			
		}
		
		
		fscan.close();
		fout.close();
		fis.close();
		fos.close();
		
		System.out.println("작업완료 ");
		
}
}

 

PrintStream  fout = new PrintStream(fos);

는 파일값 fos 를 문자열 단위로 출력할수 있게끔한다.

 

나는 항상 궁금했다.. while문에 대해서 말이다. while문은 괄호안의 값들을 break가 없으면 무한대로 실행되는 것인데,(단 조건문이 true일 경우에만) 일떄이다.