세상이야기

Java File Read/Write 예제 (파일 읽기/쓰기 예제 )소스

쿠카곰돌이 2017. 2. 17. 14:06
반응형



import java.io.BufferedReader;

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;

     

     ArrayList datalist = new ArrayList();

     

     BufferedReader br = null;

     

     int chkInt =0;

     try {

          br = new BufferedReader(new FileReader("D:\\htmlp\\eve.html"));

          String line;

          String title;

          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="),line.indexOf("aria-describedby"));

                datalist.add(title);

                System.out.println(title);

              }


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

              chkInt ++;

          }

      } catch (IOException e) {

          e.printStackTrace();

      } finally {

          try {

              if (br != null) {

                  br.close();

              }

          } catch (IOException ex) {

              ex.printStackTrace();

          }

      }

      

      

      try {

            FileWriter writer = new FileWriter("eveOut.txt", true);

            for(int i=0; i < datalist.size() ; i++) {

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

                writer.write(outStr);

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

            }

            writer.close();            

      } catch (IOException e) {

            e.printStackTrace();

      }

}

}

반응형