세상이야기

자바 파일 읽고/쓰기 처리 소스

쿠카곰돌이 2017. 2. 17. 18:27
반응형

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.FileInputStream;


import java.io.BufferedWriter;

import java.io.OutputStreamWriter;

import java.io.FileOutputStream;


import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;


public class Youtube

{

/**

* Keeps track of whether the first reading has been performed.

*/

protected boolean readFlag = false;



/*

* The main program, which can be executed from the command line

*/

public static void main(String [] args)

{

     int liLine = 0;

     int liDataIndex = 0;

     int liMode      = 0;

     ArrayList datalist = new ArrayList();

     

     BufferedReader br = null;


   String line;

   String title;

   String imgurl;

   String watch;

               

     // int chkInt =0;

     try {

          br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\htmlp\\eve.html"),"utf-8"));


          line = "";

          title = "";

          imgurl = "";

          watch = "";

          

          while ((line = br.readLine()) != null) {

              if(line.indexOf("yt-uix-sessionlink yt-uix-tile-link  spf-link  yt-ui-ellipsis yt-ui-ellipsis-2") > 0)

              {

              title = line.substring(line.indexOf("title=")+7,line.indexOf("aria-describedby")-3);

              watch = line.substring(line.indexOf("watch?v=")+8,line.indexOf(">김이브")-1);

                liMode = 1;

                // datalist.add(title);

              }

              

              if(line.indexOf("data-thumb=") > 0)

              {

              imgurl = line.substring(line.indexOf("data-thumb=")+12,line.indexOf(" src=")-1);

                System.out.println(title + "," + imgurl + "," + watch);

                liMode = 2;               

                datalist.add("'"+title+"', '"+watch+"', 'Y', '', '"+imgurl+"');");

              }

 

              // if(chkInt == 500) break; // 10 Line

              // chkInt ++;

          }

      } catch (IOException e) {

          e.printStackTrace();

      } finally {

          try {

              if (br != null) {

                  br.close();

              }

          } catch (IOException ex) {

              ex.printStackTrace();

          }

      }

      

      int seq = 1;

      try {

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("eveOut.txt"), "utf-8"));

            

            for(int i= (datalist.size()-1); i >= 0 ; i--) {

             String outStr = (String)datalist.get(i);

             writer.write("INSERT INTO tb_cast_tmp (cast_seq, cast_title, castid, use_yn, regdate, author_url) VALUES ("+seq+ ","+outStr );

                writer.write(seq +"," +outStr);

                writer.write("\r\n");

                seq ++;

            }

            writer.close();            

      } catch (IOException e) {

            e.printStackTrace();

      }

}

}

반응형