import com.opencsv.CSVReader; public class CSVImporter { public static void main(String[] args) { try { CSVReader reader = new CSVReader(new FileReader("data.csv")); String[] nextLine; while ((nextLine = reader.readNext()) != null) { for (String cell : nextLine) { System.out.print(cell + " "); } System.out.println(); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } } import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; public class CSVImporter { public static void main(String[] args) { try { Reader reader = Files.newBufferedReader(Paths.get("data.csv")); CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT); for (CSVRecord csvRecord : csvParser) { for (String cell : csvRecord) { System.out.print(cell + " "); } System.out.println(); } csvParser.close(); reader.close(); } catch (IOException e) { e.printStackTrace(); } } } import com.opencsv.CSVWriter; public class CSVExporter { public static void main(String[] args) { try { CSVWriter writer = new CSVWriter(new FileWriter("data.csv")); String[] header = {"Name", "Age", "Email"}; writer.writeNext(header); String[] data1 = {"Alice", "25", "alice@example.com"}; String[] data2 = {"Bob", "30", "bob@example.com"}; writer.writeNext(data1); writer.writeNext(data2); writer.close(); } catch (IOException e) { e.printStackTrace(); } } } import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; public class CSVExporter { public static void main(String[] args) { try { FileWriter writer = new FileWriter("data.csv"); CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT); csvPrinter.printRecord("Name", "Age", "Email"); csvPrinter.printRecord("Alice", "25", "alice@example.com"); csvPrinter.printRecord("Bob", "30", "bob@example.com"); csvPrinter.flush(); csvPrinter.close(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }


上一篇:
下一篇:
切换中文