用一个简单的文件练习
迈出小步。首先,请确保您已成功打开文件。
现代 Java 提供了 Path、File 和 Files 类,可以更轻松地处理存储中的文件。
例子:
package work.basil.example;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Csver
{
public static void main ( String[] args )
{
Csver app = new Csver() ;
app.writeDummyFile() ;
}
private void writeDummyFile ()
{
Path path = Paths.get( "/Users/basilbourque/dummy.txt" ) ;
// Use try-with-resources to auto-close the file if successfully opened.
try (
BufferedWriter writer = Files.newBufferedWriter( path ) ; // Will be automatically closed if successfully opened.
)
{
writer.write( "Bonjour le monde!" ) ;
} catch ( IOException e )
{
e.printStackTrace() ;
}
}
}
使用 CSV 库
接下来,对于 CSV 或制表符分隔的文件等,请使用库。我使用Apache Commons CSV。在 Stack Overflow 上搜索许多使用此库和其他此类库来读取和写入此类文件的示例。
使用CSVFormat 定义文件类型。这里我们使用RFC 4180 定义的标准CSV。请注意,标准 CSV 使用 CRLF(回车换行符)来表示换行符,而不是在类 Unix 平台中常见的 LF(换行符)。
打开文件时,我们指定UTF-8字符编码。 UTF-8 通常是最好使用的编码;它涵盖了所有 Unicode 字符,并且是 US-ASCII 的超集。
CSVPrinter::print 方法在输出一行时一次添加一个字段。我们通过调用CSVPrinter::println 来终止该行。
我将您的 i 和 j 变量重命名为有意义。
我在文件顶部添加了列名。您可能希望保留或放弃该功能,由您自己决定。
注意我们如何使用 try-with-resources 语法来自动关闭我们的文件。
package work.basil.example;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import javax.swing.*;
import javax.swing.table.TableModel;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Csver
{
public static void main ( String[] args )
{
Csver app = new Csver();
// Practice writing a simple file.
app.writeDummyFile();
// Write out the data in a JTable to standard CSV file.
JTable jtable = app.newJTable();
app.writeCSV( jtable.getModel() );
System.out.println( "« fin »" );
}
private void writeCSV ( final TableModel model )
{
CSVFormat format = CSVFormat.RFC4180;
Path path = Paths.get( "/Users/basilbourque/animals.csv" );
try (
BufferedWriter writer = Files.newBufferedWriter( path , StandardCharsets.UTF_8 ) ;
CSVPrinter printer = new CSVPrinter( writer , format ) ;
)
{
// Print column headers, if you want.
for ( int columnIndex = 0 ; columnIndex < model.getColumnCount() ; columnIndex++ )
{
printer.print( model.getColumnName( columnIndex ) );
}
printer.println();
// Print rows.
for ( int rowIndex = 0 ; rowIndex < model.getRowCount() ; rowIndex++ )
{
for ( int columnIndex = 0 ; columnIndex < model.getColumnCount() ; columnIndex++ )
{
printer.print( model.getValueAt( rowIndex , columnIndex ) );
}
printer.println();
}
} catch ( IOException e )
{
e.printStackTrace();
}
}
private JTable newJTable ()
{
String[] columnNames = { "Species" , "Name" };
Object[][] data = {
{ "Dog" , "Delilah" } ,
{ "Cat" , "René" } ,
{ "Bird" , "Jordan" }
};
JTable table = new JTable( data , columnNames );
return table;
}
private void writeDummyFile ()
{
Path path = Paths.get( "/Users/basilbourque/dummy.txt" );
// Use try-with-resources to auto-close the file if successfully opened.
try ( BufferedWriter writer = Files.newBufferedWriter( path ) )
{
writer.write( "Bonjour le monde!" );
} catch ( IOException e )
{
e.printStackTrace();
}
}
}