본문 바로가기

Programming/JAVA

JAVA 파일 입출력 예제

import java.io.*;
import java.util.*;

class Person1{
	String id;
	String passwd;
	Person1(String id, String passwd){
		this.id = id;
		this.passwd = passwd;
	}
}

public class MyFileReaderWriter {
	public static void main(String[] args){
		Vector personList = new Vector();
		String inputFile = "src/password.txt";
		String outputFile = "src/password.txt";
		try{
			FileReader fr = new FileReader(inputFile);

			BufferedReader br = new BufferedReader(fr);

			String data;

			while((data=br.readLine()) != null){
				String[] personData = data.split(":");
				personList.add(new Person1(personData[0],personData[1]));
			}
			fr.close();

			FileWriter fw = new FileWriter(outputFile);
			PrintWriter pw = new PrintWriter(fw);

			personList.add(new Person1("dongyang","11112"));

			for(int i=0; i < personList.size(); i++){
				Person1 p = personList.get(i);
				String str = p.id+":"+p.passwd;
				pw.println(str);
				
				System.out.println(p.id + p.passwd);
			}
			fw.flush();
			fw.close();
		}catch(FileNotFoundException fnfe){ System.err.println("파일이 없습니다."); 
		}catch(IOException ioe){System.err.println("IO예외가 발생했습니다.");
		}
	}
}

'Programming > JAVA' 카테고리의 다른 글

[JAVA]키보드 입력 Scanner  (2) 2014.09.12
JAVA 랜덤 함수 Math.random()  (0) 2014.05.26
java 문자를 아스키로 변환  (0) 2014.05.19
java C클래스 서브넷 계산기  (0) 2014.05.19