Example usage for java.io LineNumberReader close

List of usage examples for java.io LineNumberReader close

Introduction

In this page you can find the example usage for java.io LineNumberReader close.

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:org.beangle.emsapp.system.action.FileAction.java

public String download() throws IOException {
    String path = get("path");
    FileMimeType fileMimeType = new FileMimeType(mimeTypeProvider);
    if (StringUtils.isNotBlank(path)) {
        File file = new File(path);
        if (!file.isFile()) {
            return null;
        }/*from w  w  w . j  av a2  s . com*/
        boolean download = getBool("download");
        if (!download && fileMimeType.isTextType(file)) {
            List<String> lines = CollectUtils.newArrayList();
            LineNumberReader reader = new LineNumberReader(new FileReader(file));
            String line = reader.readLine();
            while (null != line) {
                lines.add(line);
                line = reader.readLine();
            }
            put("lines", lines);
            put("file", file);
            reader.close();
            return forward("content");
        } else {
            streamDownloader.download(getRequest(), getResponse(), file);
        }
    }
    return null;
}

From source file:org.beangle.ems.system.web.action.FileAction.java

public String download() throws IOException {
    String path = get("path");
    FileMimeType fileMimeType = new FileMimeType(mimeTypeProvider);
    if (Strings.isNotBlank(path)) {
        File file = new File(path);
        if (!file.isFile()) {
            return null;
        }/*from   w  w w .java  2 s . c  om*/
        boolean download = getBool("download");
        if (!download && fileMimeType.isTextType(file)) {
            List<String> lines = CollectUtils.newArrayList();
            LineNumberReader reader = new LineNumberReader(new FileReader(file));
            String line = reader.readLine();
            while (null != line) {
                lines.add(line);
                line = reader.readLine();
            }
            put("lines", lines);
            put("file", file);
            reader.close();
            return forward("content");
        } else {
            streamDownloader.download(getRequest(), getResponse(), file);
        }
    }
    return null;
}

From source file:com.github.magicsky.sya.checkers.TestSourceReader.java

/**
 * Reads a section in comments form the source of the given class. The section
 * is started with '// {tag}' and ends with the first line not started by '//'
 *
 * @since 4.0//  w  ww .j a v a  2s .co m
 */
public static String readTaggedComment(Bundle bundle, String srcRoot, Class clazz, final String tag)
        throws IOException {
    IPath filePath = new Path(srcRoot + '/' + clazz.getName().replace('.', '/') + ".java");

    InputStream in = FileLocator.openStream(bundle, filePath, false);
    LineNumberReader reader = new LineNumberReader(new InputStreamReader(in));
    boolean found = false;
    final StringBuilder content = new StringBuilder();
    try {
        String line = reader.readLine();
        while (line != null) {
            line = line.trim();
            if (line.startsWith("//")) {
                line = line.substring(2);
                if (found) {
                    content.append(line);
                    content.append('\n');
                } else {
                    line = line.trim();
                    if (line.startsWith("{" + tag)) {
                        if (line.length() == tag.length() + 1
                                || !Character.isJavaIdentifierPart(line.charAt(tag.length() + 1))) {
                            found = true;
                        }
                    }
                }
            } else if (found) {
                break;
            }
            line = reader.readLine();
        }
    } finally {
        reader.close();
    }
    Assert.assertTrue("Tag '" + tag + "' is not defined inside of '" + filePath + "'.", found);
    return content.toString();
}

From source file:com.googlecode.jweb1t.JWeb1TSearcherInMemory.java

private void fillMap(final File aFile, final int aLevel) throws IOException {
    final LineNumberReader reader = new LineNumberReader(new FileReader(aFile));
    String line;//w  w w  . ja  v a2  s  .  c  o  m
    while ((line = reader.readLine()) != null) {
        final String[] parts = line.split("\t");

        if (parts.length != 2) {
            continue;
        }

        ngramLevelMap.get(aLevel).addSample(parts[0], Long.parseLong(parts[1]));
    }
    reader.close();
}

From source file:com.sds.acube.ndisc.mts.xserver.util.XNDiscUtils.java

