2

First, I output the content of the file, here is my code. And then, I will do some string work to edit each line. What if I what to save the changes, how to do it? Can I do it without creating a tmp file?

String executeThis = "cat" + " " + "/var/lib/iscsi/nodes/"
  + iscsiInfo.selectedTargets2.get(i) + "/" + myString + "/default";
String inputThis = "";
Process process = ServerHelper.callProcessWithInput(executeThis, inputThis);

try {        
  logger.debug("stdOutput for editing targets credential:");
  BufferedReader stdOutput = new BufferedReader(
    new InputStreamReader(process.getInputStream()));

  String s = null;        
  while ((s = stdOutput.readLine()) != null) {
    logger.info("The content is@@@@@@@@@@@@@@@@@@@@@@@@"+s)
    // do something to edit each line and update the file
  }        
} catch (IOException e) {
  logger.fatal(e);
}
typeof programmer
  • 1,509
  • 4
  • 22
  • 34

2 Answers2

3

The following steps could achieve what you are looking for.

  • Instantiate a FileWriter object to create a tmp file.

     FileWriter fw = new FileWriter("tmp");
    
  • Read line by line from the source file.

  • Modify this line (string object) in memory.

  • Write out this string in the tmp file.

      fw.write(line);
    
  • Close the file handles.

  • Rename the tmp file to the source file name.

     sourceFile.renameTo(targetFile);
    
Debasis
  • 3,680
  • 1
  • 20
  • 23
2

This question has been answered here. I repeat the answer.

public static void replaceSelected(String replaceWith, String type) {
    try {
        // input the file content to the String "input"
        BufferedReader file = new BufferedReader(new FileReader("notes.txt"));
        String line;String input = "";

        while ((line = file.readLine()) != null) input += line + '\n';

        System.out.println(input); // check that it's inputted right

        // this if structure determines whether or not to replace "0" or "1"
        if (Integer.parseInt(type) == 0) {
            input = input.replace(replaceWith + "1", replaceWith + "0"); 
        }
        else if (Integer.parseInt(type) == 1) {
            input = input.replace(replaceWith + "0", replaceWith + "1");
        } 

        // check if the new input is right
        System.out.println("----------------------------------"  + '\n' + input);

        // write the new String with the replaced line OVER the same file
        FileOutputStream File = new FileOutputStream("notes.txt");
        File.write(input.getBytes());

    } catch (Exception e) {
        System.out.println("Problem reading file.");
    }
}

public static void main(String[] args) {
    replaceSelected("Do the dishes","1");    
}
Community
  • 1
  • 1
Jimmysnn
  • 583
  • 4
  • 8
  • 30
  • Just because this is what the OP asked for doesn't mean it's what he should do. Surgically rewriting each individual line in the file when he knows he's going to be editing them all isn't a recipe for good performance, nor for clean program design. – Tim Jul 18 '14 at 22:07
  • I agree with you. It is not better solution but it is a solution. I just answer his question. But your recommendation is absolutely true!!! – Jimmysnn Jul 18 '14 at 22:20