English 中文(简体)
Java - Load文档,取代护卫,但
原标题:Java - Load file, replace string, save

我有一个从用户档案中装上线的方案,然后选择了“Sting”的最后部分(即刻)。

这里用的是:

nameOfValue = 0
nameOfValue2 = 0

等等。 我选择了有把握的价值——我用印刷把这一价值 de倒。 我似乎无法挽救它。

if(nameOfValue.equals(type)) {
        System.out.println(nameOfValue+" equals "+type);
            value.replace(value, Integer.toString(Integer.parseInt(value)+1));
        }

我怎么会想? 我曾尝试过缓冲作家,但只是抹去了档案中的一切。

最佳回答

我的建议是,除了原始档案的所有内容(无论是记忆中还是临时档案中的内容;我是记忆中的内容),然后再次书写,包括修改内容。 我认为这项工作将:

public static void replaceSelected(File file, String type) throws IOException {

    // we need to store all the lines
    List<String> lines = new ArrayList<String>();

    // first, read the file and store the changes
    BufferedReader in = new BufferedReader(new FileReader(file));
    String line = in.readLine();
    while (line != null) {
        if (line.startsWith(type)) {
            String sValue = line.substring(line.indexOf( = )+1).trim();
            int nValue = Integer.parseInt(sValue);
            line = type + " = " + (nValue+1);
        }
        lines.add(line);
        line = in.readLine();
    }
    in.close();

    // now, write the file again with the changes
    PrintWriter out = new PrintWriter(file);
    for (String l : lines)
        out.println(l);
    out.close();

}

并且,你把这种方法称为一种方法,提供你希望修改的文件以及你希望选择的价值名称:

replaceSelected(new File("test.txt"), "nameOfValue2");
问题回答

我认为最方便的方式是:

  1. Read text file line by line using BufferedReader
  2. For each line find the int part using regular expression and replace it with your new value.
  3. Create a new file with the newly created text lines.
  4. Delete source file and rename your new created file.

请允许我知道,是否有必要实施 above方案。

如果没有完整的法典,就无法回答......

Is value a string ? If so the replace will create a new string but you are not saving this string anywhere. Remember Strings in Java are immutable.

你们说的是布莱德·威里特,你们是不是一帆风顺的? 这往往导致在应该存在时刻意消失的价值观。 这正是 Java最后关键词的原因。

Also difficult to answer without more details on your problem, what exactly are you trying to acheive ? There may be simpler ways to do this that are already there.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签