Java processes regular Chinese Text: add some strings to each line

Source: Internet
Author: User

Processing files in any language is very important. Yesterday, the project encountered a problem: the product sent a doc with 300 lines of text. It's messy. Any xml mark or unicode character. You must randomly display a row in the project at intervals. I discussed it with my colleagues, made an xml file separately, and gave each line of strings a regular name. It would be nice to process it in the android program. The basic process is as follows:
1. First test the text from word to Notepad ~.
2. Remove irrelevant characters using regular expressions in the eclpse editor. Roll all tabs, angle brackets, and underscores!
3. Write a java program to read a file in one row. After <string name = "xxx"> content </string> is added, it is OK to write one row to another file!

 

 

I encountered a very annoying problem. When FileOutputStream and BufferedWriter are used together, half of the files are written, and the program ends, I will go!
As a result, the length of the BufferedWriter file is limited. OK. I directly called the write method of FileOutputStream to solve this bug!

Source code:

 

[Java]
Import java. io .*;
 
Class addsomething
{
Public static void main (String [] args)
{

String fileName = "";
System. out. println ("int file name :");


InputStreamReader ir = null;
BufferedReader bf = null;
String lines = "";
StringBuffer sb = new StringBuffer ();
OutputStreamWriter osw = null;
BufferedWriter bw = null;
Try
{
Ir = new InputStreamReader (System. in );
Bf = new BufferedReader (ir );
String name = bf. readLine ();
System. out. println ("filename:" + name );
Ir = new InputStreamReader (new FileInputStream (name), "UTF-8 ");
Bf = new BufferedReader (ir );
System. out. println ("sdfsadfsadf ");
Int count = 0;
Osw = new OutputStreamWriter (new FileOutputStream (name + ". txt "),
UTF-8 ");
Bw = new BufferedWriter (osw); // This object is not used.

System. out. println ("-0 -------------------------> ");

// Read the file in one row and write it again. Use the osw write method. Instead of bw
While (null! = (Lines = bf. readLine ()))
{
System. out. println (lines );
Osw. write ("<string name = \" proverb "+ count +" \ ">"
+ Lines + "</string> \ n ");
Count ++;
System. out. println ("cout->" + count );
}
// WriteString (name + ". txt", sb );
Ir. close ();
Bf. close ();
Osw. close ();
}
Catch (Exception ex)
{
Ex. printStackTrace ();

}
}
 
}

Import java. io .*;

Class addsomething
{
Public static void main (String [] args)
{
 
String fileName = "";
System. out. println ("int file name :");
 

InputStreamReader ir = null;
BufferedReader bf = null;
String lines = "";
StringBuffer sb = new StringBuffer ();
OutputStreamWriter osw = null;
BufferedWriter bw = null;
Try
{
Ir = new InputStreamReader (System. in );
Bf = new BufferedReader (ir );
String name = bf. readLine ();
System. out. println ("filename:" + name );
Ir = new InputStreamReader (new FileInputStream (name), "UTF-8 ");
Bf = new BufferedReader (ir );
System. out. println ("sdfsadfsadf ");
Int count = 0;
Osw = new OutputStreamWriter (new FileOutputStream (name + ". txt "),
UTF-8 ");
Bw = new BufferedWriter (osw); // This object is not used.

System. out. println ("-0 -------------------------> ");

// Read the file in one row and write it again. Use the osw write method. Instead of bw
While (null! = (Lines = bf. readLine ()))
{
System. out. println (lines );
Osw. write ("<string name = \" proverb "+ count +" \ ">"
+ Lines + "</string> \ n ");
Count ++;
System. out. println ("cout->" + count );
}
// WriteString (name + ". txt", sb );
Ir. close ();
Bf. close ();
Osw. close ();
}
Catch (Exception ex)
{
Ex. printStackTrace ();

}
}

}


 

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.