Writing D:/inetpub/webs/vincenzodevivocom/wiki/data/cache/d/d8ddd2abf1422d1563498d2babef1079.i failed
Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.
Writing D:/inetpub/webs/vincenzodevivocom/wiki/data/cache/d/d8ddd2abf1422d1563498d2babef1079.i failed
Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.
Writing D:/inetpub/webs/vincenzodevivocom/wiki/data/cache/d/d8ddd2abf1422d1563498d2babef1079.xhtml failed

CSV

Come leggere e scrivere un file CSV in Java

Usando JavaCSV

Read CSV from file

CsvReaderExample.java
/*
File Format: 
-----------
  ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued
  1,Chai,1,1,10 boxes x 20 bags,18,39,0,10,FALSE
  2,Chang,1,1,24 - 12 oz bottles,19,17,40,25,FALSE
*/
import java.io.FileNotFoundException;
import java.io.IOException;
 
import com.csvreader.CsvReader;
 
public class CsvReaderExample {
 
	public static void main(String[] args) {
		try {
 
			CsvReader products = new CsvReader("products.csv");
 
			products.readHeaders();
 
			while (products.readRecord())
			{
				String productID = products.get("ProductID");
				String productName = products.get("ProductName");
				String supplierID = products.get("SupplierID");
				String categoryID = products.get("CategoryID");
				String quantityPerUnit = products.get("QuantityPerUnit");
				String unitPrice = products.get("UnitPrice");
				String unitsInStock = products.get("UnitsInStock");
				String unitsOnOrder = products.get("UnitsOnOrder");
				String reorderLevel = products.get("ReorderLevel");
				String discontinued = products.get("Discontinued");
 
				// perform program logic here
				System.out.println(productID + ":" + productName);
			}
 
			products.close();
 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
 
	}
 
}

Write / Append to CSV file

CsvWriterAppendExample.java
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
import com.csvreader.CsvWriter;
 
public class CsvWriterAppendExample {
 
	public static void main(String[] args) {
 
		String outputFile = "users.csv";
 
		// before we open the file check to see if it already exists
		boolean alreadyExists = new File(outputFile).exists();
 
		try {
			// use FileWriter constructor that specifies open for appending
			CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true), ',');
 
			// if the file didn't already exist then we need to write out the header line
			if (!alreadyExists)
			{
				csvOutput.write("id");
				csvOutput.write("name");
				csvOutput.endRecord();
			}
			// else assume that the file already has the correct header line
 
			// write out a few records
			csvOutput.write("1");
			csvOutput.write("Bruce");
			csvOutput.endRecord();
 
			csvOutput.write("2");
			csvOutput.write("John");
			csvOutput.endRecord();
 
			csvOutput.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
 
	}
}
/*
Output Format:
-------------
  id,name
  1,Bruce
  2,John
*/
 
code/java/csv.txt · Last modified: 2011/05/14 00:00 by Vincenzo De Vivo
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki