Java 如何替换文件中的字符串?
我刚刚开始编程,在用java创建基本文件I/O程序时遇到了麻烦 用例:我想检查文件中的字符串,并在同一行中追加一个字符串。例如,文件内容如下:Java 如何替换文件中的字符串?,java,string,file,io,Java,String,File,Io,我刚刚开始编程,在用java创建基本文件I/O程序时遇到了麻烦 用例:我想检查文件中的字符串,并在同一行中追加一个字符串。例如,文件内容如下: hostname=localhost port=192 因此,我希望我的程序在上面的文件中查找hostname字符串,并用传递给它的值替换localhost 我能够获取文件并将内容传递到临时文件,但不确定如何操作文件中的字符串。非常感谢您的帮助。您可以尝试String.replace(): 您需要使用replace、concat等方法。尝试一些代码和张
hostname=localhost
port=192
因此,我希望我的程序在上面的文件中查找hostname
字符串,并用传递给它的值替换localhost
我能够获取文件并将内容传递到临时文件,但不确定如何操作文件中的字符串。非常感谢您的帮助。您可以尝试
String.replace()
:
您需要使用replace、concat等方法。尝试一些代码和张贴,如果你卡住了 这里有两种方法(基本无任何错误/异常处理和将目标和替换作为参数传递)可以实现这一点 如果您的文件存储键/值对,那么最好的方法是使用user
java.util.Properties
public class ReplaceInFile {
private final static String src = "test.txt";
private final static String dst_str = "test_new_str.txt";
private final static String dst_prop = "test_new_prop.txt";
public static void main(String[] args) throws IOException {
usingStringOperations();
usingProperties();
}
private static void usingProperties() throws IOException {
File srcFile = new File(src);
FileInputStream fis = new FileInputStream(srcFile);
Properties properties = new Properties();
properties.load(fis);
fis.close();
if(properties.getProperty("hostname") != null) {
properties.setProperty("hostname", "127.0.0.1");
FileOutputStream fos = new FileOutputStream(dst_prop);
properties.store(fos, "Using java.util.Properties");
fos.close();
}
}
private static void usingStringOperations() throws IOException {
File srcFile = new File(src);
FileInputStream fis = new FileInputStream(srcFile);
int len = fis.available();
if(len > 0) {
byte[] fileBytes = new byte[len];
fis.read(fileBytes, 0, len);
fis.close();
String strContent = new String(fileBytes);
int i = strContent.indexOf("localhost");
if(i != -1) {
String newStrContent = strContent.substring(0, i) +
"127.0.0.1" +
strContent.substring(i + "localhost".length(), strContent.length());
FileOutputStream fos = new FileOutputStream(dst_str);
fos.write(newStrContent.getBytes());
fos.close();
}
}
}
}
你能展示一些你已经拥有的代码吗?使用API来实现这一点。
public class ReplaceInFile {
private final static String src = "test.txt";
private final static String dst_str = "test_new_str.txt";
private final static String dst_prop = "test_new_prop.txt";
public static void main(String[] args) throws IOException {
usingStringOperations();
usingProperties();
}
private static void usingProperties() throws IOException {
File srcFile = new File(src);
FileInputStream fis = new FileInputStream(srcFile);
Properties properties = new Properties();
properties.load(fis);
fis.close();
if(properties.getProperty("hostname") != null) {
properties.setProperty("hostname", "127.0.0.1");
FileOutputStream fos = new FileOutputStream(dst_prop);
properties.store(fos, "Using java.util.Properties");
fos.close();
}
}
private static void usingStringOperations() throws IOException {
File srcFile = new File(src);
FileInputStream fis = new FileInputStream(srcFile);
int len = fis.available();
if(len > 0) {
byte[] fileBytes = new byte[len];
fis.read(fileBytes, 0, len);
fis.close();
String strContent = new String(fileBytes);
int i = strContent.indexOf("localhost");
if(i != -1) {
String newStrContent = strContent.substring(0, i) +
"127.0.0.1" +
strContent.substring(i + "localhost".length(), strContent.length());
FileOutputStream fos = new FileOutputStream(dst_str);
fos.write(newStrContent.getBytes());
fos.close();
}
}
}
}