Various java implementation programs for reading and writing text files

Source: Internet
Author: User
Tags flush readline regular expression table name stringbuffer

First look at the simplest java reading text files by line? The code is as follows:

The code is as follows: Copy code

FileReader reader = new FileReader ("D: \ www.111cn.net \ feisanWeb \ src \ 265_url.txt ");
BufferedReader br = new BufferedReader (reader );
String s1 = null;
While (s1 = br. readLine ())! = Null ){
  }
Br. close ();
Reader. close ();


The following operations are performed in combination with the database:

However, you cannot import data at the time of import. After viewing the data, you will find that all data formats do not contain the table name.

The code is as follows: Copy code

Insert into (id, stat_date, referrer, func, catid, user_type, action_stat, channel) VALUES (240092, "2012-3-6", www.111cn.net,-10,116 11,-10, 65: 4.0; 57: 1.0; 52: 1.0 ","-10 ");

Insert into (id, stat_date, referrer, func, catid, user_type, action_stat, channel) VALUES (240093, "2012-3-6", "baidu.com",-1, 10587,-10, "65: 273.0", "-10 ");


A total of more than 60 thousand entries. If it takes a long time to add a table name one by one, we can use SQL to batch insert the value of VALUES;

According to the form of SQL script, we only need to replace the ";" following the brackets of VALUES with a comma. The final form is

The code is as follows: Copy code

(240092, "2012-3-6", "renren.com",-10,116 11,-10, "65: 4.0; 57: 1.0; 52: 1.0", "-10 "),
(240093, "2012-3-6", "baidu.com",-1, 10587,-10, "65: 273.0", "-10 "),
(240094, "2012-3-6", "baidu.com",-1, 10589,-10, "65: 1040.0; 57: 6.0; 52: 10.0", "-10 "),
(240095, "2012-3-6", "qq.com",-10,119 13,-10, "65: 1.0", "channelId_846 "),
(240096, "2012-3-6", "baidu.com",-1, 10581,-10, "65: 1423.0; 57: 5.0; 52: 5.0", "-10 "),
(240097, "2012-3-6", "baidu.com",-1, 10583,-10, "57: 20.0; 52: 23.0; 65: 2823.0", "-10 "),

In this way, we only need to add

The code is as follows: Copy code

Insert into t_xxx (id, stat_date, referrer, func, catid, user_type, action_stat, channel)
VALUES


You can insert databases in batches. Below is the JavaIO processing program;

   

The code is as follows: Copy code
Public static void main (String [] args) throws Exception {
BufferedReader in = new BufferedReader (new FileReader ("D: \ SQL. SQL"); // The text file to be read
BufferedWriter br = new BufferedWriter (new FileWriter ("D: \ aaa. SQL"); // output result file
String s = "";
Pattern pattern = Pattern. compile (". * [)] [;]"); // regular expression matches the end of an SQL statement.
Matcher m = null;
Int I = 1;
While (s = in. readLine ())! = Null ){
M = pattern. matcher (s );
If (m. matches ()){
System. out. println (I ++ );
S = s. replace (");", "),"); // replace
             }
If (! S. matches ("*") {// removes blank lines
Br. write (s );
Br. write ("n ");
             }
         }
// Close
In. close ();
Br. flush ();
Br. close ();
    }


Fast execution speed, less than 2 seconds;


/*
* Simple example of reading/writing text files
* There are three examples:
* 1.
* 2.
* 3. Read the content of one file and write it to another file.
* It also shows that if the content read from the input stream is written to the output stream (text stream only)
* The three examples can exist independently, so you can only view one of them as needed.
*/

The code is as follows: Copy code

Import java. io. BufferedReader;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. InputStreamReader;
Import java. io. OutputStream;
Import java. io. OutputStreamWriter;
Import java. io. PrintStream;
Import java. io. PrintWriter;


Example of reading a file into memory (StringBuffer)
Public final class AccessTextFile {

/**
* 1. Read the text in the stream into a StringBuffer.
* @ Throws IOException
*/
Public void readToBuffer (StringBuffer buffer, InputStream is)
Throws IOException {
String line; // used to save the content read by each row
BufferedReader reader = new BufferedReader (new InputStreamReader (is ));
Line = reader. readLine (); // read the first row
While (line! = Null) {// if line is null, the read is complete.
Buffer. append (line); // add the read content to the buffer.
Buffer. append ("n"); // add a line break
Line = reader. readLine (); // read the next row
        }
    }

/**
* 2. Demonstrate reading the content in StringBuffer into the stream
*/


Public void writeFromBuffer (StringBuffer buffer, OutputStream OS ){
// Use PrintStream to conveniently output content to the output stream
// Its object usage is the same as that of System. out.
// (System. out itself is a PrintStream object)
PrintStream ps = new PrintStream (OS );
Ps. print (buffer. toString ());
    }


/**
* 3 *. Copy the content from the input stream to the input stream.
* @ Throws IOException
*/


Public void copyStream (InputStream is, OutputStream OS) throws IOException {
// For the read process, see the comments in readToBuffer.
String line;
BufferedReader reader = new BufferedReader (new InputStreamReader (is ));
PrintWriter writer = new PrintWriter (new OutputStreamWriter (OS ));
Line = reader. readLine ();
While (line! = Null ){
Writer. println (line );
Line = reader. readLine ();
        }
Writer. flush ();
                           
    }

This is the case for text file operations. If you have any questions, refer to the following section.

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.