拆分字符串并存储在数组中

时间:2016-10-25 10:39:02

标签: java

我有要读取的CSV文件,我存储在字符串中,我需要在每个","我的意见就像

名称,ID,说明,emailId Micheal,234655,"下一行问题的测试说明 谢谢和问候,Mike",Mike @ yahoo.com

请帮助我。

此致 维杰

enter code here

import java.io.*;
import java.util.logging.*;
import java.util.ArrayList;
import java.util.List;
public class ReadingCSVFile {

 public static void main(String args[])
 {
    StringBuilder sb =new StringBuilder();
    int n;
    String inputStream;
    String Name[] =new String[30];
    List l1 = new ArrayList();
    try
    {
         FileInputStream fis =new  fileInputStream("C:/Users/vijaykumar.naga/Desktop/Abc.txt"); 
     FileOutputStream fos = new FileOutputStream("C:/Users/vijaykumar.naga/Desktop/Output.txt");
     int ch;
     while((ch = fis.read()) != -1){
         sb.append((char)ch);
     }
     System.out.println("The InputString is " +sb.toString());
     String abcd =sb.toString();
       for(int i=0; i<30; i++)
         {
         l1.add(abcd.split(","));
         System.out.println("The Names are " +l1);
          }

        }
    catch(Exception e)
     {

      }
    }

  }

4 个答案:

答案 0 :(得分:0)

请参阅代码的修订版并附上说明:

import java.io.*;
import java.util.logging.*;
import java.util.ArrayList;
import java.util.List;
public class ReadingCSVFile {

 public static void main(String args[])
 {
    StringBuilder sb =new StringBuilder();
    int n;
    String inputStream;
    String Name[] =new String[30];
    List<String> l1 = new ArrayList<>(); //Use generics <> operator
    try
    {
         FileInputStream fis =new  fileInputStream("C:/Users/vijaykumar.naga/Desktop/Abc.txt"); 
     FileOutputStream fos = new FileOutputStream("C:/Users/vijaykumar.naga/Desktop/Output.txt");
     int ch;
     while((ch = fis.read()) != -1){
         sb.append((char)ch);
     }
     System.out.println("The InputString is " +sb.toString());
     String abcd =sb.toString();
       /*for(int i=0; i<30; i++)
         {
         l1.add(abcd.split(","));           //This is wrong. abcd.split(",") will return String[]
         System.out.println("The Names are " +l1);
          }

        }*****This loop is not needed*/ 
    //As you already read the content in abcd variable, do as follows
    l1.addAll(Arrays.asList(abcd.split(",")));

    catch(Exception e)
     {

      }
    }

  }

答案 1 :(得分:0)

这是我用来读取CSV文件的简单解决方案。

    Scanner sc = null;
    try {
        sc = new Scanner(new File("test.csv"));
        String s = null;
        String[] array;
        while(sc.hasNext()){
            s = sc.nextLine();
            array = s.split(",");
            System.out.println(Arrays.toString(array));
        }
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } finally {
        if(sc != null) sc.close();
    }

基本上,这将使用扫描仪一次读取一行,可能不是最好的但是40行,我真的不在乎;)

然后,我使用逗号分割行,小心如果在值中使用,,则不在此处进行检查。

然后,我在控制台中打印数组。

编辑:

try (Scanner sc = new Scanner(new File("test.csv"))){
        String s = null;
        String[] array;
        while(sc.hasNext()){
            s = sc.nextLine();
            array = s.split(",");
            // Do what you want with the array, here I just print the result
            System.out.println(Arrays.toString(array));
        }
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    }

答案 2 :(得分:0)

我建议使用正则表达式:
    ([A-ZA-Z] +)\,(\ d +)\,(\ “+ \”。。| +)\,(\ W + @ \ W + \ W +)

答案 3 :(得分:-1)

您可以尝试split()

使用以下语法

String[] parts = string.split(",");

或者您可以将StringTokenizer与令牌,

一起使用

希望这有帮助

相关问题