java 读取csv文件代码<%@ page contenttype="text/html; charset=gb2312" language="java" import="java.sql.*" errorpage="" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>java 读取csv文件代码</title>
</head>
<body>
<%
/*
*/
import java.io.bufferedreader;
import java.io.bufferedwriter;
import java.io.file;
import java.io.filereader;
import java.io.filewriter;
import java.io.ioexception;
import java.text.simpledateformat;
import java.util.arraylist;
import java.util.date;
import java.util.list;
import java.util.stringtokenizer;
import java.util.logging.filehandler;
import java.util.logging.formatter;
import java.util.logging.level;
import java.util.logging.logrecord;
import java.util.logging.logger;
public class inputfile
{
private static logger getlogger()
{
try
{
filehandler hand = new filehandler( "c:/test/log.txt" );
hand.setlevel( level.all );
logger log = logger.getlogger( "log_file" );
hand.setformatter( new myloghander() );
log.addhandler( hand );
return log;
}
catch( exception e )
{
e.printstacktrace();
}
return null;
}
public static list< list< string > > fileinput( file file )
{
list< list< string > > listoflist = new arraylist< list< string > >();
try
{
if( file.exists() )
{
bufferedreader reader = new bufferedreader( new filereader( file ) );;
string line;
line = reader.readline();
for( ; line != null; line = reader.readline() )
{
stringtokenizer stokenizer = new stringtokenizer( line, " " );
list< string > list = new arraylist< string >();
while( stokenizer.hasmoreelements() )
list.add( stokenizer.nexttoken() );
listoflist.add( list );
}
}
else
getlogger().info( file + " not exist" );
(www.111cn.net) }
catch( ioexception e )
{
getlogger().info( e.getmessage() );
}
return listoflist;
}
public static list< list< string > > changevalue( list< list< string > > input, list< list< string > > param )
{
for( list< string > list : param )
{
int row = integer.parseint( list.get( 0 ) );
int line = integer.parseint( list.get( 1 ) );
string value = list.get( 2 );
input.get( row - 1 ).set( line - 1, value );
}
return input;
}
public static string fileoutput( list< list< string >> input )
{
string string = null;
for( int i = 0; i < input.size(); i ++ )
{
list< string > list = input.get( i );
for( int j = 0; j < list.size(); j ++ )
{
if( j == list.size() - 1 )
string += list.get( j ) + "n";
else
string += list.get( j ) + " ";
}
}
return string;
}
public static void writefile( string string, string pfad )
{
try
{
file datafile = new file( pfad );
bufferedwriter writer = new bufferedwriter( new filewriter( datafile ) );
writer.write( string );
writer.close();
}
catch( ioexception e )
{
getlogger().info( e.getmessage() );
}
}
public static void main( string[] args ) throws ioexception
{
file datefile = new file( args[0] );
file parafile = new file( args[1] );
list< list< string >> inputfile = inputfile.fileinput( datefile );
list< list< string >> paramfile = inputfile.fileinput( parafile );
inputfile = inputfile.changevalue( inputfile, paramfile );
string string = inputfile.fileoutput( inputfile );
inputfile.writefile( string, args[2] );
}
}
class myloghander extends formatter
{
public string format( logrecord record )
{
date date = new date();
simpledateformat sdf = new simpledateformat( "yyyy/mm/dd hh:mm:ss" );
return sdf.format( date ) + ":" + record.getmessage() + "n";
}
}
%>
</body>
</html>
from:
http://www.111cn.net/jsp/Java/csv.htm