当前位置:网站首页>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
边栏推荐
- Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
- Harbor integrated LDAP authentication
- The overseas listing of Shangmei group received feedback, and brands such as Han Shu and Yiye have been notified for many times and received attention
- Report on the development strategy of China's engineering bidding agency and suggestions for the 14th five year plan Ⓙ 2022 ~ 2028
- Some 5000+ likes, the development notes of a director of cosmic factory, leaked
- Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
- Can you draw with turtle?
- China's Call Center Industry 14th five year plan direction and operation analysis report Ⓔ 2022 ~ 2028
- LeetCode 1646. Get the maximum value in the generated array
- How to connect a laptop to a projector
猜你喜欢

Harbor integrated LDAP authentication

Preliminary analysis of smart microwave radar module

Mysql database - Advanced SQL statement (I)
![[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list](/img/db/b992d2b461ca17652518a1511b4947.gif)
[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list

How to switch between dual graphics cards of notebook computer

Cesium terrain clipping draw polygon clipping
![[actual combat record] record the whole process of the server being attacked (redis vulnerability)](/img/9c/34b916aca2f9270ec4cf4651f0de7e.jpg)
[actual combat record] record the whole process of the server being attacked (redis vulnerability)

Teach you to easily learn the type of data stored in the database (a must see for getting started with the database)
![[Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)](/img/6e/3ffa91154a718b6ace6c8ca87c5995.jpg)
[Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)

2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
随机推荐
Es6~es12 knowledge sorting and summary
Supply and demand situation and market scale calculation report of China's portable energy storage power PES industry Ⓛ 2022 ~ 2028
Overview of Yunxi database executor
The overseas listing of Shangmei group received feedback, and brands such as Han Shu and Yiye have been notified for many times and received attention
Summary of fluent systemchrome
How can enterprises and developers take advantage of the explosion of cloud native landing?
Cesium terrain clipping draw polygon clipping
On my first day at work, this API timeout optimization put me down!
How the computer flushes the local DNS cache
LeetCode 540. A single element in an ordered array
Correlation
Programming language (1)
Blue Bridge Cup -- Mason prime
Mindmanager2022 serial number key decompression installer tutorial
Why should enterprises do more application activities?
Buuctf, misc: sniffed traffic
Yyds dry goods inventory Spring Festival "make" your own fireworks
WiFi 2.4g/5g/6g channel distribution
Leetcode: a single element in an ordered array
Wisdom tooth technology announced that it had completed the round D financing of US $100million and had not obtained a valid patent yet