Results 1 to 6 of 6
  1. #1
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default Reading files and splitting them

    hey hey,

    i'm stuck at splitting my input i get from the FileReader.
    For school i had to make an app where i write and array into a file using the FileWriter.

    all cool and easy..but then i need to get that file, split it and make an object from it.

    i tried several ways and i know i need to use an array to split it but i have no idea how to get it to the output.

    i did API research and google research but i don't understand it.

    Could you take a look at my code?


    Making a class of customers...

    Java Code:
    public class Klant implements Comparable <Klant> {
    
        private int klantenNr;
        private String naam;
        private String achternaam;
        private String straat;
        private int nr;
        private String gemeente;
        private int Postcode;
    
        public Klant(int klantenNr, String naam, String achternaam, String straat, int nr, String gemeente, int Postcode) {
            this.klantenNr = klantenNr;
            this.naam = naam;
            this.achternaam = achternaam;
            this.straat = straat;
            this.nr = nr;
            this.gemeente = gemeente;
            this.Postcode = Postcode;
        }
    
        public int getPostcode() {
            return Postcode;
        }
    
        public void setPostcode(int Postcode) {
            this.Postcode = Postcode;
        }
    
        public String getAchternaam() {
            return achternaam;
        }
    
        public void setAchternaam(String achternaam) {
            this.achternaam = achternaam;
        }
    
        public String getGemeente() {
            return gemeente;
        }
    
        public void setGemeente(String gemeente) {
            this.gemeente = gemeente;
        }
    
        public int getKlantenNr() {
            return klantenNr;
        }
    
        public void setKlantenNr(int klantenNr) {
            this.klantenNr = klantenNr;
        }
    
        public String getNaam() {
            return naam;
        }
    
        public void setNaam(String naam) {
            this.naam = naam;
        }
    
        public int getNr() {
            return nr;
        }
    
        public void setNr(int nr) {
            this.nr = nr;
        }
    
        public String getStraat() {
            return straat;
        }
    
        public void setStraat(String straat) {
            this.straat = straat;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Klant other = (Klant) obj;
            if (this.klantenNr != other.klantenNr) {
                return false;
            }
            return true;
        }
    
        @Override
        public int hashCode() {
            return (int) this.klantenNr;
        }
    
    @Override
        public String toString() {
            return klantenNr + ":" + naam + ":" + achternaam
                             + ":" + straat + ":" + nr + ":"
                             + gemeente + ":" + Postcode ;
        }
    
        public int compareTo(Klant o) {
            return this.achternaam.compareTo(o.achternaam);
        }
    
    }
    Now let's start writing it into a file...

    Java Code:
    import java.io.*;
    import java.io.FileWriter;
    import java.util.*;
    
    /**
     *
     * @author Dieter
     */
    public class KlantenApp {
    
              FileWriter file = null;
    
             Set<Klant> klantenBestand = new TreeSet<Klant>();
                 klantenBestand.add(new Klant (1234, "Dieter", "De Wreede", "Henisstraat", 13, "Tongeren", 3700));
                 klantenBestand.add(new Klant (5678, "Sarah", "Welkenhuyzen", "Bunzingpad", 1, "Lanaken", 3850));
                 klantenBestand.add(new Klant (9101, "Davy", "De Wreede", "Henisstraat", 13, "Tongeren", 3700));
                 klantenBestand.add(new Klant (1121, "Meneer", "Koekenbak", "Roomboterlaan", 180, "Graden", 1337));
                 klantenBestand.add(new Klant (3141, "Mevrouw", "Klets", "Koffiewal", 2, "Roddeltopia", 8000));
           try{
                File fileName = new File ("c:\\CrapMap\\KlantenBestand.txt");
                  file = new FileWriter(fileName, true);
                file.write("Welkom in het klantenBestand!"
                                + "\n"
                                + "==============================="
                                + "\n"
                                + "Totaal overzicht van de klanten: "
                                + "Current count = " + klantenBestand.size()
                                + "\n\n");
               
                Iterator i1 = klantenBestand.iterator();
                while(i1.hasNext()){
                    file.write(i1.next() + "\n" );
                             
                }
           }
            
           catch(IOException ex){
               System.out.println("oops, something went wrong!");
               System.out.println(ex.getMessage());
           }
           finally{
               try{
                   if(file != null) file.close();
               }
               catch(IOException ex){
                   System.out.println("fault");
               }
           }
        }
    }
    now i created it, i want to read it and split it, notice i can read from it allready but i don't know how to split it, i tried stuff but don't know how to do it anymore...

    Java Code:
    import java.io.*;
    /**
     *
     * @author Dieter
     */
    public class ReadKlanten {
        public static void main(String[] args) {
             FileReader file = null;
    
            try{
                int character;
                File fileName= new File("c:\\CrapMap\\KlantenBestand.txt");
                file = new FileReader(fileName);
                while((character = file.read()) != -1){
                    System.out.print((char)character);
                }
                System.out.println("");
            }
            catch(IOException ex){
               System.out.println("oops, something went wrong!");
               System.out.println(ex.getMessage());
           }
           finally{
               try{
                   if(file != null) file.close();
               }
               catch(IOException ex){
                   System.out.println("fault");
               }
        }
        }
    }

    hope you guys can help me figure this out cause i don't know how to do it.

    Thanks in advence,

    Dieter
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Wrap the FileReader with a BufferedReader and use the readLine() method to read a line from the file.
    Then use String.split(",") to split the line into individual values again.
    You'd then have a problem if your values contain a comma themselves.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,010
    Rep Power
    20

    Default

    You'll also need to deal with the header you've put into the file.

  4. #4
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Wrap the FileReader with a BufferedReader and use the readLine() method to read a line from the file.
    Then use String.split(",") to split the line into individual values again.
    You'd then have a problem if your values contain a comma themselves.

    thx for your quick reply mate :)
    i will give it a try, i allready added ":" to the toString method for splitting them.
    was thinking about calculating the amount of ":" and then write it to output. don't know if that will work.

    You'll also need to deal with the header you've put into the file.
    isnt there a way to skip the header part? else i just trow the header part away, it was just for the show :p

    all the best,

    Dieter
    Last edited by Dieter; 09-29-2009 at 09:43 PM.
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  5. #5
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    i think i got it now, not sure tho :p

    only thing is the header. Just need to make some changes :)

    thx for helping me clear this out :)

    Java Code:
    package FileSchrijver;
    
    import java.io.*;
    import java.util.*;
    
    public class LezenKlant {
    	public static void main(String[] args) throws IOException{
    		
    	Collection<Klant> hS= new TreeSet<Klant>();
        LineNumberReader lineNumberReader=null;
        File file=new File("c:\\CrapMap\\KlantenBestand.txt");
        String text;
        String[] geg = new String[7];
       
       try {
           lineNumberReader=new LineNumberReader(new FileReader(file));            
           while((text=lineNumberReader.readLine())!=null){
               geg=text.split(" ");
           hS.add(new Klant(Integer.parseInt(geg[0]), geg[1], geg[2], geg[3], Integer.parseInt(geg[5]), geg[4], Integer.parseInt(geg[6])));            
           }
           Iterator<Klant> i=hS.iterator();
           while(i.hasNext()){
               System.out.println(i.next());
           }
       }
       catch (IOException exception) {
           System.out.println("foutje");
           System.out.println(exception.getMessage());
       }
       finally{
           try {
               if(lineNumberReader!=null){
                   lineNumberReader.close();
               }
           }
           catch (Exception exception) {
               System.out.println("foutje1");
               System.out.println(exception.getMessage());
           }
       }
    	
      }
    }
    All the best,

    Dieter
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,010
    Rep Power
    20

    Default

    Stick some sort of marker after the header, in the line before the data starts.
    Then you can simply read until you hit that point.

Similar Threads

  1. reading Excel Files
    By wdb07159 in forum New To Java
    Replies: 2
    Last Post: 05-06-2009, 12:45 PM
  2. Replies: 9
    Last Post: 04-07-2009, 03:51 AM
  3. Reading In Text Files
    By Dukey in forum New To Java
    Replies: 4
    Last Post: 04-04-2009, 11:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •