Recently, I was working on an embedded Linux server using Java.
In the project, all data should be recorded in log files and stored on the embedded Linux platform. In addition, real-time data should be stored in a table for network query at any time.
We have used txt files for log files, which is the simplest. excel files use jxl, sqlLite embedded databases, and third-party java libraries. The debugging is successful, and the running is not bad. However, we encountered a problem where the memory of the Java Virtual Machine was insufficient. We set the memory allocated by the virtual machine to 32 MB, which solved the problem.
However, the boss believes that the above method is too cumbersome, taking into account both aesthetics and practicality, and using CSV files as log files.
I checked and summarized the methods on the network. There are two main methods to read and write CVS files: Borrow a third-party library and use the FileWriter class.
1. Use third-party libraries.
Currently, third-party libraries mainly include opencsv and javacsv. It seems that there is also supercsv, but they are similar.
2. Use FileWriter.
This is my final method, which is easy to use. You don't need to add a library, add a sample code, and share it with you.
Import java. io. FileWriter;
Import java. io. IOException;
Public class javacsv {
/**
* Write csv files
*/
Public static void main (String [] args ){
Try {
// If this file does not exist, it will be automatically created
// If this file is available, it will be continued at the end of the file
FileWriter fw = new FileWriter ("E: \ youyouTest.csv", true );
Fw. write ("aaa, bbb, ccc, ddd, eee, fff, ggg, hhh \ n ");
Fw. write ("aaa, bbb, ccc, ddd, eee, fff, ggg, hhh \ n ");
Fw. write ("aa1, bb1, PC3, dd1, ee1, ff1, gg1, hh1 \ n ");
Fw. write ("aaa \ n ");
Fw. write ("aa2, bb2, cc2, dd2, ee2, ff2, gg2, hh2 \ n ");
Fw. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
The above methods are used in actual projects and are recorded for your reference.
Author: youhaipeng