import java.io.*;

class database extends application{
    
    public int get_total_lines(String filename) throws IOException{
        int r = 0;
        try {
            BufferedReader in = new BufferedReader(new FileReader(filename));		
            r = 0;
		
            while (in.readLine() != null)
                r++;
        }
        catch (FileNotFoundException exception) {
            System.err.println("The required file "+filename+" was not found, new file created.");
            this.add_line(filename, null, false);
            return 0;
        }
        return r;
    }
	
    public String get_line(String filename, int line_number) throws IOException{
        String line = new String();

        try {
            BufferedReader in = new BufferedReader(new FileReader(filename));			
            for (int r = 0; r < line_number; r++)
                in.readLine();
		
            line = in.readLine();	
            in.close();
	}
	catch (FileNotFoundException exception){
            System.err.println("The required file "+filename+" was not found, new file created.");
            this.add_line(filename, null, false);
            return null;
	}
	
	return line;
    }
    
    public void add_line(String file_name, String line, boolean append) throws IOException{
        try{
            BufferedWriter out = new BufferedWriter(new FileWriter(file_name, append));
            if (append){
                out.flush();
                out.newLine();
            }
            if (line != null)
                out.write(line);
            out.close();
        }
        catch (IOException exception){
            System.err.println("Error writing the file: "+file_name+".");
        }
    }
	
    public String add_quotes(String s){
        s = s.replaceAll("\"", "\"\"");
	return s;
    }
	
    public String remove_quotes(String s){
        s = s.replaceAll("\"\"", "\"");
        return s;
    }
    
    public void copyFile(String filename) throws Exception{
        File in = new File(filename);
        
        if (filename.compareTo(conf.RootDirectory+in.getName()) != 0){
            File out = new File(conf.RootDirectory+in.getName()); 
        
            FileInputStream fis  = new FileInputStream(in);
            FileOutputStream fos = new FileOutputStream(out);
        
            byte[] buf = new byte[1024];
            int i = 0;
        
            while((i=fis.read(buf))!=-1)
                fos.write(buf, 0, i);
        
            fis.close();
            fos.close();
        }
    }
}