/**
 * XNDisc ? ?/*from w  w  w  . j  a v a2s . c  o  m*/
 */
private static void readVersionFromFile() {
    XNDisc_PublishingVersion = "<unknown>";
    XNDisc_PublishingDate = "<unknown>";
    InputStreamReader isr = null;
    LineNumberReader lnr = null;
    try {
        isr = new InputStreamReader(
                XNDiscUtils.class.getResourceAsStream("/com/sds/acube/ndisc/mts/xserver/version.txt"));
        if (isr != null) {
            lnr = new LineNumberReader(isr);
            String line = null;
            do {
                line = lnr.readLine();
                if (line != null) {
                    if (line.startsWith("Publishing-Version=")) {
                        XNDisc_PublishingVersion = line.substring("Publishing-Version=".length(), line.length())
                                .trim();
                    } else if (line.startsWith("Publishing-Date=")) {
                        XNDisc_PublishingDate = line.substring("Publishing-Date=".length(), line.length())
                                .trim();
                    }
                }
            } while (line != null);
            lnr.close();
        }
    } catch (IOException ioe) {
        XNDisc_PublishingVersion = "<unknown>";
        XNDisc_PublishingDate = "<unknown>";
    } finally {
        try {
            if (lnr != null) {
                lnr.close();
            }
            if (isr != null) {
                isr.close();
            }
        } catch (IOException ioe) {
        }
    }
}

From source file:com.sds.acube.jstor.XNDiscXNApi.java

/**
 * XNDisc XNApi ?  ?//from www  .j a  v  a  2  s.  c om
 */
private static void readVersionFromFile() {
    XNDiscXNApi_PublishingVersion = "<unknown>";
    XNDiscXNApi_PublishingDate = "<unknown>";
    InputStreamReader isr = null;
    LineNumberReader lnr = null;
    try {
        isr = new InputStreamReader(XNDiscXNApi.class.getResourceAsStream("/com/sds/acube/jstor/version.txt"));
        if (isr != null) {
            lnr = new LineNumberReader(isr);
            String line = null;
            do {
                line = lnr.readLine();
                if (line != null) {
                    if (line.startsWith("Publishing-Version=")) {
                        XNDiscXNApi_PublishingVersion = line
                                .substring("Publishing-Version=".length(), line.length()).trim();
                    } else if (line.startsWith("Publishing-Date=")) {
                        XNDiscXNApi_PublishingDate = line.substring("Publishing-Date=".length(), line.length())
                                .trim();
                    }
                }
            } while (line != null);
            lnr.close();
        }
    } catch (IOException ioe) {
        XNDiscXNApi_PublishingVersion = "<unknown>";
        XNDiscXNApi_PublishingDate = "<unknown>";
    } finally {
        try {
            if (lnr != null) {
                lnr.close();
            }
            if (isr != null) {
                isr.close();
            }
        } catch (IOException ioe) {
        }
    }
}

From source file:org.apache.hadoop.yarn.server.nodemanager.TestLinuxContainerExecutorWithMocks.java

private List<String> readMockParams() throws IOException {
    LinkedList<String> ret = new LinkedList<String>();
    LineNumberReader reader = new LineNumberReader(new FileReader(mockParamFile));
    String line;/*  www.ja  v  a  2  s.c  om*/
    while ((line = reader.readLine()) != null) {
        ret.add(line);
    }
    reader.close();
    return ret;
}

From source file:org.kalypso.model.wspm.tuhh.schema.simulation.QRelationFileReader.java

