title: Java: Detect the delimiter of a csv or txt file
body: <p>I saw that this question was already asked several times but they're on other language and I can't get a grasp on the answers.</p>
  
  <p>I am receiving a .csv or .txt file through a socket.
  Is there any way I can detect the delimiter or "splitter" of a line in the CSV or TXT file?</p>
  
  <p>This is the server code that handles the file writing,</p>
  
  <pre><code>try{
  final ServerSocket server = new ServerSocket(8998);
  socket = server.accept();
  File sdcard = Environment.getExternalStorageDirectory();
  File myFile = new File(sdcard,"TestReceived"+curDate+".csv");
  
  final BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  final PrintWriter pw = new PrintWriter(new FileWriter(myFile));
  
   String line;
   String[] wordsarray;
  
   int bc = 0;
   int dc = 0;
   int pq = 0;
   int rq = 0;
   int id = 0;
  
   line = br.readLine();
   wordsarray = line.split(",");
  
   for (int x = 0; x&lt;wordsarray.length; x++){
  
   switch(wordsarray[x]){
        case "COLUMN NAME A": id = x;
        break;
        case "COLUMN NAME B": bc = x;
        break;
        case "COLUMN NAME C": dc = x;
        break;
        case "COLUMN NAME D": pq = x;
        break;
        case "COLUMN NAME E": rq = x; 
        break;
        }              
    }
           pw.println(wordsarray[dc]+"\t"+wordsarray[rq]+"\t"+wordsarray[pq]+"\t"+wordsarray[bc]+"\t"+wordsarray[id]);
  
  
           for (line = br.readLine(); line != null; line = br.readLine()) {
                  wordsarray = line.split(",");
                  pw.println(wordsarray[dc]+"\t"+wordsarray[rq]+"\t"+wordsarray[pq]+"\t"+wordsarray[bc]+"\t"+wordsarray[id]);
  
  
                          }
                pw.flush();
                pw.close();
                br.close();
                socket.close();
                server.close();
  
  }
  catch (Exception e){
  e.printStackTrace();
   }
  </code></pre>
  
  <p>If I put a comma on <code>line.split();</code> and the file has a different delimiter, it produces repeated lines and I don't even know why is that happening</p>
  
  <pre><code>COLUMN NAME A COLUMN NAME B COLUMN NAME C COLUMN NAME D COLUMN NAME E COLUMN NAME A COLUMN NAME B COLUMN NAME C COLUMN NAME D COLUMN NAME E COLUMN NAME A COLUMN NAME B COLUMN NAME C COLUMN NAME D COLUMN NAME E COLUMN NAME A COLUMN NAME B COLUMN NAME C COLUMN NAME D COLUMN NAME E COLUMN NAME A COLUMN NAME B COLUMN NAME C COLUMN NAME D COLUMN NAME E COLUMN NAME A COLUMN NAME B COLUMN NAME C COLUMN NAME D COLUMN NAME E
  </code></pre>
  
  <p>But If the file has a matching delimiter of comma it produces just the right output.</p>
  
  <pre><code> COLUMN NAME A COLUMN NAME B COLUMN NAME C COLUMN NAME D COLUMN NAME E
  </code></pre>
  
  <p>Is there any way I can automatically detect the delimiter of a file so I won't have to worry which delimiter the file is using? Or is there a better solution for it?</p>