Example usage for java.io LineNumberReader LineNumberReader

List of usage examples for java.io LineNumberReader LineNumberReader

Introduction

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

Prototype

public LineNumberReader(Reader in) 

Source Link

Document

Create a new line-numbering reader, using the default input-buffer size.

Usage

From source file:HTMLParser.java

public void open(String fileName) throws FileNotFoundException {
    reader = new FileReader(fileName);
    in = new LineNumberReader(reader);
}

From source file:org.openanzo.jdbc.utils.FileLineIterator.java

/**
 * Create an Iterator for an InputStreamReader
 * /*ww w .  ja v  a2 s  .  c  o  m*/
 * @param reader
 *            source of data
 */
protected FileLineIterator(Reader reader) {
    this.reader = reader;
    this.lnr = new LineNumberReader(reader);
}

From source file:ru.jkff.antro.ReportReader.java

public Report readReport(String filename) throws IOException {
    // function getProfileData() {
    // return (//from  w w w  . j a  v a 2 s . co m
    //   JSONObject (read until {} [] braces are balanced)
    // )
    // }
    // function getTrace() {
    // return (
    //   JSONObject (read until {} [] braces are balanced)
    // )
    // }
    try {
        LineNumberReader r = new LineNumberReader(
                new BufferedReader(new FileReader(filename), REPORT_FILE_BUFFER_SIZE));
        StringBuilder sb = new StringBuilder();
        String line;
        while (null != (line = r.readLine())) {
            sb.append(line);
        }

        JSONArray data = new JSONArray(sb.toString());

        JSONArray profileData = data.getJSONArray(0);
        JSONObject traceData = data.getJSONObject(1);

        return new Report(toTrace(traceData), toAnnotatedFiles(profileData));
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.github.javarch.persistence.orm.test.DataBaseTestBuilder.java

private String readNextSqlScript(Resource rs) throws IOException {
    EncodedResource encoded = new EncodedResource(rs);
    LineNumberReader lnr = new LineNumberReader(encoded.getReader());
    String currentStatement = lnr.readLine();
    StringBuilder scriptBuilder = new StringBuilder();
    while (currentStatement != null) {
        if (StringUtils.hasText(currentStatement)
                && (SQL_COMMENT_PREFIX != null && !currentStatement.startsWith(SQL_COMMENT_PREFIX))) {
            if (scriptBuilder.length() > 0) {
                scriptBuilder.append('\n');
            }//from ww  w .j a v a2  s.  com
            scriptBuilder.append(currentStatement);
        }
        currentStatement = lnr.readLine();
    }
    return scriptBuilder.toString();
}

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;
        }/*w  w  w.ja  v  a  2  s.c  o m*/
        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.termmed.utils.FileHelper.java

/**
 * Count lines./*www  . j a v  a  2s  . co m*/
 *
 * @param file the file
 * @param firstLineHeader the first line header
 * @return the int
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static int countLines(File file, boolean firstLineHeader) throws IOException {

    FileInputStream fis = new FileInputStream(file);
    InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
    LineNumberReader reader = new LineNumberReader(isr);
    int cnt = 0;
    String lineRead = "";
    while ((lineRead = reader.readLine()) != null) {
    }

    cnt = reader.getLineNumber();
    reader.close();
    isr.close();
    fis.close();
    if (firstLineHeader) {
        return cnt - 1;
    } else {
        return cnt;
    }
}

From source file:edu.stanford.muse.util.JSONUtils.java

public static String jsonForNewNewAlgResults(AddressBook ab, String resultsFile)
        throws IOException, JSONException {
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(resultsFile)));
    JSONObject result = new JSONObject();

    JSONArray groups = new JSONArray();
    int groupNum = 0;
    while (true) {
        String line = lnr.readLine();
        if (line == null)
            break;
        line = line.trim();//w w  w . java 2  s. co  m
        // line: group 8, freq 49: seojiwon@gmail.com sseong@stanford.edu debangsu@cs.stanford.edu

        // ignore lines without a ':'
        int idx = line.indexOf(":");
        if (idx == -1)
            continue;

        String vars = line.substring(0, idx);
        // vars: freq=5 foo bar somevar=someval
        // we'll pick up all tokens with a = in them and assume they mean key=value
        StringTokenizer varsSt = new StringTokenizer(vars);

        JSONObject group = new JSONObject();
        while (varsSt.hasMoreTokens()) {
            String str = varsSt.nextToken();
            // str: freq=5
            int x = str.indexOf("=");
            if (x >= 0) {
                String key = str.substring(0, x);
                String value = "";
                // we should handle the case of key= (empty string)
                if (x < str.length() - 1)
                    value = str.substring(x + 1);
                group.put(key, value);
            }
        }

        String groupsStr = line.substring(idx + 1);
        // groupsStr: seojiwon@gmail.com sseong@stanford.edu debangsu@cs.stanford.edu

        StringTokenizer st = new StringTokenizer(groupsStr);

        JSONArray groupMembers = new JSONArray();
        int i = 0;

        while (st.hasMoreTokens()) {
            String s = st.nextToken();
            Contact ci = ab.lookupByEmail(s);
            if (ci == null) {
                System.out.println("WARNING: no contact info for email address: " + s);
                continue;
            }
            groupMembers.put(i++, ci.toJson());
        }
        group.put("members", groupMembers);
        groups.put(groupNum, group);
        groupNum++;
    }

    result.put("groups", groups);
    return result.toString();
}

