Reprint please specify the source!
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.util.LinkedList;
/**
* Replace some of the contents of multiple files in a folder
*
*/
public class Replacemultiplefileparts {
public static void Main (string[] args) throws Exception {
Destination Folder
File directory = new file ("c:/users/lyj/desktop/new Folder");
Get line breaks for the system
String lineseparator = System.getproperty ("Line.separator", "/n");
String FilePath = "";
Read all Files under folder
int filenum = 0, foldernum = 0;
if (Directory.Exists ()) {
linkedlist<file> list = new linkedlist<file> ();
file[] files = directory.listfiles ();
for (File tempfile:files) {
if (Tempfile.isdirectory ()) {
System.out.println ("folder:" + Tempfile.getabsolutepath ());
List.add (Tempfile);
filenum++;
} else {
Get not a file
FilePath = Tempfile.getabsolutepath ();
System.out.println ("File:" + FilePath);
Process (FilePath, Tempfile.getname (), lineseparator);
foldernum++;
}
}
} else {
SYSTEM.OUT.PRINTLN ("file does not exist!");
}
System.out.println ("folder total:" + foldernum + ", file total:" + filenum);
}
/**
* Processing Process
*
* @param FilePath
* @param fileName
* @param lineseparator
* @throws IOException
*/
public static void process (string filePath, String fileName,
String lineseparator) throws IOException {
BufferedReader in = new BufferedReader (New InputStreamReader (
New FileInputStream (FilePath), "UTF-8");
try {
File read
String str = ReadFile (in, lineseparator);
Content substitution
str = replacefilecontents (str);
Output a new file
Writenewfile (FileName, str);
} catch (Exception e) {
E.printstacktrace ();
} finally {
In.close ();
}
}
/**
* Read all Files under folder
*
* @param path
*/
public static void Traversefolder (String path) {
int filenum = 0, foldernum = 0;
File File = new file (path);
if (file.exists ()) {
linkedlist<file> list = new linkedlist<file> ();
file[] files = file.listfiles ();
for (File tempfile:files) {
if (Tempfile.isdirectory ()) {
System.out.println ("folder:" + Tempfile.getabsolutepath ());
List.add (Tempfile);
filenum++;
} else {
System.out.println ("File:" + Tempfile.getabsolutepath ());
foldernum++;
}
}
} else {
SYSTEM.OUT.PRINTLN ("file does not exist!");
}
System.out.println ("folder total:" + foldernum + ", file total:" + filenum);
}
/**
* File read
*
* @param in
* @param lineseparator
* @return
* @throws IOException
*/
public static string ReadFile (BufferedReader in, string lineseparator)
Throws IOException {
String line = null;
StringBuffer linebuffer = new StringBuffer ();
while (line = In.readline ()) = null) {
Linebuffer.append (line + lineseparator);
}
System.out.println (Linebuffer.tostring ());
String str = linebuffer.tostring ();
return str;
}
/**
* Replace content
*
* @param str
* @return
*/
public static string replacefilecontents (String str) {
str = Str.replaceall (regex, replacement);
return str;
}
/**
* Output File
*
* @param fileName
* @param str
* @throws IOException
*/
public static void Writenewfile (string fileName, String str)
Throws IOException {
Path to output folder
File txt = new file ("c:/users/lyj/desktop/new Folder 2/" + FileName);
if (!txt.exists ()) {
Txt.createnewfile ();
}
byte bytes[] = new byte[1024];
bytes = Str.getbytes (); New Plus.
int b = bytes.length; Change
System.out.println (b);
FileOutputStream fos = new FileOutputStream (TXT);
Fos.write (bytes, 0, b);
Fos.close ();
}
}
Replace some of the contents of multiple files Demo