Results 1 to 6 of 6
Thread: Reading files and splitting them
- 09-29-2009, 02:50 PM #1
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...
Now let's start writing it into a file...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 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.*; 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"); } } } }
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,
DieterProgramming 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...
- 09-29-2009, 03:12 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 09-29-2009, 03:42 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You'll also need to deal with the header you've put into the file.
- 09-29-2009, 09:36 PM #4
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.
isnt there a way to skip the header part? else i just trow the header part away, it was just for the show :pYou'll also need to deal with the header you've put into the file.
all the best,
DieterLast 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...
- 09-30-2009, 09:26 AM #5
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 :)
All the best,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()); } } } }
DieterProgramming 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...
- 09-30-2009, 09:46 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
reading Excel Files
By wdb07159 in forum New To JavaReplies: 2Last Post: 05-06-2009, 12:45 PM -
Reading in a line of data and splitting the line up into variables
By guru32 in forum New To JavaReplies: 9Last Post: 04-07-2009, 03:51 AM -
Reading In Text Files
By Dukey in forum New To JavaReplies: 4Last Post: 04-04-2009, 11:53 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks