Comma-separated records into String Array?

╄→尐↘猪︶ㄣ 提交于 2019-12-05 22:36:54

Commons CSV was designed for your specific use case. Let's not reinvent the wheel, the code below will result in a GZipped CSV being parsed into fields and lines and seems to be what you're trying to do.

public String[][] getInfo() throws IOException {
    final CSVParser parser = new CSVParser(new FileReader(new InputStreamReader(new GZIPInputStream(fileinput)), CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
    String[][] result = parser.nextRecord().values();
    return result;
}

Few of these modifications should work for you.

public String[] getArray(String source) {

FileInputStream fileinput = new FileInputStream(source);
GZIPInputStream gzip = new GZIPInputStream(fileinput);
InputStreamReader inputstream = new InputStreamReader(gzip);
BufferedReader bufr = new BufferedReader(inputstream);

String str = null;
List<String> numbers = new LinkedList<String>;

while((str = bufr.readLine()) != null) {
   String[] localArr = str.split(",");
   for(String intString : localArr){
    numbers.add(intString.trim());
   }
}
return arr;

Have you tried using the scanner class as well as scanner.nextInt(). you do not need to do striping then.

Scanner s = new Scanner(inputstream);
ArrayList<String> list = new ArrayList<String>();

while (s.hasNextInt())
    list.add(s.nextInt());
String[] arr = list.toArray(new String[list.size()]);

Not tested:

arr = str.replaceAll("\"", "").replaceAll("(","").replaceAll(")","").split(",");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!