I recently integrated the extraction of information from the text, save it in the new text, used here is a regular expression, I hope you learn together.
File Operation class: Fileoperation.java
Package dyx_13;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.OutputStreamWriter;
public class FileOperation {
Create a file to detect whether a file is created
public boolean cratefile (File fileName) {
Boolean flag = false;
try {
if (!filename.exists ()) {
Filename.createnewfile ();//file does not appear create new file
Flag = true;
}
}catch (Exception e) {
E.printstacktrace ();
}
return true;
}
Read TXT file
public static String Readtxtfile (File fileName) {
StringBuffer str = new StringBuffer ();
String result = null;
FileReader FileReader = null;//Read file
BufferedReader bufferreader = null;//Read buffer stream
try {
FileReader = new FileReader (fileName);
Bufferreader = new BufferedReader (FileReader);
try {
For (String, line = Bufferreader.readline ())!=null;) {
Str.append (line) append ("\ n");
}
result = Str.tostring ();
}catch (Exception e) {
E.printstacktrace ();
}
}catch (Exception e) {
E.printstacktrace ();
}
System.out.println ("Read the content is:" + result);
return result;
}
Determine file permissions
public boolean writetxtfile (String content, File fileName) {
Randomaccessfile mm = null;
Boolean flag = false;
FileOutputStream o = null;
try{
o = new FileOutputStream (filename);//File to Output
O.write (Content.getbytes ("GBK"));
O.close ();
Flag = true;
}catch (Exception e) {
E.printstacktrace ();
}
return flag;
}
Write file
public static void Write (string filePath, String content) throws IOException {
File File2 = new file (filePath);//Get Path
BufferedWriter out = null;
try {
out = new BufferedWriter (New OutputStreamWriter (
New FileOutputStream (File2, true));
Out.write (content+ "\ r \ n");
} catch (Exception e) {
E.printstacktrace ();
} finally {
try {
Out.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
Clear text message
public static void Clear (String path) throws IOException {
File F = new file (path);
FileWriter FW = new FileWriter (f);
Fw.write ("");
Fw.close ();
}
public static void Contenttotxt (String filePath1, String filePath2) {
StringBuffer str = new StringBuffer (); Original TXT file
string S1 = new string ();//newly added content
try {
File File1 = new file (filePath1);//Get Path
if (file1.exists ()) {
System.out.println ("Source file exists");
} else {
System.out.println ("Source file does not exist");
File1.createnewfile ();//does not exist create file
}
File File2 = new file (filePath2);//Get Path
if (file2.exists ()) {
SYSTEM.OUT.PRINTLN ("Destination file exists");
} else {
SYSTEM.OUT.PRINTLN ("The target file does not exist");
File1.createnewfile ();//does not exist create file
}
BufferedReader input = new BufferedReader (new FileReader (File1));
For (String, line = Input.readline ())!=null;) {
Str.append (line) append ("\ n");
}
S1 = str.tostring ();
String emailstr = Regexp.email (S1);
String telestr = Regexp.tele (S1);
String netaddstr = regexp.netaddress (S1);
String idstr = Regexp.idcard (S1);
System.out.println (S1);
Input.close ();
BufferedWriter output = new BufferedWriter (new FileWriter (file2));
Output.write (EMAILSTR);
Output.write (telestr+ "\ n");
Output.write (netaddstr+ "\ n");
Output.write (idstr+ "\ n");
Output.close ();
}catch (Exception e) {
E.printstacktrace ();
}
}
}
Regular expression processing: Regexp.java
Package dyx_13;
Import java.io.IOException;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class RegExp {
public static string e-mail (string filepath,string content) throws IOException {
String regex = "\\w+@\\w+ (\\.\\w+) +";
String email = null;
Matcher m=pattern.compile (regex). Matcher (content);
while (M.find ()) {
email = m.group ();
Fileoperation.write (filePath, email);
SYSTEM.OUT.PRINTLN (email);
}
return email;
}
Phone number
public static string Tele (string filepath,string content) throws IOException {
String telephone = null;
String regex = "0?" (13[0-9]|15[012356789]|18[0236789]|14[57]) [0-9] {8} ";
Matcher m=pattern.compile (regex). Matcher (content);
while (M.find ()) {
Telephone = M.group ();
Fileoperation.write (FilePath, telephone);
SYSTEM.OUT.PRINTLN (telephone);
}
return telephone;
}
Url
public static string Netaddress (string filepath,string content) throws IOException {
String netadd = null;
String regex = "(https?| Ftp|file)://[-a-za-z0-9+&@#/%?=~_|!:,.;] *[-a-za-z0-9+&@#/%=~_|] ";
Matcher m=pattern.compile (regex). Matcher (content);
while (M.find ()) {
Netadd = M.group ();
Fileoperation.write (FilePath, Netadd);
System.out.println (Netadd);
}
return netadd;
}
Id
public static string Idcard (string filepath,string content) throws IOException {
String id = null;
String regex = "(\\d{14}\\w) |\\d{17}\\w";
Matcher m=pattern.compile (regex). Matcher (content);
while (M.find ()) {
id = m.group ();
Fileoperation.write (FilePath, id);
SYSTEM.OUT.PRINTLN (ID);
}
return ID;
}
}
Run the main program Zhu.java
Package dyx_13;
Import Java.io.File;
Import java.io.IOException;
public class Zhu {
/**
* @param args
* @throws IOException
*/
public static void Main (string[] args) throws IOException {
TODO auto-generated Method Stub
O.contenttotxt (s1, S2); FileOperation o = new FileOperation ();
String S1 = "D:\\java Workspace\\dyx\\java class\\dyx_2\\content\\file.txt";
String s2 = "D:\\java Workspace\\dyx\\java class\\dyx_2\\content\\outfile.txt";
Fileoperation.clear (S2);
File F = new file (S1);
String s = fileoperation.readtxtfile (f);
System.out.println (s);
Regexp.email (s2,s);
Regexp.tele (s2,s);
Regexp.netaddress (s2,s);
Regexp.idcard (s2,s);
}
}
Use regular expressions to read the TXT file in the mailbox, phone number, URL address, mobile phone number, one row to save the data to a new file