How to read and write Csv files in java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.