如何读取CSV中的行并将其放入java中的文本文件中

如何读取CSV中的行并将其放入java中的文本文件中,java,csv,Java,Csv,正在寻找一种存储csv的方法,然后取第一行,将数据值放在文本文件的某些位置,然后转到下一行。不希望使用外部库或JAR 我的CSV看起来像:日期、代码、时间、代码2,值为55463333, 1999年5月3日;0003;2234(是的,最后一次有多个输入 列。它将有多行 我的Csv代码是: Scanner scanner = new Scanner(new File ("C:\\Users\\Documents\\order.csv\\")); scanner.useDelimiter

正在寻找一种存储csv的方法,然后取第一行,将数据值放在文本文件的某些位置,然后转到下一行。不希望使用外部库或JAR

我的CSV看起来像:日期、代码、时间、代码2,值为55463333, 1999年5月3日;0003;2234(是的,最后一次有多个输入 列。它将有多行

我的Csv代码是:

   Scanner scanner = new Scanner(new File ("C:\\Users\\Documents\\order.csv\\"));
   scanner.useDelimiter("\n");

   ArrayList<String> data = new ArrayList<String>();
   while (scanner.hasNext()){
       scanner.useDelimiter(",");
       data.add(scanner.next());
   }

   scanner.close();
Scanner Scanner=new Scanner(新文件(“C:\\Users\\Documents\\order.csv\”);
scanner.useDelimiter(“\n”);
ArrayList数据=新的ArrayList();
while(scanner.hasNext()){
scanner.useDelimiter(“,”);
data.add(scanner.next());
}
scanner.close();

不确定如何在数组中循环使用csv中的所有元素,然后将这些元素写入文本文件。

您可以做的是一次读取一行(使用下一行),而不是一次读取一个单词(下一步)

编辑:我看到您的对象可以拉伸到多行

假设您有如下所示的csv

创建一个名为my object的类

class MyObjectEntity{
  String a, b, c;
  String d=""; 
  public MyObjectEntity(String[] tokens){
     this.a = tokens[0]; ... the rest of your constructor
  }

  public void appendD(String s) { d+=s; } //used for appending the next line. 
}
然后在你的代码中

MyObjectEntity object = null;
List<MyObjectEntity> list = new ArrayList<>();
while (scanner.hasNextLine()){
   String line = scanner.nextLine();

   if(object ==null) { //check if the object already exists?
       //no previous object.

       //you then chop up your text using the split
       String[] choppedUpText = line.split(",");
       if(choppedUpText.length ==4){ //has to have 4 parts 
           //we create the object first. 
           //then continue iterating to see if there are more parts
           object = new MyObjectEntity( choppedUpText);
       }else {
           println("Error in line: "+ line);
       }

   }else {
      //previous object exists

      if(line.contains(","){ // check if it is a new object, or a continuation of d
          //new object (save old copy to the list)
          list.add(object)

          //create a new object
          String[] choppedUpText = line.split(",");
          if(choppedUpText.length ==4){
             object = new MyObjectEntity( choppedUpText);
          }else {
             println("Error in line: "+ line);
             object = null; //reset it back to null
          }

      }else{

          //continuation of d
          object.append(line);
      }   
   } 
}
//end of loop (if object is not null, then add the last entry in)
if(object !=null) {    list.add(object);    }
MyObjectEntity对象=null;
列表=新的ArrayList();
while(scanner.hasNextLine()){
字符串行=scanner.nextLine();
如果(object==null){//检查对象是否已经存在?
//没有以前的对象。
//然后使用拆分器将文本切碎
字符串[]choppedUpText=line.split(“,”);
如果(choppedUpText.length==4){//必须有4个部分
//我们首先创建对象。
//然后继续迭代,看看是否有更多的部分
对象=新的MyObject实体(choppedUpText);
}否则{
println(“行中错误:+行);
}
}否则{
//上一个对象存在
if(line.contains(“,”{//检查它是新对象还是d的延续
//新建对象(将旧副本保存到列表)
list.add(对象)
//创建一个新对象
字符串[]choppedUpText=line.split(“,”);
如果(choppedUpText.length==4){
对象=新的MyObject实体(choppedUpText);
}否则{
println(“行中错误:+行);
object=null;//将其重置回null
}
}否则{
//d的延续
object.append(行);
}   
} 
}
//循环结束(如果对象不为null,则在中添加最后一个条目)
如果(object!=null){list.add(object);}

您得到的输出是什么?不想使用外部库或JAR-为什么?csv处理可能会变得非常复杂。您应该使用NextLine而不是next,然后使用拆分。迭代列表(),然后写入文本文件()。
class MyObjectEntity{
  String a, b, c;
  String d=""; 
  public MyObjectEntity(String[] tokens){
     this.a = tokens[0]; ... the rest of your constructor
  }

  public void appendD(String s) { d+=s; } //used for appending the next line. 
}
MyObjectEntity object = null;
List<MyObjectEntity> list = new ArrayList<>();
while (scanner.hasNextLine()){
   String line = scanner.nextLine();

   if(object ==null) { //check if the object already exists?
       //no previous object.

       //you then chop up your text using the split
       String[] choppedUpText = line.split(",");
       if(choppedUpText.length ==4){ //has to have 4 parts 
           //we create the object first. 
           //then continue iterating to see if there are more parts
           object = new MyObjectEntity( choppedUpText);
       }else {
           println("Error in line: "+ line);
       }

   }else {
      //previous object exists

      if(line.contains(","){ // check if it is a new object, or a continuation of d
          //new object (save old copy to the list)
          list.add(object)

          //create a new object
          String[] choppedUpText = line.split(",");
          if(choppedUpText.length ==4){
             object = new MyObjectEntity( choppedUpText);
          }else {
             println("Error in line: "+ line);
             object = null; //reset it back to null
          }

      }else{

          //continuation of d
          object.append(line);
      }   
   } 
}
//end of loop (if object is not null, then add the last entry in)
if(object !=null) {    list.add(object);    }