T.김동식
예외처리 & 예외발생
public static void main(String[] args) throws Exception{}
throw new Exception();
입출력기능을 추상화
자바에서 입출력기능을 스트림클래스로 제공
C언어가 조작하는 자바에서 입출력장치를 조작하기위해, 입출력기능을 추상화, 클래스로 제공
하드웨어(입출력장치)가 고장이 나더라도 상관없이 장치에 독립적으로 프로그래밍/구현할 수 있다.
inputstreamreader
바이트로 읽어들인 자료를 문자로 변환해주는 스트림
보조스트림
다른스트림을 감싸서 부가 기능을 제공하는 스트림
직렬화
인스턴스 내용을 그대로 저장하거나 네트워크로 전송할 수 있도록 정렬하는것.
serialization, externalization
기술을 구현하기 위해서 자바에서 사용되는 두가지 인터페이스
a.txt파일에 다음처럼 출력해보세요.
지금까지 자바 정말 재미있게 공부했어요^^
//내 문제풀이 1
try(FileWriter fw = new FileWriter("src/chapter15/a.txt")) {
fw.write("1. 지금까지 자바 정말 재미있게 공부했어요^^");
} catch (Exception e) {}
//내 문제풀이 2
//OutputStream에 속한 flush()는
//Flushes this output stream and forces any buffered output bytes to be written out.
//출력 스트림과 버퍼된 출력 바이트를 강제로 쓰게 한다.
try(FileOutputStream fos = new FileOutputStream("src/chapter15/a.txt")) {
Writer wr = new OutputStreamWriter(fos);
wr.write("2. 지금까지 자바 정말 재미있게 공부했어요^^");
wr.flush();
} catch (Exception e) {}
//선생님 풀이
try (FileOutputStream fos = new FileOutputStream("src/chapter15/b.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos)){
String data = "3. 지금까지 자바 정말 재미있게 공부했어요^^";
osw.write(data);
} catch (Exception e) {}
Servlet
html 세팅
<form name="form" action="login" method="post">
아이디: <input type="text" name="user_id"><br>
비밀번호: <input type="password" name="user_pw"><br>
<input type="submit" value="로그인">
<input type="reset" value="초기화">
<input type="hidden" name="address" value="모란역" id="">
<input type="hidden" name="email" value="sh@sanghee.com" id="">
<input type="hidden" name="hp" value="010-2574-2411" id="">
</form>
인코딩 및 변수설정, login/second 공통설정사항
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
PrintWriter pw = response.getWriter();
String user_id = request.getParameter("user_id");
String user_pw = request.getParameter("user_pw");
String address = request.getParameter("address");
String email = request.getParameter("email");
String hp = request.getParameter("hp");
submit 후 이동될 /login
String data = "";
data +="<html><body>";
data +=user_id+"<br>";
data +=user_pw+"<br>";
data +=address+"<br>";
data +=email+"<br>";
data +=hp+"<br>";
data += "</body></html>";
pw.print(data);
pw.print("<a href='/pro09/second?user_id="+user_id
+"&user_pw="+user_pw
+"&address="+address
+"&email="+email
+"&hp="+hp
+"'>이동</a>");
href로 이동될 /second
pw.print("<html><body>");
if(user_id !=null && user_id.length() !=0) {
pw.print("로그인한 내역입니다."+"<br>");
pw.print(user_id+"<br>");
pw.print(user_pw+"<br>");
pw.print(address+"<br>");
pw.print(email+"<br>");
pw.print(hp);
}else {pw.print("로그인하지 않으셨습니다.");}
쿠키세팅 /set
response.setContentType("text/html; charset=utf-8");
PrintWriter pw = response.getWriter();
//Date와 컴퓨터가 이해할 수 있도록 URLEncoder
Date date = new Date();
Cookie c=new Cookie("cookieTest",URLEncoder.encode("jsp 프로그램입니다.","utf-8"));
c.setMaxAge(24*60*60);//초단위, 하루를 세팅
response.addCookie(c);
pw.print(date);
pw.print(c);
쿠키출력 /get
response.setContentType("text/html; charset=utf-8");
PrintWriter pw = response.getWriter();
Cookie[] cookieArr = request.getCookies();
pw.print(URLDecoder.decode(i.getValue())); //모든 쿠키 출력
for(Cookie i:cookieArr) {
if(i.getName().equals("cookieTest")) { //추가한 cookieTest 쿠키의 value만을 출력
System.out.println(i.getValue());
pw.print(URLDecoder.decode(i.getValue()));
}}
Android
버튼이동 xml & java
<Button
android:id="@+id/link1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="네이버" />
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://papago.naver.com/"));
startActivity(browserIntent);
}
});
계산기구현 xml & java
<EditText
android:id="@+id/Edit1"
android:layout_weight="1"
android:layout_width="163dp"
android:layout_height="62dp"
android:layout_margin="20dp"
android:hint="숫자1">
</EditText>
<EditText
android:id="@+id/Edit2"
android:layout_weight="1"
android:layout_width="168dp"
android:layout_height="69dp"
android:layout_margin="20dp"
android:hint="숫자2">
</EditText>
<Button
android:id="@+id/plus"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="더하기"
></Button>
<Button
android:id="@+id/minus"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="빼기"
></Button>
<Button
android:id="@+id/multiply"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="곱하기"
></Button>
<Button
android:id="@+id/divide"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="나누기">
</Button>
<TextView
android:id="@+id/TextResult"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="계산 결과 : "
android:textColor="#FF0000"
android:textSize="30dp" />
public class MainActivity extends AppCompatActivity {
Button btn1;
Button btn2;
Button btn3;
Button btn4;
EditText num1;
EditText num2;
TextView txtResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
setTitle("4개의 버튼");
Button btn1=findViewById(R.id.plus);
num1 =findViewById(R.id.Edit1);
num2 =findViewById(R.id.Edit2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String number1=num1.getText().toString();
String number2=num2.getText().toString();
int result= Integer.parseInt(number1) + Integer.parseInt(number2);
txtResult =findViewById(R.id.TextResult);
txtResult.setText("계산결과 : " + result );
}
});
}