当前位置:网站首页>Text replacement demo
Text replacement demo
2022-07-03 22:29:00 【User 2700206】
package com.na.ip;
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.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws UnknownHostException, IOException {
String local = "localhost";
String server = "baseserver";
String ip = getLocalIp();
String baseserver = "";
String folder = getJarDir();
System.out.println(" The current path is :" + folder);
System.out.println(" This machine ip by :" + ip);
System.out.println(" Whether it is right (Y/N)?");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
if ("Y".equalsIgnoreCase(str)) {
} else {
System.out.println(" Enter this machine IP:");
sc = new Scanner(System.in);
str = sc.nextLine();
ip = str;
}
System.out.println(" Input basic service machine IP( Multiple needs , separate ):");
sc = new Scanner(System.in);
str = sc.nextLine();
baseserver = str;
if (args != null && args.length > 0 && args[0].equalsIgnoreCase("vip")) {
if (args.length > 2 && args[1].equals("vvip")) {
System.out.println(" Enter source folder path :");
sc = new Scanner(System.in);
str = sc.nextLine();
folder = str;
}
System.out.println(" Enter this machine IP String to replace :");
sc = new Scanner(System.in);
str = sc.nextLine();
local = str;
System.out.println(" Input basic service machine IP String to replace :");
sc = new Scanner(System.in);
str = sc.nextLine();
server = str;
}
System.out.println(" This machine ip by :" + ip + ", Will replace the string :" + local + "; Basic services ip by :" + baseserver + ", Will replace the string :" + server);
System.out.println(" Enter the destination folder path :");
sc = new Scanner(System.in);
str = sc.nextLine();
String dest = str;
File dir = new File(folder);
for (File file : dir.listFiles()) {
if (!file.isDirectory()) {
continue;
}
String folderName = file.getName();
File serverFolder = new File(dest + File.separator + folderName);
if (!serverFolder.exists()) {
serverFolder.mkdirs();
}
for (File configFile : file.listFiles()) {
if (!configFile.isDirectory()) {
continue;
}
String configFolderName = configFile.getName();
File configFolder = new File(serverFolder.getAbsoluteFile() + File.separator + configFolderName);
if (!configFolder.exists()) {
configFolder.mkdirs();
}
for (File config : configFile.listFiles()) {
if (config.isDirectory()) {
continue;
}
String name = config.getName();
File configTxt = new File(configFolder.getAbsoluteFile() + File.separator + name);
System.out.println(" Start replacing " + config.getAbsolutePath() + " The contents of the document :");
if(name.contains("ignite") && baseserver.split(",|,").length > 1
&& server.split(",|,").length == baseserver.split(",|,").length) {
replacTextContent(config.getAbsolutePath(),baseserver.split(",|,"),
server.split(",|,"), configTxt.getAbsolutePath());
replacTextContent(configTxt.getAbsolutePath(),new String[] { local },
new String[] { ip }, configTxt.getAbsolutePath());
}else {
replacTextContent(config.getAbsolutePath(), new String[] { local, server },
new String[] { ip, baseserver }, configTxt.getAbsolutePath());
}
}
}
}
}
// obtain jar Catalog
public static String getJarDir() {
File file = getFile();
if (file == null)
return null;
return getFile().getParent();
}
public static File getFile() {
String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getFile();
try {
path = java.net.URLDecoder.decode(path, "UTF-8");// Convert Chinese and spaces
} catch (java.io.UnsupportedEncodingException e) {
return null;
}
return new File(path);
}
/**
* Replace the string in the text file
*
* @param path
* @throws IOException
*/
public static void replacTextContent(String path, String[] srcStr, String[] replaceStr, String dest){
try {
// read
File file = new File(path);
//FileReader in = new FileReader(file);
BufferedReader bufIn = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
// Memory flow , As a temporary stream
//CharArrayWriter tempStream = new CharArrayWriter();
OutputStreamWriter tempStream = new OutputStreamWriter(new FileOutputStream(new File(dest)),"UTF-8");
// Replace
String line = null;
while ((line = bufIn.readLine()) != null) {
// Replace... In each line , Qualified string
for (int i = 0; i < srcStr.length; i++) {
line = line.replaceAll(srcStr[i], replaceStr[i]);
}
// Write the line to memory
tempStream.write(line);
// Add line breaks
tempStream.append(System.getProperty("line.separator"));
}
// close Input stream
bufIn.close();
// Stream in memory write in file
//FileWriter out = new FileWriter(new File(dest));
//tempStream.writeTo(out);
//out.close();
tempStream.close();
System.out.println(" End of replacement , Generate file path :" + dest);
} catch (Throwable e) {
System.err.println(" Replace "+path+" File failed !");
}
}
public static String getLocalIp() throws UnknownHostException {
InetAddress address = InetAddress.getLocalHost();
// System.out.println(address);// Get the computer name and ip Address
String hostAddress = address.getHostAddress();
// System.out.println(hostAddress);// obtain ip Address
// String hostName = address.getHostName();
// System.out.println(hostName);// Get the computer name
return hostAddress;
}
}Compile operation :
javac -encoding utf-9 Main.java
java Main
边栏推荐
- Pat grade A - 1164 good in C (20 points)
- 2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
- What are the common computer problems and solutions
- Ansible common usage scenarios
- Summary of fluent systemchrome
- Common problems in multi-threaded learning (I) ArrayList under high concurrency and weird hasmap under concurrency
- Some 5000+ likes, the development notes of a director of cosmic factory, leaked
- Conditional statements of shell programming
- Analysis report on the development prospect and investment strategy of global and Chinese modular automation systems Ⓟ 2022 ~ 2027
- How to solve the problem of computer networking but showing no Internet connection
猜你喜欢

To rotate 90 degrees clockwise and modify the video format

Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)

Awk getting started to proficient series - awk quick start

Leetcode week 4: maximum sum of arrays (shape pressing DP bit operation)

How can enterprises and developers take advantage of the explosion of cloud native landing?

The reason why the computer runs slowly and how to solve it

pivot ROP Emporium
![[SRS] build a specified version of SRS](/img/01/0d2d762e01b304220b8924d20277e3.jpg)
[SRS] build a specified version of SRS
![[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)](/img/b8/e2a59772d009b6ee262fb4807f2cd2.jpg)
[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)
![[secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]](/img/55/309c9d52e503405b289bcfc4912be9.jpg)
[secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]
随机推荐
Yyds dry goods inventory Spring Festival "make" your own fireworks
[Android reverse] use DB browser to view and modify SQLite database (download DB browser installation package | install DB browser tool)
The 14th five year plan and investment feasibility study report of China's industry university research cooperation Ⓧ 2022 ~ 2028
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- led lamp module (V)
To rotate 90 degrees clockwise and modify the video format
2022 electrician (elementary) examination questions and electrician (elementary) registration examination
LeetCode 540. A single element in an ordered array
English topic assignment (28)
Rest参考
Yyds dry goods inventory hands-on teach you to create a jigsaw puzzle using the canvasapi
Bluebridge cup Guoxin Changtian single chip microcomputer -- detailed explanation of schematic diagram (IV)
Dynamic research and future planning analysis report of China's urban water supply industry Ⓝ 2022 ~ 2028
320. Energy Necklace (ring, interval DP)
Farmersworld farmers world, no faith, how to talk about success?
[SRS] build a specified version of SRS
The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation
(POJ - 2912) rochambau (weighted concurrent search + enumeration)
[automation operation and maintenance novice village] flask-2 certification
[dynamic planning] counting garlic customers: the log of garlic King (the longest increasing public subsequence)
Data consistency between redis and database