/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modelnya;

import java.io.*;

/**
 *
 * @author Guest
 */
public class getFile {
    public static String[] readFile() throws FileNotFoundException, IOException{
        File file=new File("schedule.txt");
        FileInputStream fstream=new FileInputStream(file);
        DataInputStream in=new DataInputStream(fstream);
        BufferedReader br=new BufferedReader(new InputStreamReader(in));

        String [] str= new String[countLine()];        
        int k=0;
        String text=null;
        while ((text=br.readLine()) != null){
            str[k]=text;
            k++;
        }
        in.close();
        return str;
    }

    public static void writeFile() throws IOException {
        Writer output=null;
        File file=new File("schedule.txt");
        String text="mboh kah.. cuma nyoba";
        FileWriter in=new FileWriter(file);
        output=new BufferedWriter(in);
        output.write(text+System.getProperty("line.separator"));
        output.close();
        System.out.println("tulis baru berhasil");
    }

    public static void appendFile(String date, String start, String end, String subject, String client, String place, String event) throws IOException {
        String id=Integer.toString(getlastID()+1);
        File file=new File("schedule.txt");
        //String text="bisa nambah ga ya??";
        String text=id+";"+date+";"+start+";"+end+";"+subject+";"+client+";"+place+";"+event+";";
        FileWriter in=new FileWriter(file, true);
        in.write(text+System.getProperty("line.separator"));
        in.flush();
        in.close();        
    }

    public static void splitFile() throws FileNotFoundException, IOException{
        File file=new File("schedule.txt");
        FileInputStream fstream=new FileInputStream(file);
        DataInputStream in=new DataInputStream(fstream);
        BufferedReader br=new BufferedReader(new InputStreamReader(in));
        String str;
        while ((str=br.readLine()) != null){
            String[] record=null;
            record=str.split(";");
            for (int i = 0; i < record.length; i++) {
                System.out.println(record[i]);
            }
        }
        in.close();
    }

    public static void searchFileID() throws FileNotFoundException, IOException {
        String noID="3";
        File file=new File("schedule.txt");
        FileInputStream fstream=new FileInputStream(file);
        DataInputStream in=new DataInputStream(fstream);
        BufferedReader br=new BufferedReader(new InputStreamReader(in));
        String str;
        while ((str=br.readLine()) != null){
            String[] id=null;
            id=str.split(";");            
            if (id[0].equals(noID)){
                System.out.println(str);
            }
        }        
        in.close();
    }

    public static String[] searchFilebyDate(String date) throws FileNotFoundException, IOException {
        File file=new File("schedule.txt");
        FileInputStream fstream=new FileInputStream(file);
        DataInputStream in=new DataInputStream(fstream);
        BufferedReader br=new BufferedReader(new InputStreamReader(in));

        String[] str=new String[countLinebyDate(date)];
        int k=0;
        String text;
        while ((text=br.readLine()) != null){
            String[] rec=null;
            rec=text.split(";");
            if (rec[1].equals(date)){
                str[k]=text;
                k++;                
            }
        }
        in.close();
        return str;
    }

    public static void removeLineFromFile(String idToRemove) {
        String file="schedule.txt";
        try {
          File inFile = new File(file);
          if (!inFile.isFile()) {
            System.out.println("Parameter is not an existing file");
            return;
          }

          //Construct the new file that will later be renamed to the original filename.
          File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
          BufferedReader br = new BufferedReader(new FileReader(file));
          PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
          String line = null;
          //Read from the original file and write to the new
          //unless content matches data to be removed.
          while ((line = br.readLine()) != null) {
            String[] id=null;
            id=line.split(";");
            if (!id[0].equals(idToRemove)){
              pw.println(line);
              pw.flush();
            }                        
          }
          pw.close();
          br.close();

          //Delete the original file
          if (!inFile.delete()) {
            System.out.println("Could not delete file");
            return;
          }

          //Rename the new file to the filename the original file had.
          if (!tempFile.renameTo(inFile)) {
                System.out.println("Could not rename file");
            }

        }
        catch (FileNotFoundException ex) {
          ex.printStackTrace();
        }
        catch (IOException ex) {
          ex.printStackTrace();
        }
    }

    public static void updateFile(String id, String date, String start, String end, String subject, String client, String place, String event) throws IOException{
        File file=new File("schedule.txt");        
        String text=id+";"+date+";"+start+";"+end+";"+subject+";"+client+";"+place+";"+event+";";
        FileWriter in=new FileWriter(file, true);
        in.write(text+System.getProperty("line.separator"));
        in.flush();
        in.close();
    }

    public static String[] dump(String data) {
        String[] record=null;
        record=data.split(";");
        return record;
        /*String id=record[0];
        String date=record[1];
        String start=record[2];
        String end=record[3];
        String subject=record[4];
        String client=record[5];
        String place=record[6];
        String event=record[7];
        try {
            appendFile(id,date,start,end,subject,client,place,event);
        } catch (IOException ex) {
            Logger.getLogger(getFile.class.getName()).log(Level.SEVERE, null, ex);
        }*/
    }

    public static int countLine() throws FileNotFoundException, IOException{
        File file=new File("schedule.txt");
        FileInputStream fstream=new FileInputStream(file);
        DataInputStream in=new DataInputStream(fstream);
        BufferedReader br=new BufferedReader(new InputStreamReader(in));
        int i=0;
        while (br.readLine() != null){
            i++;
        }
        in.close();
        return i;
    }

    public static int countLinebyDate(String date) throws FileNotFoundException, IOException{
        File file=new File("schedule.txt");
        FileInputStream fstream=new FileInputStream(file);
        DataInputStream in=new DataInputStream(fstream);
        BufferedReader br=new BufferedReader(new InputStreamReader(in));
        String text;
        int i=0;
        while ((text=br.readLine()) != null){
            String[] rec=null;
            rec=text.split(";");
            if (rec[1].equals(date)){
                i++;
            }
        }
        in.close();
        return i;
    }

    private static int getlastID() throws FileNotFoundException, IOException{
        File file=new File("schedule.txt");
        FileInputStream fstream=new FileInputStream(file);
        DataInputStream in=new DataInputStream(fstream);
        BufferedReader br=new BufferedReader(new InputStreamReader(in));
        String str=null;
        String strID=null;
        while ((str=br.readLine()) != null){
            String[] id=null;
            id=str.split(";");
            strID=id[0];
        }
        int lastID=Integer.parseInt(strID);
        in.close();
        return lastID;
    }
}