public void read(final File inputFile) throws IOException {
    final String filename = inputFile.getName();

    LineNumberReader reader = null;
    try {/*from www . j ava 2 s .  c  o m*/
        reader = new LineNumberReader(new FileReader(inputFile));

        while (reader.ready()) {
            final String line = reader.readLine();
            if (line == null)
                break;

            final int lineNumber = reader.getLineNumber();
            readLine(line, lineNumber, filename);
        }

        reader.close();

        m_qresult.setPointsObservation(m_observation);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:com.ggvaidya.scinames.ui.DatasetImporterController.java

private void displayPreview() {
    filePreviewTextArea.setText("");
    if (currentFile == null)
        return;/*  www.j av a 2s. co m*/

    if (currentFile.getName().endsWith("xls") || currentFile.getName().endsWith("xlsx")) {
        // Excel files are special! We need to load it special and then preview it.
        ExcelImporter imp;

        String excelPreviewText;
        try {
            imp = new ExcelImporter(currentFile);
            List<Sheet> sheets = imp.getWorksheets();

            StringBuffer preview = new StringBuffer();
            preview.append("Excel file version " + imp.getWorkbook().getSpreadsheetVersion() + " containing "
                    + sheets.size() + " sheets.\n");
            for (Sheet sh : sheets) {
                preview.append(
                        " - " + sh.getSheetName() + " contains " + sh.getPhysicalNumberOfRows() + " rows.\n");

                // No rows?
                if (sh.getPhysicalNumberOfRows() == 0)
                    continue;

                // Header row?
                Row headerRow = sh.getRow(0);
                boolean headerEmitted = false;

                for (int rowIndex = 1; rowIndex < sh.getPhysicalNumberOfRows(); rowIndex++) {
                    if (rowIndex >= 10)
                        break;

                    Row row = sh.getRow(rowIndex);

                    if (!headerEmitted) {
                        preview.append(
                                "  - " + String.join("\t", ExcelImporter.getCellsAsValues(headerRow)) + "\n");
                        headerEmitted = true;
                    }
                    preview.append("  - " + String.join("\t", ExcelImporter.getCellsAsValues(row)) + "\n");
                }

                preview.append("\n");
            }

            excelPreviewText = preview.toString();
        } catch (IOException ex) {
            excelPreviewText = "Could not open '" + currentFile + "': " + ex;
        }

        filePreviewTextArea.setText(excelPreviewText);

        return;
    }

    // If we're here, then this is some sort of text file, so let's preview the text content directly.
    try {
        LineNumberReader reader = new LineNumberReader(new BufferedReader(new FileReader(currentFile)));

        // Load the first ten lines.
        StringBuffer head = new StringBuffer();
        for (int x = 0; x < 10; x++) {
            head.append(reader.readLine());
            head.append('\n');
        }

        reader.close();
        filePreviewTextArea.setText(head.toString());
    } catch (IOException ex) {
        filePreviewTextArea.setBackground(BACKGROUND_RED);
        filePreviewTextArea.setText("ERROR: Could not load file '" + currentFile + "': " + ex);
    }
}

From source file:com.sds.acube.ndisc.xadmin.XNDiscAdminUtil.java

/**
 * XNDisc Admin ? ?/*w ww  .  ja va 2s.  c o  m*/
 */
private static void readVersionFromFile() {
    XNDiscAdmin_PublishingVersion = "<unknown>";
    XNDiscAdmin_PublishingDate = "<unknown>";
    InputStreamReader isr = null;
    LineNumberReader lnr = null;
    try {
        isr = new InputStreamReader(
                XNDiscAdminUtil.class.getResourceAsStream("/com/sds/acube/ndisc/xadmin/version.txt"));
        if (isr != null) {
            lnr = new LineNumberReader(isr);
            String line = null;
            do {
                line = lnr.readLine();
                if (line != null) {
                    if (line.startsWith("Publishing-Version=")) {
                        XNDiscAdmin_PublishingVersion = line
                                .substring("Publishing-Version=".length(), line.length()).trim();
                    } else if (line.startsWith("Publishing-Date=")) {
                        XNDiscAdmin_PublishingDate = line.substring("Publishing-Date=".length(), line.length())
                                .trim();
                    }
                }
            } while (line != null);
            lnr.close();
        }
    } catch (IOException ioe) {
        XNDiscAdmin_PublishingVersion = "<unknown>";
        XNDiscAdmin_PublishingDate = "<unknown>";
    } finally {
        try {
            if (lnr != null) {
                lnr.close();
            }
            if (isr != null) {
                isr.close();
            }
        } catch (IOException ioe) {
        }
    }
}