출처: https://hyeonstorage.tistory.com/250
"키보드로부터 한 줄을 입력받아 화면에 출력 하시오"
public static void main(String[] args) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inputStreamReader);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
import java.io.FileInputStream;
import java.io.IOException;
//file의 내용을 읽어 화면에 출력하는 프로그램
public class FileView {
public static void main(String[] args) {
if(args.length != 1) {
System.out.println("사용법 : java FileView 파일명");
}
//항상 null로 먼저 정의하고
FileInputStream fis = null;
try {
//try구문에서 입출력스트림 생성
fis = new FileInputStream(args[0]);
int i = 0;
while((i = fis.read()) != -1) {
System.out.write(i);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
//finally구문에서 스트림 close하기
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class StreamReaderTeste {
public static void main(String[] args) {
if(args.length != 1) {
System.out.println("사용법 : java StreamReaderTest 파일명");
System.exit(0);
}
FileInputStream fis = null;
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
fis = new FileInputStream(args[0]);
isr = new InputStreamReader(fis);
osw = new OutputStreamWriter(System.out);
char[] buffer = new char[512];
int readcount = 0;
while((readcount = isr.read(buffer)) != -1) {
osw.write(buffer,0,readcount);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fis.close();
isr.close();
osw.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
import java.io.FileReader;
import java.io.FileWriter;
public class Test2 {
public static void main(String[] args) throws Exception {
int n = 0;
FileReader fr = new FileReader("C:/samdata/테스트.txt");
FileWriter fw = new FileWriter("C:/samdata/테스트카피.txt", false);
while ((n = fr.read()) != -1) {
fw.write(n);
}
fr.close();
fw.close();
}
}
package nio;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class BookObjectOutputTest {
public static void main(String[] args) {
FileOutputStream fout = null;
ObjectOutputStream oos = null;
ArrayList<Book> list = new ArrayList<>();
Book b1 = new Book("a0001","자바완성","정프로",10000);
Book b2 = new Book("a0002","IO완성","정아마추어",20000);
Book b3 = new Book("a0003","NIO완성","정코딩",30000);
list.add(b1);
list.add(b2);
list.add(b3);
try {
fout = new FileOutputStream("booklist.dat");
oos = new ObjectOutputStream(fout);
oos.writeObject(list);
oos.reset();
System.out.println("저장완료");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
oos.close();
fout.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class Test02_2 {
public static void main(String[] args) throws Exception {
// 키보드에서 데이터를 읽는 스캐너 객체 준비
Scanner keyScanner = new Scanner(System.in);
// 1) 접속할 서버 주소 입력 받기
System.out.print("서버 주소: ");
String serverAddress = keyScanner.nextLine();
// 2) 접속할 서버의 포트 번호 입력 받기
System.out.print("포트 번호: ");
int port = Integer.parseInt(keyScanner.nextLine());
// 3) 사용자로부터 입력 받은 값을 가지고 서버에 접속한다.
Socket socket = new Socket(serverAddress, port);
System.out.println("=> 소켓 객체 생성 완료!");
// 4) 입출력할 스트림 객체 준비
InputStream in0 = socket.getInputStream();
OutputStream out0 = socket.getOutputStream();
// 5) 스트림 객체에 입출력 보조 객체를 연결한다.
Scanner in = new Scanner(in0);
PrintStream out = new PrintStream(out0);
System.out.println("=> 입출력 스트림 준비 완료!");
// 6) 키보드에서 서버에 보낼 메시지를 입력 받는다.
System.out.print("서버에 보낼 메시지: ");
String message = keyScanner.nextLine();
// 7) 키보드에서 입력 받은 메시지를 서버에 보낸다.
out.println(message);
System.out.println("=> 서버에 메시지 전송 완료!");
// 8) 서버가 보낸 메시지를 읽는다.
String response = in.nextLine();
System.out.println("=> 서버로부터 메시지 수신 완료!");
// 9) 서버가 보낸 메시지를 화면에 출력한다.
System.out.println(response);
in.close();
in0.close();
out.close();
out0.close();
socket.close();
keyScanner.close();
}
}