Results 1 to 11 of 11
  1. #1
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default Deleting specific chars from a file

    Hi all,

    I have the following code which is reading a text file, there are some dashes (-) I would like to take them out such as (PRODUCT-NAME-IN) to be (PRODUCTNAMEIN) as an example. Could anyone help me with it please.






    Java Code:
    import java.io.*;
    import java.util.Calendar;
    
    public class ReadAndWrite{
    
    public static void main(String args[]) throws IOException {
    
    	FileInputStream ProductID = new FileInputStream("javaOutput.java");
        BufferedReader ProductIDinStream  = new BufferedReader(new InputStreamReader(ProductID));
    PrintWriter(new File("Product.dat")));
          
        String line1;   
        
        int i;
        for (i=0; i<3 ; i++ ){
    
        	line1=ProductIDinStream.readLine();
        	if (line1.equals("PRODUCT-NAME-IN"))
        		
        		line1.replaceAll("PRODUCT-NAME-IN", "R");
        		
        	System.out.println(line1);
        	} 
        	{
        	System.out.println("Rjnjjaid");
      
        }
    
       	ProductIDinStream.close();
        
    
    }
    }
    Thanks in advance ,,

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    if (line1.equals("PRODUCT-NAME-IN"))

    line1.replaceAll("PRODUCT-NAME-IN", "R");

    System.out.println(line1);
    replaceAll returns a new String and does not change the old one (because of Strings are immutable) so you have to write line1 = line1.replaceAll....

    And why dou want to replace it with a R ?

    I would like to take them out such as (PRODUCT-NAME-IN) to be (PRODUCTNAMEIN) as an example
    System.out.println("PRODUCT-NAME-IN".replace("-", ""));
    or more generally
    System.out.println("PRODUCT-NAME-IN".replaceAll("(.*?)-(.*?)-(.*?)", "$1$2$3"));
    :D :confused:

  3. #3
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Perfect,, it works very well,,

    Thank you so much for your help,, BTW I was writing R just to try it,,

  4. #4
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    I'm sorry but how can I make it general.. i have many places that have dashes and I would like to change all of them.. what should I write instead of PRODUCT-NAME-IN..


    Thanks in advance for your help

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    We dont know how your file/line looks like, so we cant give you the right answer.
    But if (line1.equals("PRODUCT-NAME-IN")) only returns true if the line is 1:1 like "PRODUCT-NAME-IN"
    You could use contains("-") to check if the line contains a minus sign?! Or you could use matches(..) with a correct regular expression ?!
    Or Pattern and Matcher classes, or the Scanner class..or or or :)
    Can you give us an example how your file looks like?

  6. #6
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Here I have listed a part of my text file.. I would like to make it general, so that will read until find any dash and then delete this dash.

    the line that you gave it to me earlier was working perfectly with just "PRODUCT-NAME-IN" but I would like to make it general for all the dashes..

    ----------------------- Starting the part of the text file -------------------
    BufferedReader PRODUCT-NAME-IN = new BufferedReader
    BufferedReader PRODUCT-IN-STOCK-IN = new BufferedReader
    BufferedReader PRODUCT-SALES-PRICE-IN = new BufferedReader
    BufferedReader SUPPLIER-COMPANY-NAME-IN = new BufferedReader
    BufferedReader SUPPLIER-PHONE-IN = new BufferedReader

    String PRODUCT-NAME-IN-IN = 01.readLine();

    String P-STOCK-IN = 01.readLine();

    String P-SALES-IN = 01.readLine();




    S-ID-OUT = RAND-NUMBER + S-ID-OUT;

    S-ID-OUT = P-ID-OUT + S-ID-OUT;

    P-ID-OUT = S-ID-OUT + P-ID-OUT;

    int PID = P-ID-OUT / CURRENT-9;
    float P-ID-OUT = P-ID-OUT1 % CURRENT-9;
    int SID = S-ID-OUT / PID;
    float S-ID-OUT = S-ID-OUT1 % PID;
    PRODUCT-O-OUT.Write ("\t" + "hello" + "\t");
    PRODUCT-O-OUT.Write ("\t" + "Hello2" + "\t");


    PRODUCT-NAME-IN.close();
    PRODUCT-IN-STOCK-IN.close();
    PRODUCT-SALES-PRICE-IN.close();
    PRODUCT-OUT.close();
    SUPPLIER-COMPANY-NAME-IN.close();
    SUPPLIER-PHONE-IN.close();
    ----------------- end of the example---------------

    Hope that make it easier,,

  7. #7
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Hi everyone,

    Is there anyone can help me please???

    Thanks in advance

  8. #8
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    I want to help, but I dont understand your previous post and dont understand what problem do you have right now.

  9. #9
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Thank you so much .. let's say I have the following text file


    ----------------------- Starting the part of the text file -------------------
    BufferedReader PRODUCT-NAME-IN = new BufferedReader
    BufferedReader PRODUCT-IN-STOCK-IN = new BufferedReader
    BufferedReader PRODUCT-SALES-PRICE-IN = new BufferedReader
    BufferedReader SUPPLIER-COMPANY-NAME-IN = new BufferedReader
    BufferedReader SUPPLIER-PHONE-IN = new BufferedReader

    String PRODUCT-NAME-IN-IN = 01.readLine();

    String P-STOCK-IN = 01.readLine();

    String P-SALES-IN = 01.readLine();




    S-ID-OUT = RAND-NUMBER + S-ID-OUT;

    S-ID-OUT = P-ID-OUT + S-ID-OUT;

    P-ID-OUT = S-ID-OUT + P-ID-OUT;

    int PID = P-ID-OUT / CURRENT-9;
    float P-ID-OUT = P-ID-OUT1 % CURRENT-9;
    int SID = S-ID-OUT / PID;
    float S-ID-OUT = S-ID-OUT1 % PID;
    PRODUCT-O-OUT.Write ("\t" + "hello" + "\t");
    PRODUCT-O-OUT.Write ("\t" + "Hello2" + "\t");


    PRODUCT-NAME-IN.close();
    PRODUCT-IN-STOCK-IN.close();
    PRODUCT-SALES-PRICE-IN.close();
    PRODUCT-OUT.close();
    SUPPLIER-COMPANY-NAME-IN.close();
    SUPPLIER-PHONE-IN.close();
    ----------------- end of the example---------------




    and I would like to to change every dash(-) in this text file to be without all the dashes.


    I used your following solution and it works perfectly but it works with a specific variable or statement.

    --------- Here is your helpful solution ------------------
    System.out.println("PRODUCT-NAME-IN".replace("-", ""));
    or more generally
    System.out.println("PRODUCT-NAME-IN".replaceAll("(.*?)-(.*?)-(.*?)", "$1$2$3"));
    --------------- end of your solution --------------------


    This solution helps a lot with a specific variable ("PRODUCT-NAME-IN") but I would like to make more general and read everything but once face a DASH delete it directly..


    Hope I explain it correctly..

  10. #10
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    ....
    Java Code:
    	public static void main(String[] args) throws Exception {
    		Scanner sc = new Scanner(new File("YOURTEXTFILE.txt"));
    		while(sc.hasNextLine()){
    			System.out.println(sc.nextLine().replace("-", ""));
    		}
    	}
    :confused:

  11. #11
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    I really appreciate your help,, Hope I can help you with anything,,,

Similar Threads

  1. Replies: 1
    Last Post: 02-07-2011, 08:22 PM
  2. JTextfield only allow specific chars
    By fuzzdn in forum AWT / Swing
    Replies: 1
    Last Post: 12-11-2010, 03:49 PM
  3. File Not Deleting
    By Moncleared in forum New To Java
    Replies: 7
    Last Post: 02-21-2010, 08:28 PM
  4. Replies: 2
    Last Post: 11-08-2008, 10:40 AM
  5. Help deleting a file
    By 3speed in forum New To Java
    Replies: 4
    Last Post: 11-01-2008, 05:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •