【问题标题】:How to replace a string from a file?如何替换文件中的字符串?
【发布时间】:2013-01-22 10:50:12
【问题描述】:

我刚开始编程,在用 java 创建基本的文件 I/O 程序时遇到了困难。

用例:我想检查文件中的字符串并在同一行附加一个字符串。例如。文件内容如下:

hostname=localhost
port=192

所以,我希望我的程序在上述文件中查找 hostname 字符串,并将 localhost 替换为我传递给它的任何值。

我能够获取文件并将内容传递给临时文件,但不确定如何操作文件中的字符串。非常感谢任何帮助。

【问题讨论】:

  • 你能展示一些你已经拥有的代码吗?
  • 为此使用Properties API。

标签: java string file io


【解决方案1】:

你可以试试String.replace():

String replacement = "you-other-host";

// Read your file line by line...
line = line.replace("localhost", replacement);
// and write the modified line to your temporary file

【讨论】:

    【解决方案2】:

    这里有两种方法(基本没有任何错误/异常处理以及将目标和替换作为参数传递)如何做到这一点。

    如果您的文件存储键/值对而不是最好的方法是用户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();    
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      您将需要使用替换、连接等方法。如果遇到困难,请尝试一些代码并发布!

      http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 2014-10-13
        • 2014-12-25
        • 2012-07-29
        • 1970-01-01
        相关资源
        最近更新 更多