java 什么工具类 字符串传入null 返回空字符串,传入字符串返回原字符串
时间: 2023-08-14 10:07:21 浏览: 48
以下是一个 Java 工具类实现,可以满足你的需求:
```java
public class StringUtils {
public static String nullToEmpty(String str) {
return str == null ? "" : str;
}
public static String emptyToNull(String str) {
return str == null || str.isEmpty() ? null : str;
}
}
```
其中,nullToEmpty 方法用于将传入的字符串 str 如果为 null,则返回空字符串 "",否则返回原字符串;emptyToNull 方法则用于将传入的字符串 str 如果为空字符串或为 null,则返回 null,否则返回原字符串。你可以根据需要选择其中的一个方法使用。
相关问题
用java语言实现对文件中的某个字符串进行替换写一个工具类
好的,我可以回答这个问题。以下是一个用 Java 语言实现对文件中的某个字符串进行替换的工具类的示例代码:
```java
import java.io.*;
public class FileStringReplacer {
public static void replaceStringInFile(String filePath, String oldString, String newString) {
try {
File file = new File(filePath);
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "", content = "";
while ((line = reader.readLine()) != null) {
content += line + "\n";
}
reader.close();
content = content.replaceAll(oldString, newString);
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(content);
writer.close();
System.out.println("替换完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
你可以调用 `replaceStringInFile` 方法,传入文件路径、要替换的字符串和替换后的字符串,即可实现对文件中的某个字符串进行替换。
使用java语言实现对目录及子目录中的所以文件进行字符串替换写一个工具类
好的,这是一个很好的编程类问题。以下是一个示例 Java 工具类,它可以递归地遍历目录及其子目录中的所有文件,并将指定的字符串替换为新的字符串:
```
import java.io.*;
public class StringReplaceTool {
public static void replaceInDirectory(File directory, String oldStr, String newStr) {
if (directory.isDirectory()) {
File[] files = directory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
replaceInDirectory(file, oldStr, newStr);
} else {
replaceInFile(file, oldStr, newStr);
}
}
}
}
public static void replaceInFile(File file, String oldStr, String newStr) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "", content = "";
while ((line = reader.readLine()) != null) {
content += line.replaceAll(oldStr, newStr) + "\n";
}
reader.close();
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
要使用此工具类,您可以调用 `replaceInDirectory` 方法并传入要替换的目录、旧字符串和新字符串。例如:
```
File directory = new File("/path/to/directory");
String oldStr = "foo";
String newStr = "bar";
StringReplaceTool.replaceInDirectory(directory, oldStr, newStr);
```
这将递归地遍历 `/path/to/directory` 目录及其子目录中的所有文件,并将所有出现的 "foo" 字符串替换为 "bar" 字符串。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)