]> www.vanbest.org Git - tv_grab_nl_java/commitdiff
REad and write config file
authorJan-Pascal van Best <janpascal@vanbest.org>
Sat, 10 Mar 2012 22:54:41 +0000 (23:54 +0100)
committerJan-Pascal van Best <janpascal@vanbest.org>
Sat, 10 Mar 2012 22:54:41 +0000 (23:54 +0100)
tv_grab_nl_java/src/org/vanbest/xmltv/Config.java
tv_grab_nl_java/src/org/vanbest/xmltv/Main.java

index b4c124d5e7f6ba2ecc289e4851ad6bf54e767849..87a69d9cfa1b52cac10ce494778e51b9cfba460f 100644 (file)
@@ -1,20 +1,33 @@
 package org.vanbest.xmltv;
 
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.commons.io.FileUtils;
 
 public class Config {
-       public String configFile;
        public List<Channel> channels;
        
        public Config() {
        }
        
-       public void writeConfig() {
-               
+       public void writeConfig(String filename) throws IOException {
+               FileUtils.forceMkdir(new File(filename).getParentFile());
+               PrintWriter out = new PrintWriter(new OutputStreamWriter( new FileOutputStream( filename )));
+               for(Channel c: channels) {
+                       out.println(c.id + ": " + c.name);
+               }
+               out.close();
        }
        
        public static File defaultConfigFile() {
@@ -25,8 +38,25 @@ public class Config {
                return readConfig(defaultConfigFile().getCanonicalPath());
        }
 
-       public static Config readConfig(String filename) {
+       public static Config readConfig(String filename) throws IOException {
                Config result = new Config();
+               BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( filename)));
+               List<Channel> channels = new ArrayList<Channel>();
+               while(true) {
+                       String s = reader.readLine();
+                       if(s==null) break;
+                       if (!s.contains(":")) continue;
+                       if (s.startsWith("#")) continue;
+                       String[] parts = s.split("[[:space:]]*:[[:space:]]*", 2);
+                       Channel c = new Channel(Integer.parseInt(parts[0]), parts[1], "");
+                       channels.add(c);
+               }
+               result.setChannels(channels);
                return result;
        }
+       
+       public void setChannels(List<Channel> channels) {
+               this.channels = channels;
+       }
+
 }
index 6b7262f391cba42adba87bbbc87cd6496dd77617..f54ed3b9e7ca8ab90f4b3dcac8ae01e0cf77137d 100644 (file)
@@ -60,6 +60,19 @@ public class Main {
                TvGids gids = new TvGids();
                
                List<Channel> channels = gids.getChannels();
+               System.out.println(channels);
+               
+               Config config = new Config();
+               config.setChannels(channels);
+               try {
+                       config.writeConfig(Config.defaultConfigFile().getCanonicalPath());
+               } catch (FileNotFoundException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               } catch (IOException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
                
                
        }