这是我拥有的代码,本质上我想将其写入 csv
文件:
ArrayList <String> course = new ArrayList<>();
ArrayList <String> name = new ArrayList<>();
ArrayList <Integer> age = new ArrayList<>();
File file = new File("jj.csv");
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Course,Name,Age");
bw.newLine();
for (int i = 0; i < course.size(); i++) {
bw.write(course.get(i));
bw.newLine();
}
for (int i = 0; i < name.size(); i++) {
bw.write("," + name.get(i));
bw.newLine();
}
for (int i = 0; i < age.size(); i++) {
bw.write("," + age.get(i));
bw.newLine();
}
bw.close();
fw.close();
我希望输出采用这种格式,我得到一个输出,其中第二行和第三行到处都是:
Course Name Age
math john 7
english bob 9
回答1
我认为使用 MessageFormat.format()
方法(它在 java.text 包中)是一个很好的解决方案。这是一个例子:
@Test
public void test05(){
final String formatTemplate = "{0},{1},{2}";
List<String> course = Arrays.asList("course0","course1");
List <String> name = Arrays.asList("name0","name1");
List <Integer> age = Arrays.asList(1,2);
final int size = course.size();
for (int i = 0; i < size; i++) {
final String lineString = MessageFormat.format(formatTemplate, course.get(i), name.get(i), age.get(i));
System.out.println(lineString);
//TODO: bw.write(linString);bw.newLine();
}
}
可以得到:
course0,name0,1
course1,name1,2
回答2
尝试在每次写入后删除换行符的插入并使用一个循环,我将假设三个 lists 具有相同的大小
for(int i=0;i<course.size();i++) {
bw.write(course.get(i));
bw.write(","+name.get(i));
bw.write(","+age.get(i));
bw.newLine();
}
bw.close();
fw.close();
, 或单行使用
for(int i=0;i<course.size();i++) {
bw.write(course.get(i) + ","+name.get(i) + ","+age.get(i));
bw.newLine();
}
bw.close();
fw.close();
回答3
尽我所能从您的问题中得知,您可以这样做。它写入文件和控制台。
一些数据
List<String> course = List.of("math", "english", "chemistry");
List<String> name = List.of("John", "Mary", "James");
List<Integer> age = List.of(19, 20, 20);
// file destination and format string
File file = new File("jj.csv");
String format = "%-10s %-8s %-4s%n";
- 这使用 try-with-resources 来打开和关闭 writer。
- 在单个循环中处理 lists
- 使用
System.format
为csv
文件格式化 values - 并使用
System.printf
将它们格式化到控制台。
- 使用
try (BufferedWriter bw =
new BufferedWriter(new FileWriter(file))) {
bw.write("Course,Name,Age");
System.out.printf(format, "Course", "Name",
"Age");
for (int i = 0; i < course.size(); i++) {
for (int i = 0; i < course.size(); i++) {
// for csv file
String s = String.format(",%s,%s,%s",course.get(i),
name.get(i), age.get(i));
// for console
System.out.printf(format, course.get(i),
name.get(i), age.get(i));
bw.write(s);
}
}
} catch (Exception e) {
e.printStackTrace();
}
印刷
Course Name Age
math John 19
english Mary 20
chemistry James 20
您可能需要考虑创建一个类来保存学生信息。
class Student {
private String name;
private String course;
private int age;
public Student(String name, String course, int age) {
this.name = name;
this.course = course;
this.age = age;
}
public String getName() {
return name;
}
public String getCourse() {
return course;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return String.format("%s, %s, %s]", name, course, age);
}
}
然后你可以把它放在像这样的 ArrayList
中。
List<Student> students = new ArrayList<>(
List.of(new Student("John", "math", 19),
new Student("Mary", "english", 20),
new Student("John", "James", 20)));
并使用循环打印。
for (Student student : students) {
System.out.println(student);
}
印刷
[John, math, 19]
[Mary, english, 20]
[John, James, 20]
输出格式由类的 toString
方法控制,可以调整以满足您的要求。