[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Very simple: delete a line or a string at a line in a text file

For example, I have a text file:
---------------------------
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
--------------------------
And I want to delete line4. Then the content of the text file will be:
---------------------------
This is line 1
This is line 2
This is line 3
This is line 5
This is line 6
--------------------------
And I want to replace the string "line 5" at  line 4 with the string "line 4" . The content of the text file will be:
---------------------------
This is line 1
This is line 2
This is line 3
This is line 4
This is line 6
--------------------------
Thank you.
0
maidinhtai
Asked:
maidinhtai
  • 2
2 Solutions
 
CEHJCommented:
You can use the following convenience class thus:


StringList sl = new StringList();
sl.read("yourfile.txt");
// I want to delete line4
sl.remove(3);
// I want to replace the string "line 5" at  line 4 with the string "line 4"
sl.set(3, "line 4");
// Save it
sl.save("yourfile.txt");


import java.io.*;
import java.util.ArrayList;

/**
 *      A utility class the allows a text file to be
 *      treated as a collection of Strings
 *
 * @author             CEHJ
 * @created            29 February 2004
 */
public class StringList extends ArrayList {

      /**
       *Constructor for the StringList object
       */
      public StringList() {
            super();
      }

      public void read(InputStream in) {
            read(new InputStreamReader(in));
      }


      public void read(Reader r) {
            String line = null;
            BufferedReader in = null;
            try {
                  in = new BufferedReader(r);
                  while ((line = in.readLine()) != null) {
                        add(line);
                  }
            }
            catch (IOException e) {
                  e.printStackTrace();
            }
            finally {
                  try {
                        in.close();
                  }
                  catch (IOException e) {
                        e.printStackTrace();
                  }
            }

      }

      /**
       *      Constructor for the StringList object
       *
       * @param      fileName      The file to open
       */
      public void read(String fileName) {
            try {
                  read(new FileReader(fileName));
            }
            catch(IOException e) {
                  e.printStackTrace();
            }
      }

      /**
       *      Save the String to named file
       *
       * @param      fileName      The name of the file to save to
       */
      public void save(String fileName) {
            PrintWriter out = null;
            try {
                  out = new PrintWriter(new FileOutputStream(fileName), true);
                  for (int i = 0; i < size(); i++) {
                        out.println((String) get(i));
                  }
            }
            catch (IOException e) {
                  e.printStackTrace();
            }
            finally {
                  if (out != null) {
                        out.close();
                  }
            }
      }

}
0
 
KruleCommented:
In both cases you appear to want to do the same thing, which would be to delete a line in a file.

Here's what I would do: (taken from http://www.javacoffeebreak.com/java103/java103.html) and modified

import java.io.*;

class FileInputDemo
{
        public static void main(String args[])
      {
                ArrayList lines = new ArrayList();

                // args.length is equivalent to argc in C
                if (args.length == 1)
            {
                        try
                  {
                                // Open the file that is the first
                                // command line parameter
                                FileInputStream fstream = new
                              FileInputStream(args[0]);

                                // Convert our input stream to a
                                // DataInputStream
                        DataInputStream in =
                                        new DataInputStream(fstream);

                                // Continue to read lines while
                                // there are still some left to read
                                while (in.available() !=0)
                        {
                              lines.add(in.readLine());
                        }

                        in.close();

                                // Whatever logic we have to pick which line to delete goes here
                                lines.remove(5); // Shifts the rest as necessary

                                FileOutputStream outStream = new FileOutputStream(args[0]);

                                DateOutputStream out = new DataOutputStream(outStream);


                                for( int i = 0; i < lines.size(); i++) {
                                    out.write((String)lines.get(i));
                                }
                  }
                        catch (Exception e)
                  {
                        System.err.println("File input error");
                  }
            }
                else
                                System.out.println("Invalid parameters");
      }

}

Enjoy
0
 
CEHJCommented:
:-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now