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 ();
}
}
}