01. Java (v)
02. git
03. Database
04. Jsp [Server]
05. 미니프로젝트 (3W)
06. HTML,CSS
07. JS
08. SpringFramework , SrpingBoot
09. React JS [Front-end]
10. 중간프로젝트 (1M)
11. Linux 명령어
12. AWS 클라우드
13. DevOps - Docker
14. App - Android
15. 최종프로젝트 (1M)
폴더를 생성할 때는 File클래스 사용
생성자 매개변수로 폴더를 생성할 경로 + 폴더명
을 지정
String path = "C:\\Users\\user\\Desktop\\file\\myfolder";
try {
File file = new File(path);
if( !file.exists() ) { //파일이 존재하면 true
file.mkdir(); //폴더 생성
System.out.println("폴더가 생성 되었습니다");
} else {
file.delete(); //폴더 삭제(파일삭제)
System.out.println("폴더가 이미 있습니다");
}
} catch (Exception e) {
e.printStackTrace();
}
텍스트 파일을 프로그램으로 읽을 때 사용하는 문자기반 스트림
데이터를 읽고 버퍼에 저장한 후 한번에 쓰는 형태로 사용하기 때문에 속도가 빠름
Scanner scan = new Scanner(System.in);
String path = "C:\\Users\\Windows\\Desktop\\file\\test02.txt";
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(path, true) ); //true를 주면 기존 파일이 있을경우, 내용을 이어서 작성하게 됩니다.
while(true) {
System.out.print(">");
String str = scan.nextLine();
if(str.equals("exit")) {
break;
}
str += "\r\n"; //줄바꿈
bw.write(str);
bw.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (Exception e2) {
}
}
텍스트 파일을 프로그램으로 읽을 때 사용하는 문자 기반 스트림
데이터를 읽고 버퍼에 저장한 후 한번에 읽는 형태로 사용되기 때문에 속도가 빠름
String path = "C:\\Users\\Windows\\Desktop\\file\\test02.txt";
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(path));
//System.out.println( br.readLine() ); //더이상 읽을 데이터가 없으면 null
String str;
while( (str = br.readLine() ) != null) {
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (Exception e2) {
}
}
String path = "C:\\Users\\Windows\\Desktop\\file\\test01.txt";
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(path));
int result;
while( (result = bis.read()) != -1) {
System.out.print( (char)result );
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bis.close();
} catch (Exception e2) {
}
}
outputstream의 성능향상 스트림
String path = "C:\\Users\\Windows\\Desktop\\file\\test01.txt";
//OutputStream os = null;
BufferedOutputStream bos = null;
try {
//os = new FileOutputStream(path);
bos = new BufferedOutputStream( new FileOutputStream(path) );
String str = "youjin choi? good morning?";
bos.write( str.getBytes() ); //파일을 씀
bos.flush(); //버퍼를 밀어냄 (데이터가 전달됨)
//Thread.sleep(20000); //20초 스탑
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (Exception e2) {
}
}
System.out.println("프로그램 정상 종료");
//프로그램이 종료되면 버퍼가 자동으로 비워짐.
구현 클래스가 매번 달라지거나, 한번만 사용되는 경우, 굳이 구현 클래스를 생성하지 않고 익명클래스로 선언할 수 있음
interface Car{
public void run();
}
public class Garage{
public Car car = new Car(){
@Override
public void run(){
System.out.println("달립니다.");
}
}
}
함수적 프로그래밍
Y = f(x) 형태의 함수로 구성된 프로그래밍 기법
함수를 매개 값으로 전달하고 결과를 받는 코드로 구성
함수적 프로그래밍이 객체 지향 프로그래밍 보다는 효율적인 경우
람다식의 장점
Person pp = new Person();
pp.greeting(()->{
System.out.println("안녕");
})
pp.greeting(word->System.out.println(word));