Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 객체지향
- 조건문
- Java
- 변수초기화
- 기초
- 동적 데이터
- 자바
- MAP
- IBatis CRUD
- 쓰레드
- 계산기
- Comparable
- iBatis
- oclick 동적
- I/O
- Thread
- 고급자바
- 변수선언
- 연산자
- 변수
- 동적 버튼 생성
- io
- 객체
- ListSort
- 동적 문자열
- IBatis 게시판
- comparator
- 코딩
- enum
- 동적 버튼 onclick
Archives
- Today
- Total
Jun's Blog
[Java] FileStream 파일 읽기&출력 예제 본문
package kr.or.ddit.basic;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* 파일 읽기 예제
*/
public class T05_FileStreamTest {
public static void main(String[] args) {
// FileInputStream 객체를 이용한 파일 내용 읽기
FileInputStream fis = null;
try {
// 방법1 (파일경로 정보를 문자열로 저장하기)
// fis = new FileInputStream("파일경로"); // 객체 생성
// 방법2 (파일정보를 File 객체를 이용하여 지정하기)
File file = new File("파일경로"); // 객체 생성
fis = new FileInputStream(file);
int c; // 읽어온 데이터를 저장할 변수
while((c=fis.read()) != -1) { // -1이 아니면 뭔가를 읽었다.
// 읽어온 자료 출력하기
System.out.print((char) c);
}
fis.close();
} catch(FileNotFoundException e) {
System.out.println("지정된 파일이 없습니다.");
} catch(IOException e) {
System.out.println("알 수 없는 입출력 오류입니다");
}
}
}
package kr.or.ddit.basic;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 파일 출력 예제
*/
public class T06_FileStreamTest {
public static void main(String[] args) {
// 파일에 출력하기
FileOutputStream fos = null;
try {
// 출력을 OutputStream 객체 생성
fos = new FileOutputStream("파일경로");
for(char ch='a'; ch<='z'; ch++) {
fos.write(ch);
}
System.out.println("파일에 쓰기 작업 완료...");
//쓰기 작업 완료 후 스트림 닫기
fos.close();
//==============================================
//저장된 파일의 내용을 읽어 화면에 출력하기
FileInputStream fis = new FileInputStream("파일경로");
int c;
while((c=fis.read()) != -1) {
System.out.println((char) c);
}
System.out.println();
System.out.println("출력 끝...");
fis.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
'High Java > IO' 카테고리의 다른 글
[Java] byteArrayIO 예제 (0) | 2020.10.23 |
---|---|
[Java] File 활용한 예제 (0) | 2020.10.22 |
Comments