By Java how to delete some lines in a text file containg some character?

Hello All,
I am having a text file [Basically It is a Sql File]. its containing some commented lines.
All i need to do is, By using a java Method I have to delete All the commented lines in That file .....
For Example if My file is like following.
/**** Commented By Kannan *********/
Select * From Employee;
go
/******************** Commented by Others ********/
Select * From DevMembers;
go
/*** Ends here **********/
And the output file would be
Select * From Employee;
go
select * From DevMembers;
go
can Anyone Help me in this regards,
Akram Kannan

BufferedReader should probably wrap the FileReader if you will be reading line-by-line. Same with the Writer.
And a simple -
if (  "/*".equals(record.substring(0,1) ) ... should handle the comment locations bit.
Message was edited by:
abillconsl

Similar Messages

  • Delete specified line in a text file

    can anyone help me??
    how to remove specific line in a text file using java?? thank thanks

    Read the file, write a new one, and when you write skip over the line you want to delete.

  • Deleting a line from a text file & opening a project from another project

    hi
    im doing project on linux redhat 8.0 using java (jbuilder 8). I need to
    open a project2 from behind a menu item in project1. I tried importing
    the package of project2 and then initializing a constructor of main
    class of project2 in project1(behind the "open project2" menu item) but
    i dont know where to place the class files of project2 so im getting a
    compilation error(when compiling project1) that unable to locate
    imported package files.
    is there any other way of doing this
    like making an .exe file of project2??? if so, then pls tell me how.
    Another problem is i want to delete a line of selected text from a text
    file
    using java. is there a method for that or any other way?
    Anybodyyyy have any ideasss?
    Alisha

    Cross- and double-posted: http://forum.java.sun.com/thread.jspa?threadID=645244

  • How to insert new line in a text file

    hi all,
    i wnat to know how can i insert a new line in a text file using java.
    for example i want the formate of the text like this
    1 2 3
    4 5 6
    until now i know only how to insert data but not new line.
    Thanks in advandce

    Hi you can put a new line in a text file using System.getProperty("line.separator"). This implementation will work for every OS.
    import java.io.*;
    class Demo{
    public static void main(String args[]) throws Exception {
    FileWriter fr = new FileWriter("FileDemo.txt");
         fr.write("AAAAAAAAAA");
    fr.write(System.getProperty("line.separator"));
    fr.write("AAAAAAAAAAA");
    fr.close();
    }

  • How to read every line from a text file???

    How can i read every line from my text file ("eka.txt")
    now it only reads the first line and prints it out.
    What is wrong with this?
    import java.io.*;
    import java.util.*;
    class Testi{
         public static void main(String []args)throws IOException {
         BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
    File inputFile = new File ("eka.txt");
    FileReader fis =new FileReader(inputFile);
    BufferedReader bis = new BufferedReader(fis);
    String test=bis.readLine();
    String tmp= "";
    while((bis.readLine().trim() != null)) {
    int spacefound=0;
    int l=test.indexOf(" ");
         for(int i=0;i<test.length();i++){
         char c=test.charAt(i);
         if(c!=' ') tmp+=""+c;
         if(c==' ' && (spacefound<1) && !(tmp.equals(""))){
         tmp+=""+c;
         spacefound++;
         if(tmp.length()==l) {
         System.out.println(tmp);
         tmp="";
         spacefound=0;
         if(tmp.length()<l){
         for(int i=0;i<=(l-tmp.length());i++)
         tmp+=""+' ';
         System.out.println(tmp);

    Try this code, Hope it servers your purpose.
    import java.io.*;
    import java.util.*;
    class Testi {
         public static void main(String []args)throws IOException {
              BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
              File inputFile = new File ("Eka.txt");
              FileReader fis =new FileReader(inputFile);
              BufferedReader bis = new BufferedReader(fis);
              String test=bis.readLine();
              while(test != null) {
                   StringTokenizer st = new StringTokenizer(test," ");
                   while(st.hasMoreTokens())
                        System.out.println(st.nextToken());
                   test = bis.readLine();
    }Sudha

  • Java write/append to each line of a text file

    I have spent numerous hours trying to figure out what I am doing wrong. If anyone more experienced could tell me what is wrong with my code.
    I have a very simple text file with 5 lines:
    line1
    line2
    line3
    line4
    line5
    All I am trying to do is append some string to the end of each of those lines. Everytime I run my code, it erases all content but does not write/append anything to the file. Any help is greatly appreciated.
    Thanks! I am about to throw this monitor out the window!!
    package Chapter6;
    import java.io.*;
    public class fileNavigation2 {
         public static void main(String[] args) {
              File dir = new File("C:\\testing");
              System.out.println(dir.isDirectory());
              try {
                   File file = new File(dir, "Test.txt");
                   FileReader fr = new FileReader(file);
                   BufferedReader br = new BufferedReader(fr);
                   FileWriter fw = new FileWriter(file);
                   BufferedWriter bw = new BufferedWriter(fw);
                   String s = "Add1,";
                   String s2 = "Add2\n";
                   String str;
                   while((str = br.readLine()) != null) {
                   StringBuffer sb = new StringBuffer(str);
                   sb.append(s + s2);
                   String y = sb.toString();
                   System.out.println(sb);
                   System.out.println("Appending");
                   bw.write(y);
                   bw.close();
                   System.out.println("Done");
              }catch(IOException e) {}
    }

    First, thanks a lot for your feedback. The code makes a lot of sense but it does not update the content of my Test.txt file.
    I only edited the line of code that creates the new file so that it could find the location of the file.
    Scanner file = new Scanner(new File("C:\\testing",fileName));==============================
    The code now looks like:
    import java.io.*;
    import java.util.*;
    public class Main {
        static void appendTo(String fileName, String[] newLines) throws IOException {
            List<String> allLines = getLinesFrom(fileName);
            for(String line : newLines) {
                allLines.add(line);
            writeLinesTo(fileName, allLines);
        static List<String> getLinesFrom(String fileName) throws IOException {
            List<String> lines = new ArrayList<String>();
            Scanner file = new Scanner(new File("C:\\testing",fileName));
            while(file.hasNextLine()) {
                lines.add(file.nextLine());
            file.close();
            System.out.println(lines);
            return lines;
        static void writeLinesTo(String fileName, List<String> lines) throws IOException {
            BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
            for(String line : lines) {
                out.write(line);
                out.write(System.getProperty("line.separator"));
            out.close();
        public static void main(String[] args) {
            String fileName = "Test.txt";
            String[] extraLines = {
                    "a new line",
                    "and yet another new line"
            try {
                appendTo(fileName, extraLines);
                System.out.println("Done.");
            } catch (IOException e) {
                e.printStackTrace();
    }Again, thanks for the help.

  • How to read specific lines from a text file using external table or any other method?

    Hi,
    I have a text file with delimited data, I have to pick only odd number rows and load into a table...
    Ex:
    row1:  1,2,2,3,3,34,4,4,4,5,5,5,,,5  ( have to load only this row)
    row2:   8,9,878,78,657,575,7,5,,,7,7
    Hope this is enough..
    I am using Oracle 11.2.0 version...
    Thanks

    There are various ways to do this.  I would be inclined to use SQL*Loader.  That way you can load it from the client or the server and you can use a SQL*Loader sequence to preserve the row order in the text file.  I would load the whole row as a varray into a staging table, then use the TABLE and MOD functions to load the individual numbers from only the odd rows.  Please see the demonstration below.
    SCOTT@orcl12c> HOST TYPE text_file.csv
    1,2,2,3,3,34,4,4,4,5,5,5,,,5
    8,9,878,78,657,575,7,5,,,7,7
    101,201
    102,202
    SCOTT@orcl12c> HOST TYPE test.ctl
    LOAD DATA
    INFILE text_file.csv
    INTO TABLE staging
    FIELDS TERMINATED BY ','
    TRAILING NULLCOLS
    (whole_row VARRAY TERMINATED BY '/n' (x INTEGER EXTERNAL),
    rn SEQUENCE)
    SCOTT@orcl12c> CREATE TABLE staging
      2    (rn         NUMBER,
      3     whole_row  SYS.OdciNumberList)
      4  /
    Table created.
    SCOTT@orcl12c> HOST SQLLDR scott/tiger CONTROL=test.ctl LOG=test.log
    SQL*Loader: Release 12.1.0.1.0 - Production on Tue Aug 27 13:48:37 2013
    Copyright (c) 1982, 2013, Oracle and/or its affiliates.  All rights reserved.
    Path used:      Conventional
    Commit point reached - logical record count 4
    Table STAGING:
      4 Rows successfully loaded.
    Check the log file:
      test.log
    for more information about the load.
    SCOTT@orcl12c> CREATE TABLE a_table
      2    (rn       NUMBER,
      3     data  NUMBER)
      4  /
    Table created.
    SCOTT@orcl12c> INSERT INTO a_table (rn, data)
      2  SELECT s.rn,
      3         t.COLUMN_VALUE data
      4  FROM   staging s,
      5         TABLE (s.whole_row) t
      6  WHERE  MOD (rn, 2) != 0
      7  /
    17 rows created.
    SCOTT@orcl12c> SELECT * FROM a_table
      2  /
            RN       DATA
             1          1
             1          2
             1          2
             1          3
             1          3
             1         34
             1          4
             1          4
             1          4
             1          5
             1          5
             1          5
             1
             1
             1          5
             3        101
             3        201
    17 rows selected.

  • How to read some lines from a text file using java.

    hi,
    i m new to java and i want to read some lines from a text file based on some string occurrence in the file. This file to be read in steps.
    we only want to read the file upto the first Occurrence of "TEXT" string.
    How to do it ,,,
    Kinldy give the code
    Regards,
    Sagar
    this is the text file
    dfgjdjj
    sfjhjkd
    ghjkdg
    hjkdgh TEXT
    ikeyt
    ujt
    jk
    tyk TEXT
    rukl
    r

    Hendawy wrote:
    Since the word "TEXT" is formed of 4 letters, you would read the text file 4 bytes by four bytes. Wrong on two counts. First, the file may not be encoded 1 byte per character. It could be utf-16 in which case it would be two byte per character. Second, even if it were 1 byte per character, the string "Text" may not start on a 4 byte boundary.
    Consider a FileInputStream object "fis" that points to your text file. use fis.read(byte[] array, int offset, int len) to read every four bytes. Convert the "TEXT" String into a byte array "TEXT".getBytes(), and yous the Arrays class to compare the equality of the read bytes with your "TEXT".getBytes()Wrong since it relies on my second point and will fail when fis.read(byte[] array, int offset, int len) does not read 4 bytes (as is no guaranteed to). Check the Javadoc. Also, the file may not be encoded with the default character encoding.
    The problem is easily solved by reading a line at a time using a BufferedReader wrapping an InputStreamReader wrapping a FileInputStream and specifying the correct character encoding.
    Edited by: sabre150 on Apr 29, 2009 2:13 PM

  • How to delete some date in target table at a mapping?

    How to delete some date in target table at a mapping?
    I extract date from source tabel into target table,
    but before extract date I want to delete some date from target?
    how to do?

    Just to change a bit of terminology in the reply, within the mapping, click on operator properties and choose TRUNCATE/INSERT.
    Note that truncate is dependent on constraints, so you probably must disable those before doing this. You can of course do DELETE/INSERT...
    Jean-Pierre

  • How to delete a line in SAP Script

    Hi,
         How to delete a line in SAP Script.
    Cheers
    S Kumar

    Okay.if still you are looking the option of deleting line there is one solution.
    Just delete the all contents of perticuler(which needs to deleted) line including the tag column(Where you are /*) and save.if you see again than line would not be seen as blank,you will found it has been deleted.

  • How to delete a line item from the sales order

    Hi all,
    how to delete a line item from the sales order for which the production is already happened and it has been delivered. the production order status is DLV.
    Regards
    Kumar

    Hi
    U can do this in two ways one u can short close the order by entering Reason for rejection in VA02 at header level and if yr order is multiple line item order u can enter the reason for rejection in any of the line item which u don't want to deliver.
    This is called short close ( as the qty is not delivered fully).
    Thx.

  • How to delete the line item from a sales order

    Hi all,
    how to delete a line item from the sales order for which the production is already happened and it has been delivered. the production order status is DLV.
    Regards
    Kumar

    Hi Kumar,
    I think you can just delete it in the sales order directly, if you are using make-to-order scenario, then there will be special stock left for the sales order as the production has been goods receipt, you need to use MM transaction move the stock to unrestricted use stock. If you are using make-to-stock scenario, there should be no further problem. If you are using assembly order, please try to reject the sales order item to see if it could fullfill your requirement.
    Regards,
    Rachel

  • How to delete the line between the last point and first point?

    How to delete the line between the last point and first point? 
    I want to draw a curve many times, from first point to the end point. and redraw from first point to the end point.But I hope update point by point. but between the end point and the first point,  there is a line. How to delete the line?
    the code is:
    CNiReal64Vector plotData(50);
    m_graph.ChartLength = 50;
    //m_graph.ClearData();
    for (int j = 0; j < 2; j++)
           for (int i=0; i<50; i++)
                   plotData[i] = ((double)rand()/(double)RAND_MAX)*6 + 4;
                   m_graph.GetPlots().Item("Plot-1").ChartXY(i, plotData[i]);
                   Sleep(100);
    Attachments:
    20150605142608.png ‏31 KB

    Hi Kumar,
    I think you can just delete it in the sales order directly, if you are using make-to-order scenario, then there will be special stock left for the sales order as the production has been goods receipt, you need to use MM transaction move the stock to unrestricted use stock. If you are using make-to-stock scenario, there should be no further problem. If you are using assembly order, please try to reject the sales order item to see if it could fullfill your requirement.
    Regards,
    Rachel

  • Delete lines from a text file

    i need to delete (or replace them with white space) a few lines from a text file. I have a text file with first few lines & last few lines containing "<"or ">". I need to delete/replace with white space, the entire line. i need to do this urgently
    Could some one please tell me how to do this?

    the file can be of size 8MB or more. i get this file
    every week from a third party. So the size is not
    constant. I need to remove/replace with white space,
    the fist & last few lines and the rest is comma
    seperated values which i need to load to database
    using sqlldr. But still not sure abt how to remove
    the first few lines.
    i need to read this file, replace the lines as i read
    them and write the replaced string back to the file &
    then load the rest of lines to database.8 MByte is fairly small. Read the file a line at a time and copy to a new file only the lines you want. Should take no more than a second or so.
    P.S. It will probably be a mistake if you try to edit the original file in place.

  • How can I find out the number of lines in a text file?

    How can I find out the number of lines in a text file?

    java.io.BufferedReader in = new java.io.BufferedReader( new java.io.FileReader( "YourFile.txt" ) );
    int lineCount = 0;
    while( in.readLine() != null )
    lineCount ++;
    System.out.println( "Line Count = " + lineCount );

Maybe you are looking for

  • A list of values type item in a based table block

    My block is based on a table. An item of the block take the value of a field of the table when the form raises. But this item must be updateable (by selecting a value in a list) and then it must be of the list of value type. And the values in the lis

  • Bold line in ALv grid output

    Hi, I want show few line item informations in bold in ALV grid output. Please let me know your suggestions.

  • Baseline timephased data disappeared in MS Project 2013 with Project Server 2013!!!

    Hi, I am facing a very strange problem within a custom "task usage" view that I'  ve created. In the right pane I have three timephased fields (work, baseline  work and actual work). When I first add resources to tasks and save baseline  everything w

  • Error trying to Buy using shopping cart

    Hi, I added the iTunes Essentials: Green Day - The Basics, to my shopping cart yesterday. When I go to the cart and click Buy, I get the following message: +"Purchase of this item is not currently available. This item is being modified. Please try ag

  • My adobe illustrator CS3 won't work with OS X Yosemite

    after installing OS X Yosemite software upgrade my Adobe Illustrator CS3 will not open with the ability to use the tools, only as a Read only file. I was told to install JavaForOSX2014-001 but that has not helped.