Java reads CSV file code <%@ 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 ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>java read CSV file code </title>
<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");
}
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>