1. 기록할 때

 

import java.io.*; 

import java.text.SimpleDateFormat; 

import java.util.*; 

 

public class writeOut {

     public void wf() {

        GregorianCalendar gcA = new GregorianCalendar();

        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd-HHmmss");

        Date dy = gcA.getTime(); // Date -> util 패키지

        String strT = sf.format(dy);

        String id = request.getParameter(id);

        Random rand = new Random();

        int rand0 = rand.nextInt(10);

        int rand1 = rand.nextInt(10);

        File file = new File("경로/record/[" + strT + "_" + rand0 + rand1 + "]" + aoc + "__" + id + ".txt");

 

        try {

           BufferedWriter out = new BufferedWriter(new FileWriter(file));

           Scanner inputS = new Scanner(System in);

           String text00 = inputS.next();

           out.write(text00 );

           out.newLine();

           out.close();

           } catch (FileNotFoundException e) {

               e.printStackTrace();

           }

     }

}  

 

 

 

2. 읽어올 때

 

public class readIn {

     public void rf() {

        String 파일명 = request.getParameter("선택된 파일명");

        String id = request.getParameter(id);

        String id1 = 파일명.substring("__"+1);

        String id2 = id + ".txt";

        String ReadF = "경로/record/" + 파일명;

        String ReadW = "";

        Scanner in = null;

        try{

            if(id2 == id1){

                in = new Scanner(new File(ReadF));

 

                while(in.hasNext()){

                    ReadW = in.nextLine();

                    out.println(ReadW);

                }

            }else{

                out.println("파일이 없습니다.");

            }

        }catch(FileNotFoundException e){

            e.printStackTrace();

        }

    }