Java Path Remove nio removeLine(String path, String line, boolean commentAware)

Here you can find the source of removeLine(String path, String line, boolean commentAware)

Description

Removes this line from the specified file.

License

Open Source License

Parameter

Parameter Description
path - the path to a file
line - the line to remove
commentAware - indicates whether or not this method is aware that comments exist

Exception

Parameter Description
IOException an exception

Declaration

static boolean removeLine(String path, String line, boolean commentAware)
        throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**// w ww  .j a v  a 2s  . c  om
     * Removes this line from the specified file.
     * 
     * @param path
     *            - the path to a file
     * @param line
     *            - the line to remove
     * @param commentAware
     *            - indicates whether or not this method is aware that comments exist
     * @throws IOException 
     * @returns <code>true</code> if the file contained the line to begin with; <code>false</code> otherwise
     */
    static boolean removeLine(String path, String line, boolean commentAware)
            throws IOException {
        boolean hasLine = fileHasLine(path, line, commentAware);
        if (hasLine) {
            String[] lines = getLines(path, true);
            if (hasLine) {
                String text = "";
                for (int i = 0; i < lines.length; i++) {
                    if (!lines[i].matches(line
                            + (commentAware ? "(\\s*//.*)?" : ""))) {
                        text += lines[i] + System.lineSeparator();
                    }
                }
                if (!text.isEmpty()
                        && text.endsWith(System.lineSeparator())) {
                    text = text.substring(0, text.length()
                            - System.lineSeparator().length());
                }
                File file = new File(path);
                file.delete();
                try {
                    Files.write(Paths.get(path), text.getBytes(),
                            StandardOpenOption.CREATE);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return hasLine;
    }

    static boolean fileHasLine(String path, String line,
            boolean commentAware) throws IOException {
        String[] lines = getLines(path, true);
        boolean hasLine = false;
        for (String fileLine : lines) {
            if (fileLine
                    .matches(line + (commentAware ? "(\\s*//.*)?" : ""))) {
                hasLine = true;
                break;
            }
        }
        return hasLine;
    }

    /**
     * Returns the lines of the specified file as a <code>List</code>.
     * 
     * @param path
     *            - the path to a file
     * @param includeBlankLines
     *            - indicates whether or not to include blank lines in list
     * @return the lines of the specified file or an empty list if the file was not found
     * @throws IOException
     */
    static String[] getLines(String path, boolean includeBlankLines)
            throws IOException {
        List<String> lines = new ArrayList<String>();
        File file = new File(path);
        if (file.exists()) {
            BufferedReader reader = new BufferedReader(new FileReader(
                    new File(path)));
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (!line.trim().isEmpty() || includeBlankLines) {
                    lines.add(line);
                }
            }
            reader.close();

        }
        return lines.toArray(new String[] {});
    }
}

Related

  1. removeDirectoryIfItIsEmpty(Path directoryToRemove)
  2. removeDriveLetter(Path path)
  3. removeFile(final String removePath)
  4. removeFile(String path)
  5. removeFile(String workspacePath)
  6. removeRecursive(Path path)
  7. removeRecursive(Path path)
  8. removeSymlink(Path softLinkLocation)
  9. reportRemoveSymink(Path softLinkLocation)