Replace some of the contents of multiple files Demo

Source: Internet
Author: User
Tags readfile

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

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.