Removing the Control Characters from a text file

Hi,
I am using the java.util.regex.* package to removing the control characters from a text file. I got below programming from the java.sun site.
I am able to successfully compile the file and the when I try to run the file I got the error as
------------------------------------------------------------------------D:\Debi\datamigration>java Control
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repet
ition
{cntrl}
at java.util.regex.Pattern.error(Pattern.java:1472)
at java.util.regex.Pattern.closure(Pattern.java:2473)
at java.util.regex.Pattern.sequence(Pattern.java:1597)
at java.util.regex.Pattern.expr(Pattern.java:1489)
at java.util.regex.Pattern.compile(Pattern.java:1257)
at java.util.regex.Pattern.<init>(Pattern.java:1013)
at java.util.regex.Pattern.compile(Pattern.java:760)
at Control.main(Control.java:24)
Please help me on this issue.
Thanks&Regards
Debi
import java.util.regex.*;
import java.io.*;
public class Control {
public static void main(String[] args)
throws Exception {
//Create a file object with the file name
//in the argument:
File fin = new File("fileName1");
File fout = new File("fileName2");
//Open and input and output stream
FileInputStream fis =
new FileInputStream(fin);
FileOutputStream fos =
new FileOutputStream(fout);
BufferedReader in = new BufferedReader(
new InputStreamReader(fis));
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(fos));
     // The pattern matches control characters
Pattern p = Pattern.compile("{cntrl}");
Matcher m = p.matcher("");
String aLine = null;
while((aLine = in.readLine()) != null) {
m.reset(aLine);
//Replaces control characters with an empty
//string.
String result = m.replaceAll("");
out.write(result);
out.newLine();
in.close();
out.close();

Hi,
I used the code below with the \p, but I didn't able to complie the file. It gave me an
D:\Debi\datamigration>javac Control.java
Control.java:24: illegal escape character
Pattern p = Pattern.compile("\p{cntrl}");
^
1 error
Please help me on this issue.
Thanks&Regards
Debi
// The pattern matches control characters
Pattern p = Pattern.compile("\p{cntrl}");
Matcher m = p.matcher("");
String aLine = null;

Similar Messages

  • Removing unwanted control characters in exported text files

    I am currently evaluating Crystal Reports 2008 to determine applicability to our requirements. I need to export data files to continuous text to be read by other application software. I have successfully created the files but have what I believe to be page feed or end-of-page control characters (small rectangles) in the output. Can someone enlighten me as to how I can suppress or remove these control characters?

    In the export to text options enter 0 for the number of lines per page. This will produce an unpaginated text document without the page control markers.

  • Removing non printable characters from an excel file using powershell

    Hello,
    anyone know how to remove non printable characters from an excel file using powershell?
    thanks,
    jose.

    To add - Excel is a binary file.  It cannot be managed via external methods easily.  You can write a macro that can do this.  Post in the Excel forum and explain what you are seeing and get the MVPs there to show you how to use the macro facility
    to edit cells.  Outside of cell text "unprintable" characters are a normal part of Excel.
    ¯\_(ツ)_/¯

  • Reading characters from a text file into a multidimensional array?

    I have an array, maze[][] that is to be filled with characters from a text file. I've got most of the program worked out (i think) but can't test it because I am reading my file incorrectly. However, I'm running into major headaches with this part of the program.
    The text file looks like this: (It is meant to be a maze, 19 is the size of the maze(assumed to be square). is free space, # is block, s is start, x is finish)
    This didn't paste evenly, but thats not a big deal. Just giving an idea.
    19
    5..................
    And my constructor looks like follows, I've tried zillions of things with the input.hasNext() and hasNextLine() to no avail.
    Code:
    //Scanner to read file
    Scanner input = null;
    try{
    input = new Scanner(fileName);
    }catch(RuntimeException e) {
    System.err.println("Couldn't find the file");
    System.exit(0);
    //Set the size of the maze
    while(input.hasNextInt())
    size = input.nextInt();
    //Set Limits on coordinates
    Coordinates.setLimits(size);
    //Set the maze[][] array equal to this size
    maze = new char[size][size];
    //Fill the Array with maze values
    for(int i = 0; i < maze.length; i++)
    for(int x = 0; x < maze.length; x++)
    if(input.hasNextLine())
    String insert = input.nextLine();
    maze[i][x] = insert.charAt(x);
    Any advice would be loved =D

    Code-tags sometimes cause wonders, I replaced # with *, as the code tags interprets # as comment, which looks odd:
    ******...*.........To your code: Did you test it step by step, to find out about what is read? You could either use a debugger (e.g., if you have an IDE) or system outs to get a clue. First thing to check would be, if the maze size is read correctly. Further, the following loops look odd:for(int i = 0; i < maze.length; i++) {
        for(int x = 0; x < maze.length; x++) {
            if (input.hasNextLine()) {
                String insert = input.nextLine();
                maze[x] = insert.charAt(x);
    }Shouldn't the nextLine test and assignment be in the outer loop? And assignment be to each maze's inner array? Like so:for(int i = 0; i < maze.length; i++) {
        if (input.hasNextLine()) {
            String insert = input.nextLine();
            for(int x = 0; x < insert.size(); x++) {
                maze[i][x] = insert.charAt(x);
    }Otherwise, only one character per line is read and storing a character actually should fail.

  • How do i remove the exif metadata from my jpg file produced with a digital camera photo?

    How do i remove the exif metadata from my jpg file produced with a digital camera photo?

    The excellent and free utility Irfanview has the option to remove the EXIF data.
    Simply open and the file and do a Save, or Save As with the original name.
    Surprisingly, the default is to not keep the original EXIF data.

  • How can i remove the special characters from my keyboard on my iphone3gs

    how can i remove the special character from my keybord keys on my iphone 3gs

    What characters are you talking about exactly and why would you want to remove any?
    There is really no way to modify Apple's keyboards or add your own in iOS, but there are some apps that may provide a work around.

  • How can I remove the blue background from my PDFs files

    How can I remove the blue background from my PDFs files

    Which tutorial?
    Answers are in your HTML and CSS code.  What is the link to your online test page?
    Nancy O.

  • How to read characters from a text file in java program ?

    Sir,
    I have to read the characters m to z listed in a text file .
    I must compare the character read from the file.
    And if any of the characters between m to z is matched i have to replace it with a hexadecimal value.
    Any help or suggesstions in this regard would be very useful.
    Thanking you,
    khurram

    Hai,
    The requirement is like this
    There is an input file, the contents of the file are as follows, you can assume any name for the file.
    #Character mappings for Japanese Shift-JIS character set
    #ASCII character Mapped Shift-JIS character
    m 227,128,133 #Half width katakana letter small m
    n 227,128,134 #Half width katakana letter small n
    o 227,129,129
    p 227,129,130
    q 227,129,131
    r 227,129,132
    s 227,129,133
    t 227,129,134
    u 227,129,135
    v 227,129,136
    w 227,129,137
    x 227,129,138
    y 227,129,139
    z 227,129,142
    The contents of the above file are to be read as input.
    On encountering any character between m to z, i have to do a replacement with a hexadecimal code point value for the multibyte representation in the second column in the input file.
    I have the code to get the unicode codepoint value from the multibyte representation, but not from a file.
    So if you could please tell me how to get the characters in the second column, it would be very useful for me.
    The character # is used to represent the beginning of a comment in the input file.
    And comment lines are to be ignored while reading the file.
    Say i have a string str="message";
    then i should replace the m with the unicode code point value.
    Thanking you,
    khurram

  • Removing unicode control characters from string

    Hi.
    I have a webservice where I return an object (with some strings) back to the client. The information is read from a database, and the string can sometimes contain invalid xml characters (like unicode 0x13). This results in an error when parsing the information at the client side.
    Is there someway a easy way to set up a filter or something that checks whether the string contains characters outside the valid range specified for XML's (lower than unicode 0x20 etc), and removes them/replace them with a different character?

    If you have to get rid of the control chars then       String someText = "a\nb\nc\td\re\r\nf";
            String someTextWithoutControlChars = someText.replaceAll( "[\u0000-\u0020]","");
            System.out.println(someTextWithoutControlChars);but like kaj says, some control chars are valid.

  • [SOLVED] Why are control characters visible in text files??

    Hi,
    If I direct the output of 'ls' to a file like:
    ls > file.txt
    and open the file in a editor such as vi, joe or mousepad, it looks like this:
    [[01;34m22x22[[0m
    [[01;34m24x24[[0m
    [[01;34m32x32[[0m
    [[0mindex.theme[[0m
    [[01;34mscalable[[0m
    [[m
    I just did the same thing on an old slackware box and no control characters are visible. The same for a recent LFS build. I realize I could probably pipe through dos2unix, but it shouldn't be happening anyway. Any input is appreciated.
    Thanks
    -Frank
    Last edited by fianella (2007-10-24 10:37:00)

    at the DOS prompt (sorry, old habits die hard :-) ) type
    alias ls
    and see what the results are.  Compare that against your slackware or lfs builds.  You will probably find that the color= option is different - most builds will not include the color ANSI sequences if you pipe the output, but if it says color=always the color codes will be included in the redirected file.
    Assuming you find an alias for ls that forces color output, you need to find where that is taking place... in your ~/.bashrc file?  in /etc/profile.d?  And change that to alias ls='ls --color=tty' or something sensible like that.

  • Read email adress from a text file then check the validity of them

    a text file has three lines, each line contains one email adress:
    [email protected]
    qwe@@ws.com
    wer//@we.net
    read the email address from a text file, then check which one is invalid, output the invalid email adress in the console.

    no 3 .umm, an email adress can have more than 2 '.'s in it,
    example:
    [email protected]
    would be a valid email address.
    To decide what a valid address is you'd need to parse it against the correct standard.
    I think however that javax.mail.internet.InternetAddress does this for you, check out the docs:
    http://java.sun.com/products/javamail/1.2/docs/javadocs/javax/mail/internet/InternetAddress.html
    even if it parses it may not be a valid address though in that it may not actually exist.

  • Reading in integers from a text file

    Hi, I am going to go for the 'reading in from a text file' action as I haven't done this before. So I have been looking at all the examples and am trying the one out below :) My question is that though the structure seems pretty straightforward, what is the action of the 'token' and why doesn't java recognise it as it is used in many different examples that I have seen?
    public static int[] getIntegersFromFile(String fileName) throws IOException {
                   StringWriter writer = new StringWriter();
                   BufferedReader reader = new BufferedReader(new FileReader(fileName));
                   List<Integer> list = new ArrayList<Integer>();
                   for (String line = reader.readLine(); line != null; line = reader.readLine()) {
                   writer.write(line + " ");
                   StringTokenizer tokens = new StringTokenizer(writertoString());
                   while (tokens.hasMoreTokens()) {
                        String str = tokens.nextToken();
                        try {
                             list.add(new Integer(str));
                             } catch (NumberFormatException e) {
                                  System.out.println("Error '" + str + "' is not an integer.");
                   int[] array = new int[list.size()];
                        for( int i = 0; i < array.length; i++){
                        array[i] = list.get(i);
                   return array;
         }

    Hey, first of all, a piece of advice, try formatting your codes you make easier
    to understand for the people who help you. You can check out the
    formatting tips
    Now, A think that could help you a lot, is the next link, seek out the
    documentation for the StringTokenizer class
    http://java.sun.com/j2se/1.5.0/docs/api/
    Here your code formated and later my interpretation of your question.
    public static int[] getIntegersFromFile(String fileName) throws IOException {
       StringWriter writer = new StringWriter();
       BufferedReader reader = new BufferedReader(new FileReader(fileName));
       List<Integer> list = new ArrayList<Integer>();
       for (String line = reader.readLine(); line != null; line = reader.readLine()) {
          writer.write(line + " ");
       StringTokenizer tokens = new StringTokenizer(writertoString());
       while (tokens.hasMoreTokens()) {
          String str = tokens.nextToken();
          try {
             list.add(new Integer(str));
          } catch (NumberFormatException e) {
             System.out.println("Error '" + str + "' is not an integer.");
       int[] array = new int[list.size()];
       for( int i = 0; i < array.length; i++){
          array = list.get(i);
       return array;
    }StringTokenizer separates the string contained by the space character, and
    each piece of the separated string is stored into the token.
    Further on, could you extend your question about "why doesn't Java
    recognize what?" please, because I don't know if you have a problem
    iterating or is just that it doesn't compile or what.
    Expecting your answer, to complete mine, cya around.
    -Best Regards.

  • How to  remove the Special characters like @,#,$ ,..from field

    How to  remove the Special characters like @,#,$<,..from text  field. we nedd to remove any specila char from text field .
    ex:text  = just#fi%cation@text
    the text should be justification.

    hi check this ..
    data:char(25) value '5#4#2#&1#&',
         char1(9) .
           replace all occurrences of '#' in char with 'and' .
           replace all occurrences of '&' in char with 'num' .
         write: char.
    or use this..
    data:char(25) value 'test@ing*5#4#2#&1#&',
         char1(9) .
           replace all occurrences of '#' in char with space .
           replace all occurrences of '&' in char with space .
           replace all occurrences of '@' in char with space .
           replace all occurrences of '*' in char with space .
         write: char.
    regards,
    venkat.

  • What's the easiest way to remove/append a line of a text file???

    The line to remove can be any line in the file - not only the first or last one.
    Is there any good tutorial for file operations?
    Thanks a million,
    Serano.

    To remove a line from a text file, the easiest way is to read it in one line at a time and write out all lines except the one you want to remove to a new version of the file. Likewise, to insert a line, you read the file one line at a time and write the lines to a new version, inserting the new line at its correct spot.

  • How can I plott data from a text file in the same way as a media player using the pointer slide to go back and fort in my file?

    I would like to plott data from a text file in the same way as a media player does from a video file. I’m not sure how to create the pointer slide function. The vi could look something like the attached jpg.
    Please, can some one help me?
    Martin
    Attachments:
    Plotting from a text file like a media player example.jpg ‏61 KB

    HI Martin,
    i am not realy sure what you want!?!?
    i think you want to display only a part of the values you read from XYZ
    so what you can do:
    write all the values in an array.
    the size of the array is the max. value of the slide bar
    now you can select a part of the array (e.g. values from 100 to 200) and display this with a graph
    the other option is to use the history function of the graphes
    regards
    timo

Maybe you are looking for