其次,我创建了一个类,负责将二维对象数组的内容(JTable 内容)写入磁盘。这概述如下:
import java.io.*;
/**
*
* This class is responsible for writing the 2D object to disk.
* The 2d Object contains your JTable contents
* <p>
*
* @author Mark Burleigh
* @version %I%, %G%
* @since 1.0
*
*/
public class WriteJTableContents
{
/**
*
* This constructor takes in two parameters. It is also responsible
* for writing the JTable contents to disk (to csv file)
*
*
* @param aData - the JTable data to be saved to disk
* @param afile - the name of the file where the data shall be saved
* (this is a .CSV type file)
*
*
*/
public WriteRandomSampleData(Object[][] aData, String afile)
{
writeToDisk(aData,afile);
// This method prints the two-dimensional array to the command console
// printData();
}
/**
*
* This method is responsible for writing the contents of a JTable (2d
* array object) to disk (csv text file)
* <p>
*
* @param aData - the 2D data (Jtable contents) to be stored to disk
* @param aDatafile - the file where the data shall be stored
* to disk. This shall be of type.CSV
*
* @return
*
* @see
*
*/
public void writeToDisk(Object[][] aData, String aDatafile)
{
try
{
FileOutputStream fout = new FileOutputStream(aDatafile, false);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
//Headers
bw.append("Script No., Candidate No., Mark, Grade,Script No., Candidate No., Mark, Grade");
// End of data row (Jable row) so append new line character in csv file
bw.append('\n');
for (int row = 0; row < aData.length; row++)
{
for (int column = 0; column < aData[row].length; column++)
{
if(aData[row][column] == null)
{
bw.append("null");
// The comma separated value
bw.append(',');
}
else
{
/* In my particular example, I am doing some checking on
the 2d array for types:
if the data is not of type null (as checked above)
then it must be of type Integer.
This is because the 2D data array only contains data of either
Integer or null
each of these object types have a method called toString().
we need this in order to convert the types to a string prior to wrting them to
the file.
*/
bw.append(aData[row][column].toString());
bw.append(',');
}
}//end column loop (inner loop)
bw.append('\n');
}//end row loop (outer loop)
bw.close();
}
catch (Exception e)
{
e.getStackTrace();
}
}//end of readFileFromDisk
/**
*
* These methods is responsible for printing the random sample scripts
* Into the command console.
* <p>
*
*
*/
public void printData()
{
//System.out.println();
//System.out.println("=======WriteRandomSampleData Class===========");
//System.out.println();
for (int row = 0; row < data.length; row++)
{
for (int column = 0; column < data[row].length; column++)
{
System.out.println("data["+row+"]["+column+"] = " +data[row][column]);
}
}
}
//==================Instance Variables=============================
// JTable contents hedata
private Object[][] data;
//====================Test Driver============================
public static void main(String args[])
{
// file seperator for windows platform '\\'
String aFileLocation = "C:\\dirA\\subdir1\\subdir2\\";
// Dummy values - 2D array which stores the contents of a
// JTable into a csv text file
Object[][] testData = new Object [][] {
{new Integer(1),new Integer(1),null,null,new Integer(11),new Integer(1),null,null},
{new Integer(2),new Integer(1),null,null,new Integer(12),new Integer(1),null,null},
{new Integer(3),new Integer(1),null,null,new Integer(13),new Integer(1),null,null},
{new Integer(4),new Integer(1),null,null,new Integer(14),new Integer(1),null,null},
{new Integer(5),new Integer(1),null,null,new Integer(15),new Integer(1),null,null},
{new Integer(6),new Integer(1),null,null,new Integer(16),new Integer(1),null,null},
{new Integer(7),new Integer(1),null,null,new Integer(17),new Integer(1),null,null},
{new Integer(8),new Integer(1),null,null,new Integer(18),new Integer(1),null,null},
{new Integer(9),new Integer(1),null,null,new Integer(19),new Integer(1),null,null},
{new Integer(10),new Integer(1),null,null,new Integer(20),new Integer(1),null,null}
};
// SampleData_TEST.csv gets created in the particular directory
// and the file gets populated with the contents of the JTable
new WriteRandomSampleData(testData,aFileLocation2+"SampleData_TEST.csv");
}
}