如何从JAVA中的文本文件中读取逗号分隔值?

我有这个文本文件,其中包含地图上不同点的纬度和经度值。 我想使用hibernate将这些坐标存储到mySQL数据库中。 我想知道如何将我的弦乐分成纬度和经度? 与空间,制表符等其他分隔符一起执行这些类型的事物的一般方法是什么? 文件:

28.515046280572285,77.38258838653564 28.51430151808072,77.38336086273193 28.513566177802456,77.38413333892822 28.512830832397192,77.38490581512451 28.51208605426073,77.3856782913208 28.511341270865113,77.38645076751709 28.510530488025346,77.38720178604126 28.509615992924807,77.38790988922119 28.50875805732363,77.38862872123718 28.507994394490268,77.38943338394165 28.50728729434496,77.39038825035095 28.506674470385246,77.39145040512085 28.506174780521828,77.39260911941528 28.505665660113582,77.39376783370972 28.505156537248446,77.39492654800415 28.50466626846366,77.39608526229858 28.504175997400655,77.39724397659302 28.503685724059455,77.39840269088745 28.503195448440064,77.39956140518188 28.50276174118543,77.4007523059845 28.502309175192945,77.40194320678711 28.50185660725938,77.40313410758972 28.50140403738471,77.40432500839233 28.500951465568985,77.40551590919495 28.500498891812207,77.40670680999756 28.5000463161144,77.40789771080017 28.49959373847559,77.40908861160278 

我用来读取文件的代码:

  try { BufferedReader in = new BufferedReader(new FileReader("G:\\RoutePPAdvant2.txt")); String str; str = in.readLine(); while ((str = in.readLine()) != null) { System.out.println(str); } in.close(); } catch (IOException e) { System.out.println("File Read Error"); } 

您可以使用String.split()方法:

 String[] ar=str.split(","); 

之后,使用Double.parseDouble ()方法将字符串值解析为double。

 double latitudes=Double.parseDouble(ar[0]); double longitudes=Double.parseDouble(ar[1]); 

使用OpenCSV可靠性。 拆分不应该用于这类事情。 这是我自己的程序片段,非常简单。 我检查是否指定了分隔符,如果不是,则使用此分隔符,如果不是,则使用OpenCSV中的默认值(逗号)。 然后我读了标题和字段

 CSVReader reader = null; try { if (delimiter > 0) { reader = new CSVReader(new FileReader(this.csvFile), this.delimiter); } else { reader = new CSVReader(new FileReader(this.csvFile)); } // these should be the header fields header = reader.readNext(); while ((fields = reader.readNext()) != null) { // more code } catch (IOException e) { System.err.println(e.getMessage()); } 

如果您更喜欢使用OpenCSV,OpenCSV是一个受欢迎的库。
详情请访问: http : //opencsv.sourceforge.net/

关于类似主题还有一些其他stackoverflow问题:

  • 适用于Java的CSV API
  • 你能推荐一个Java库来读取(并可能写)CSV文件吗?

//lat=3434&lon=yy38&rd=1.0& | 以该格式o / p正在显示公共类ReadText {

 public static void main(String[] args) throws Exception { FileInputStream f= new FileInputStream("D:/workplace/sample/bookstore.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(f)); String strline; StringBuffer sb = new StringBuffer(); while ((strline = br.readLine()) != null) { String[] arraylist=StringUtils.split(strline, ","); if(arraylist.length == 2){ sb.append("lat=").append(StringUtils.trim(arraylist[0])).append("&lon=").append(StringUtils.trim(arraylist[1])).append("&rt=1.0&|"); }else{ System.out.println("Error: "+strline); } } System.out.println("Data: "+sb.toString()); } 

}

要用逗号分隔字符串(,),请使用str.split(",") ,对于制表符,请使用str.split("\\t")

  try { BufferedReader in = new BufferedReader( new FileReader("G:\\RoutePPAdvant2.txt")); String str; while ((str = in.readLine())!= null) { String[] ar=str.split(","); ... } in.close(); } catch (IOException e) { System.out.println("File Read Error"); } 

您还可以使用java.util.Scanner类。

 private static void readFileWithScanner() { File file = new File("path/to/your/file/file.txt"); Scanner scan = null; try { scan = new Scanner(file); while (scan.hasNextLine()) { String line = scan.nextLine(); String[] lineArray = line.split(","); // do something with lineArray, such as instantiate an object } catch (FileNotFoundException e) { e.printStackTrace(); } finally { scan.close(); } }