0

I have seen how to use regex patterns to remove all blank lines when reading a file, but I want to remove all unnecessary lines after all content. For example:

Input:

asdiofhpaiodf

(empty line, don't remove)

asdfihap[sdifh

asdpiofhaspdif

asiodfhpai[sdfh

(Empty Line, remove)

(Empty Line, remove)


Output:

asdiofhpaiodf

(Empty line)

asdfihap[sdifh

asdpiofhaspdif

asiodfhpai[sdfh

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • In other (clearer) words: you want to remove all trailing empty lines? – laune Jul 18 '15 at 18:25
  • Define "unneeded lines". Also could you show what have you tried so far? – Pshemo Jul 18 '15 at 18:29
  • @laune Yes, that is what i mean. I have no idea where i start. im not too gud –  Jul 18 '15 at 18:35
  • How can we represent [line separators in regex](http://stackoverflow.com/questions/3219014/what-is-a-cross-platform-regex-for-removal-of-line-breaks)? How can we represent [end of string in regex](http://www.regular-expressions.info/anchors.html)? Also are you sure you need regex here? `trim()` seems like good fit here. – Pshemo Jul 18 '15 at 18:39
  • If you are accepting `\s+$` as the answer, then this is a duplicate question and should be [marked](http://stackoverflow.com/questions/9108781/how-to-remove-leading-and-trailing-whitespace-from-the-string-in-java). –  Jul 18 '15 at 19:01

2 Answers2

2

You can trim the end of string with

String trimmedContents = origContents.replaceAll("\\s+$", "");
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • This depends what origContents is supposed to hold, and it contradicts what OP wrote in the Q and comments. Strange... – laune Jul 18 '15 at 19:20
0

Just to add on to stribizhev's answer, you may also want to use System.lineSeparator() as opposed to \s (in most cases \s more useful but i do not know your needs)

Since I'm posting an answer (cant make comments yet), I might as well go out. I was under the impression that you were trying to re-size the file. (and again i just used System.lineSeparator() to show how to use it.

    String regex = "[^"+ System.lineSeparator() + "]" + System.lineSeparator() + "$"; //or use "\\S\\s*$";
    Matcher whiteSpace = Pattern.compile(regex).matcher("");
    int threshold = 4; //number of characters to look back at the end of file.
    byte[] readBytes = new byte[threshold]; //for whatever reason we can't just read in a string :/
    try ( RandomAccessFile file = new RandomAccessFile(input_file, "rw")){
        //start at the end of file, look for non line separator character.
        long cursor;
        for(cursor = file.length() - threshold; cursor > 0 ; cursor=cursor-threshold){
            file.seek(cursor);
            file.readFully(readBytes);
            if(whiteSpace.reset(new String(readBytes)).find()){
                cursor = cursor + whiteSpace.start() + 1;
                break;
            }
        }
        file.setLength(cursor);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

no idea what the performance will be like, but i don't read the whole file in, and start from the end.

Aarjav
  • 1,334
  • 11
  • 22