From source file:com.penguineering.cleanuri.sites.reichelt.ReicheltExtractor.java

@Override
public Map<Metakey, String> extractMetadata(URI uri) throws ExtractorException {
    if (uri == null)
        throw new NullPointerException("URI argument must not be null!");

    URL url;/*from  w w w  .j a  v  a2s .  c o m*/
    try {
        url = uri.toURL();
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("The provided URI is not a URL!");
    }

    Map<Metakey, String> meta = new HashMap<Metakey, String>();

    try {
        final URLConnection con = url.openConnection();

        LineNumberReader reader = null;
        try {
            reader = new LineNumberReader(new InputStreamReader(con.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                if (!line.contains("<h2>"))
                    continue;

                // h2
                int h2_idx = line.indexOf("h2");
                // Doppelpunkte
                int col_idx = line.indexOf("<span> :: <span");
                final String art_id = line.substring(h2_idx + 3, col_idx);
                meta.put(Metakey.ID, html2oUTF8(art_id).trim());

                int span_idx = line.indexOf("</span>");
                final String art_name = line.substring(col_idx + 32, span_idx);
                meta.put(Metakey.NAME, html2oUTF8(art_name).trim());

                break;
            }

            return meta;
        } finally {
            if (reader != null)
                reader.close();
        }
    } catch (IOException e) {
        throw new ExtractorException("I/O exception during extraction: " + e.getMessage(), e, uri);
    }

}

From source file:javarestart.JavaRestartLauncher.java

public static String getText(String url) throws IOException {
    URL website = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) website.openConnection();
    try (LineNumberReader in = new LineNumberReader(new InputStreamReader(connection.getInputStream()))) {
        StringBuilder response = new StringBuilder();
        String inputLine;//from   w w w.j  ava2 s  .  c o m
        while ((inputLine = in.readLine()) != null)
            response.append(inputLine);

        return response.toString();
    }
}

From source file:com.anyi.gp.license.RegisterTools.java

public static List getMacAddresses() {
    List result = new ArrayList();
    try {/*www . j  av  a 2s  .c  o m*/
        Properties props = System.getProperties();
        String command = "ipconfig -a";
        if (props.getProperty("os.name").toLowerCase().startsWith("windows"))
            command = "ipconfig /all";

        Process process = Runtime.getRuntime().exec(command);
        InputStreamReader ir = new InputStreamReader(process.getInputStream());
        LineNumberReader input = new LineNumberReader(ir);
        String line;
        while ((line = input.readLine()) != null)
            if (line.indexOf("Physical Address") > 0) {
                String MACAddr = line.substring(line.indexOf("-") - 2);
                // System.out.println("MAC address = [" + MACAddr + "]");
                result.add(MACAddr);
            }
    } catch (java.io.IOException e) {
        e.printStackTrace();
        // System.err.println("IOException " + e.getMessage());
    }
    return result;
}