【问题标题】:Save output to an array then write to a .csv file将输出保存到数组,然后写入 .csv 文件
【发布时间】:2014-11-21 09:40:12
【问题描述】:

我有一些代码,但我需要帮助弄清楚如何将 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 文件中......这个脚本还能工作吗?我对编码还很陌生...

【问题讨论】:

  • 你能提供一些示例关键字和一些来自 csv 的示例文本吗?另外,您要写入新文件的“结果”是什么?
  • stackoverflow.com/questions/27056065/… 这就是我想要做的。忽略最后一个脚本..它没有工作

标签: java arrays file csv save


【解决方案1】:

尝试使用此代码:

//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 文件中的数据和条款文件中的数据。下一步是处理这些数据并将其写入文件。

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();
}

【讨论】:

  • 感谢您的回复!有什么方法可以用 JavaScript 而不是 Java 来编写它,这样我就可以从批处理文件中调用它并运行它?
  • @AmarokStudios 抱歉,我不知道...但是您也可以批量运行 .jar 或 .class 文件。对于 .jar 使用:java -jar [your jar file name].jar 和对于 .class 使用:java [your class name]
  • 好的,太棒了。我会试一试!我会回来告诉您它是否有效,如果有效,请接受您的回答。谢谢:D
  • @AmarokStudios 你需要导入它们!如果您使用的是 eclipse,您可以使用 Ctrl + Shift + o 来修复导入。如果您使用的是 netbeans,请按 Ctrl + Shift + I。
  • @AmarokStudios 或直接复制粘贴: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;
【解决方案2】:

是的,您可以改用这个循环:

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

   //merge all the data into one line and separate it by “,“
   for(String s : finalData){
      finalLine += s + ","; 
   }

   //remove the comma at the end of the line
   finalLine = finalLine.substring(0, finalLine.length() - 1);

   bw.write(finalLine);

   bw.close();
 }

【讨论】:

  • 好的...还有一个问题。我有多行数据,所以我想逐行搜索它们。有没有办法做到这一点?现在它将数据保存在一行中。
【解决方案3】:

给你。我不得不改变整个程序。 :) 不要再次忘记导入 ;)

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

private static BufferedWriter bw;

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

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) {
        processLine(line);
    }

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

public static void processLine(String line) throws Exception{
    String[] splitLine = line.split(",");
    String finalLine = "";
    for(int i = 0; i < splitLine.length; i++){
    for(String k : readTerms){
        if(splitLine[i].contains(k)){
            finalLine += splitLine[i] + ",";
        }
    }
    }
    finalLine = finalLine.substring(0, finalLine.length() - 1);
    saveLine(finalLine);
}

public static void saveLine(String line) throws Exception{
    bw.write(line);
    bw.newLine();
}

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

public static void closeWriter() throws Exception{
    bw.close();
}

【讨论】:

  • 非常感谢您的帮助...我有导入但它告诉我我的令牌有语法错误...:/
  • 1.检查你的进口: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;
  • 2.检查您是否已将代码放入类中。 public class [TheNameOfYourClass]{ [the code] }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-07
  • 1970-01-01
  • 2011-12-06
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
  • 2020-12-16
相关资源
最近更新 更多