Java: Split Strings

By Eric Downing. Filed in Uncategorized  |  
TOP del.icio.us digg

I was using Java to add users to a system by parsing a comma seperated value (CSV) file of user entries and I needed to split the file to gather the separate entries. There is also the StringTokenizer class that can seperate the entries as well.

So the following code snippet was used:

String fileline;

FileInputStream fis = new FileInputStream("filename.txt");
DataInputStream dis = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));

while((fileline = br.readline()) != null) {
  String[] entries = fileline.split(",");

  for(int i=0;i < entries.length;i++)
  {
    System.out.println(entries[i]);
  }
}

Leave a Reply