如何编写ArrayList<;字符串[]>;到Java中的.csv文件?
假设我有一个字符串数组的ArrayList,如下所示:如何编写ArrayList<;字符串[]>;到Java中的.csv文件?,java,arrays,csv,arraylist,data-structures,Java,Arrays,Csv,Arraylist,Data Structures,假设我有一个字符串数组的ArrayList,如下所示: ArrayList<String[]> arrayList = new ArrayList<String[]>(); arrayList.add(new String[]{"hello", "goodbye"}); arrayList.add(new String[]{"yoyo", "fofo"}); a
ArrayList<String[]> arrayList = new ArrayList<String[]>();
arrayList.add(new String[]{"hello", "goodbye"});
arrayList.add(new String[]{"yoyo", "fofo"});
arrayList.add(new String[]{"foo", "bar"});
ArrayList ArrayList=new ArrayList();
add(新字符串[]{“你好”,“再见”});
add(新字符串[]{“yoyo”,“fofo”});
add(新字符串[]{“foo”,“bar”});
用Java将其转换为.csv文件的最简单方法是什么?因为ArrayList是动态的,我在其中使用了字符串数组,因为数据中的列数不会改变,我们可以继续为.csv输出中的每一个新“行”向ArrayList添加更多数组。如果要将字符串数组写入文件,您可以迭代数组并写入文件 范例
List<String[]>arrayList= new ArrayList<String[]>();
arrayList.add(new String[]{"hello", "goodbye"});
arrayList.add(new String[]{"yoyo", "fofo"});
arrayList.add(new String[]{"foo", "bar"});
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
for(String[] data : arrayList) {
writer.write(String.join(",", data );
}
用法:
CSVPrinter csvPrinter= CSVUtil.getCSVPrinter(outDir, "fileName",
true);
csvPrinter.writeln(new String[] {"Data1","Data2","Data3"});
您需要执行以下操作:
try {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Files in Java might be tricky, but it is fun enough!");
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
for (String[] entry : arrayList) {
System.out.println(entry);
}
以下是关于如何迭代ArrayList的基本教程:
例如:
try {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Files in Java might be tricky, but it is fun enough!");
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
for (String[] entry : arrayList) {
System.out.println(entry);
}
祝你好运
PS:我故意不给你复制/粘贴的完整代码,所以你实际上学到了一些东西,一种可能的方法如下: 你可能需要添加一些检查之类的东西 //代码
import java.util.ArrayList;
import java.io.*;
public class a{
public static void main(String[] args){
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add(new String("hello"));
arrayList.add(new String("yoyo"));
arrayList.add(new String("foo"));
try{
FileWriter file = new FileWriter("sample.csv");
PrintWriter write = new PrintWriter(file);
for (String name: arrayList){
write.println(name);
}
write.close();
}
catch(IOException exe){
System.out.println("Cannot create file");
}
}
import java.util.ArrayList;
导入java.io.*;
公共a类{
公共静态void main(字符串[]args){
ArrayList ArrayList=新的ArrayList();
add(新字符串(“hello”);
add(新字符串(“yoyo”);
add(新字符串(“foo”);
试一试{
FileWriter文件=新的FileWriter(“sample.csv”);
PrintWriter write=新的PrintWriter(文件);
for(字符串名称:arrayList){
write.println(名称);
}
write.close();
}
捕获(IOException exe){
System.out.println(“无法创建文件”);
}
}
}PPS:如果您希望每行一对,您可能希望在每对之后写一个换行符(Linux中为\n,Windows中为\r\n)。我不认为工厂模式有任何必要回答“什么是……的最简单方式”这一问题:)我在该解决方案中添加了一个嵌套for循环来处理所讨论的原始数据结构:ArrayList。无论如何谢谢你!是的,没问题!很高兴我能帮上忙。