Difference between revisions of "Insert a line in a file"

From CodeCodex

(Ruby)
 
Line 4: Line 4:
  
 
In this example, you need to supply 3 arguments : the filename, a line number and the string to be inserted at the line number specified.  
 
In this example, you need to supply 3 arguments : the filename, a line number and the string to be inserted at the line number specified.  
<highlightsyntax language="java122">
+
<pre class="java">
 
java JInsert test.out 9 "HELLO WORLD"
 
java JInsert test.out 9 "HELLO WORLD"
</highlightsyntax>
+
</pre>
 
will insert the string "HELLO WORLD" at line number 9 in the file "test.out".
 
will insert the string "HELLO WORLD" at line number 9 in the file "test.out".
  
 
of course you need more error checking...  
 
of course you need more error checking...  
<highlightsyntax language="java122">
+
<pre class="java">
 
[JDK1.1]
 
[JDK1.1]
 
import java.io.*;
 
import java.io.*;
Line 56: Line 56:
 
     }
 
     }
 
}
 
}
</highlightsyntax>
+
</pre>
  
 
===Ruby===
 
===Ruby===
<pre>
+
<pre class="ruby">
 
require 'tempfile'
 
require 'tempfile'
  

Latest revision as of 17:55, 13 January 2011

Implementations[edit]

Java[edit]

The only way to insert a line in a text file is to read the original file and write the content in a temporary file with the new line inserted. Then we erase the original file and rename the temporary file to the original name.

In this example, you need to supply 3 arguments : the filename, a line number and the string to be inserted at the line number specified.

java JInsert test.out 9 "HELLO WORLD"

will insert the string "HELLO WORLD" at line number 9 in the file "test.out".

of course you need more error checking...

[JDK1.1]
import java.io.*;

 public class JInsert {
   public static void main(String args[]){
     try {
       JInsert j = new JInsert();
       j.insertStringInFile
          (new File(args[0]),Integer.parseInt(args[1]), args[2]);
       }
     catch (Exception e) {
       e.printStackTrace();
       }
     }

   public void insertStringInFile
         (File inFile, int lineno, String lineToBeInserted) 
       throws Exception {
     // temp file
     File outFile = new File("$$$$$$$$.tmp");
     
     // input
     FileInputStream fis  = new FileInputStream(inFile);
     BufferedReader in = new BufferedReader
         (new InputStreamReader(fis));

     // output         
     FileOutputStream fos = new FileOutputStream(outFile);
     PrintWriter out = new PrintWriter(fos);

     String thisLine = "";
     int i =1;
     while ((thisLine = in.readLine()) != null) {
       if(i == lineno) out.println(lineToBeInserted);
       out.println(thisLine);
       i++;
       }
    out.flush();
    out.close();
    in.close();
    
    inFile.delete();
    outFile.renameTo(inFile);
    }
}

Ruby[edit]

require 'tempfile'

def insert_string_in_file(filename, line_no, insert_str)
  work = Tempfile.open('temp') do |ftemp|
    open(filename,'r') do |fin| 
      num = 0
      while line = fin.gets
        num += 1
        ftemp.puts insert_str  if num == line_no
        ftemp.puts line
      end
      ftemp.puts insert_str  if num < line_no
    end
    ftemp
  end
  FileUtils.copy work, filename
  work.delete
end