java - 将输出保存到数组,然后写入 .csv 文件

标签 java arrays file csv save

我有一些代码,但我需要帮助弄清楚如何将该 if 语句的结果保存到数组中,然后将其保存到 .csv 文件中。提前致谢!

String data = new Scanner( new File("Test.csv") ).useDelimiter("\\A").next();
String terms = new Scanner( new File("Terms.txt") ).useDelimiter("\\A").next();

data.split(",");
terms.split("\n");

if (sourceElement.indexOf(dictionaryElement) > 0)
{
//Save results here
}
NameOfArray.join(",");
//Here I need to write the array to a .csv file... Help?

此外,我正在尝试使用充满关键字的文本文件来搜索 csv 文件并将结果保存到新的 csv 文件中...该脚本还能工作吗?我对编码还很陌生......

最佳答案

尝试使用此代码:

//imports
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

//create the array lists to save the data
private static List<String> readData = new ArrayList<String>();
private static List<String> readTerms = new ArrayList<String>();
private static List<String> splitData = new ArrayList<String>();
private static List<String> finalData = new ArrayList<String>();

public static void main(String[] args) throws Exception{
   readData();
   processData();
   writeFinalData();
}

public static void readData() throws Exception{
//create readers
BufferedReader data = new BufferedReader(new FileReader(new File("Test.csv")));
BufferedReader term = new BufferedReader(new FileReader(new File("Terms.txt")));

//read the data and save it into the array lists
String line;
while((line = term.readLine()) != null){
   readTerms.add(line);
}

while((line = data.readLine()) != null){
   readData.add(line);
}

//close the readers
data.close();
term.close();
}

现在我们有两个数组列表,分别保存 .csv 文件中的数据和 terms 文件中的数据。下一步是处理这些数据并将其写入文件。

public static void processData(){
   //looping through every line in the .csv file, spliting it by , and saving every     piece of the data into a new array list
   for(String d : readData){
      String[] split = d.split(",");
      for(int i = 0; i < split.length; i++){
         splitData.add(split[i]);
      }
   }

//searching the .csv file for keywords
for(String s : splitData){
   for(String k : readTerms){
      //testing if the data from the .csv file contains (or is equal to) the keyword. If true then we add it to the array list which holds the final data that will be writen back to the file
      if(s.contains(k)){
         finalData.add(s);
      }
   }
}
}

剩下要做的最后一件事是将与关键字匹配的数据写回文件。

public static void writeFinalData() throws Exception{
   BufferedWriter bw = new BufferedWriter(new FileWriter(new File("words.txt")));

   for(String s : finalData){
      bw.write(s);
      bw.newLine();
   }

   bw.close();
}

关于java - 将输出保存到数组,然后写入 .csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27058249/

相关文章:

java - 错误 MVC org.springframework.validation.BeanPropertyBindingResult : 1 errors with array

command-line - Java-运行cmd

JavaScript:方便使用 3 维数组吗?

javascript - 使用索引和值将元素添加到现有对象

java - 使用 Java 重命名文件

java - 从 javafx ant 任务签署 jar 不使用 jarsigner 验证

java - 使用嵌入式 Jetty 提供静态文件

javascript - 如何使用其他数组为 ApexCharts 动态创建和命名 N 个数据系列?

javascript - 在与先前选择的相同文件上检测文件选择

java - 有没有一种通用的文件访问方式,既适用于 Android 也适用于 PC?