Example usage for java.io RandomAccessFile readLine

List of usage examples for java.io RandomAccessFile readLine

Introduction

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

Prototype


public final String readLine() throws IOException 

Source Link

Document

Reads the next line of text from this file.

Usage

From source file:net.sourceforge.doddle_owl.data.JpnWordNetDic.java

private static String getData(long dfp, RandomAccessFile dataFile, String encoding) {
    try {/*from w  ww  . j a  va2  s .  com*/
        // System.out.println("dfp: " + dfp);
        dataFile.seek(dfp);
        return new String(dataFile.readLine().getBytes("ISO8859_1"), encoding);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return null;
}

From source file:net.sourceforge.doddle_owl.data.JpnWordNetDic.java

private static long getIndexFp(long fp) {
    RandomAccessFile indexFpListFile = null;
    indexFpListFile = jpnwnWordIndexFile;

    try {//ww  w  . j a v a  2s. c  o  m
        indexFpListFile.seek(fp);
        String fpStr = indexFpListFile.readLine();
        if (fpStr == null) {
            return -1;
        }
        return Long.valueOf(fpStr);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return -1;
}

From source file:net.sourceforge.doddle_owl.data.JpnWordNetDic.java

private static Concept getConcept(long dfp) {
    RandomAccessFile dataFile = null;
    try {/*from   w ww.j  a v a 2s.  c o m*/
        dataFile = jpnwnConceptDataFile;
        dataFile.seek(dfp);
        String data = new String(dataFile.readLine().getBytes("ISO8859_1"), "UTF-8");
        // System.out.println(data);
        String[] dataArray = data.split("\\^");
        String[] conceptData = new String[4];
        String id = dataArray[0].replaceAll("\t", "");
        System.arraycopy(dataArray, 1, conceptData, 0, conceptData.length);

        String uri = "";
        Concept c = null;
        uri = DODDLEConstants.JPN_WN_URI + id;
        c = new Concept(uri, conceptData);
        jpnwnURIConceptMap.put(uri, c);

        return c;
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return null;
}

From source file:org.wso2.carbon.automation.engine.frameworkutils.CodeCoverageUtils.java

private static void instrumentSelectedFiles(String carbonHome) throws IOException {
    log.info("Instrumentation of jar files in progress ...");
    File instrumentationTxt = System.getProperty("instr.file") != null
            ? new File(System.getProperty("instr.file"))
            : new File(System.getProperty("basedir") + File.separator + "src" + File.separator + "test"
                    + File.separator + "resources" + File.separator + "instrumentation.txt");
    List<String> filePatterns = new ArrayList<String>();
    if (instrumentationTxt.exists()) {
        RandomAccessFile rf = new RandomAccessFile(instrumentationTxt, "r");
        try {/*from www  . j  a  v a 2  s .co m*/
            String line;
            while ((line = rf.readLine()) != null) {
                filePatterns.add(line);
            }
        } finally {
            rf.close();
        }
    }

    // Instrument the bundles which match the specified patterns in <code>filePatterns</code>
    File plugins = null;

    direcArrayList.clear();
    searchDirectoryByName(carbonHome, direcArrayList, "plugins");

    for (String aDirecArrayList : direcArrayList) {
        if (aDirecArrayList
                .contains("repository" + File.separator + "components" + File.separator + "plugins")) {
            plugins = new File(aDirecArrayList);
        }
    }

    //instrument the jars at plugins directory first (otherwise emma will complain about state versions of class files
    int instrumentedFileCount = 0;
    for (File file : plugins.listFiles()) {
        if (file.isFile()) {
            if (filePatterns.isEmpty()) { // If file patterns are not specified, instrument all files
                instrument(file);
                instrumentedFileCount++;
            } else {
                for (String filePattern : filePatterns) {
                    if (file.getName().startsWith(filePattern)) {
                        instrument(file);
                        instrumentedFileCount++;
                    }
                }
            }
        }
    }

    log.info("Instrumented " + instrumentedFileCount + " file(s) in plugins directory");

    direcArrayList.clear();

    //instrument the jar files in patches directory

    File patchesDir = null;

    searchDirectoryByName(carbonHome, direcArrayList, "patches");

    for (String aDirecArrayList : direcArrayList) {
        if (aDirecArrayList
                .contains("repository" + File.separator + "components" + File.separator + "patches")) {
            patchesDir = new File(aDirecArrayList);
        }
    }

    int instrumentedPatchFileCount = 0;
    File[] patchFiles = patchesDir.listFiles();
    Map<Integer, String> patchesDirTreeMap = new TreeMap<Integer, String>();
    if (patchFiles != null) {
        for (File patchFile : patchFiles) {
            Pattern pattern = Pattern.compile("-?\\d+"); //get the number from patch directory name
            Matcher matcher = pattern.matcher(patchFile.getName());
            if (matcher.find()) {
                patchesDirTreeMap.put(Integer.parseInt(matcher.group()), patchFile.getName());
            }
        }
        //patches directory names are sorted by patch number before instrumentation - this is because emma
        //records last instrumented file data in its metadata, so any duplicate file instrumentation will not cause
        //problems.
        for (Map.Entry entry : patchesDirTreeMap.entrySet()) {
            String filePathOfJarFiles = patchesDir.getAbsolutePath() + File.separator + entry.getValue();
            File name = new File(filePathOfJarFiles);
            log.info("Instrumenting jar files at - " + name.getName());
            Collection patchesCollection = FileUtils.listFiles(name, null, true);
            for (Object jarFile : patchesCollection) {
                File jarFileName = new File(jarFile.toString());
                if (jarFileName.exists()) {
                    for (String filePattern : filePatterns) {
                        if (jarFileName.getName().startsWith(filePattern)) {
                            instrument(jarFileName);
                            instrumentedPatchFileCount++;
                            break;
                        }
                    }
                }
            }
        }
        log.info("Instrumented " + instrumentedPatchFileCount + " file(s) in patches directory");
    }
}

From source file:net.sourceforge.doddle_owl.data.JpnWordNetDic.java

private static String getTermAndIndexFpSet(long ifp) {
    RandomAccessFile indexFile = null;
    indexFile = jpnwnWordDataFile;/*  ww  w .j av a  2 s.  c o m*/

    try {
        // System.out.println("ifp: " + ifp);
        indexFile.seek(ifp);
        return new String(indexFile.readLine().getBytes("ISO8859_1"), "UTF-8");
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return null;
}

From source file:fm.last.commons.io.LastFileUtils.java

/**
 * Reads the last bytes from the end of the passed file as a String.
 * //  w  w  w  .  j  a  v a2  s  .c o m
 * @param file File to read from.
 * @param bytes Number of bytes from end of file to read.
 * @return The end content of the file as a String.
 * @throws IOException If the file could not be opened or read.
 */
public static String tail(File file, long bytes) throws IOException {
    RandomAccessFile raFile = null;
    StringBuffer tail = new StringBuffer();
    try {
        raFile = new RandomAccessFile(file, "r");
        long length = raFile.length();
        if (bytes >= length) {
            return FileUtils.readFileToString(file);
        } else {
            raFile.seek(length - bytes);
            tail = new StringBuffer((int) bytes);
            String line = raFile.readLine();
            while (line != null) {
                tail.append(line);
                line = raFile.readLine();
                if (line != null) { // there is another line coming, so add line break
                    tail.append("\n");
                }
            }
        }
    } finally {
        LastIoUtils.closeQuietly(raFile);
    }
    return tail.toString();
}

From source file:dk.netarkivet.common.utils.cdx.BinSearch.java

/** Perform a binary search for a string in a file.
 * Returns the position of a line that begins with 'find'.
 * Note that this may not be the first line, if there be duplicates.
 * @param in the RandomAccessFile/*from   w  ww .ja v  a 2 s. c  o  m*/
 * @param find The String to look for in the above file
 * @throws IOException If some I/O error occurs
 * @return The index of a line matching find, or -1 if none found.
 */
private static long binSearch(RandomAccessFile in, String find) throws IOException {
    // The starting position for the binary search.  Always
    // at the start of a line that's < the wanted line.
    long startpos = 0;
    // Ensure that startpos isn't a match.
    in.seek(startpos);
    String line = in.readLine();
    if (line == null) {
        return -1;
    }
    if (compare(line, find) == 0) {
        return startpos;
    }
    // The ending position for the binary search.  Always
    // *after* a line that >= the wanted line (which also means
    // at the start of a line that's > the wanted line, or at EOF
    long endpos = in.length();

    // Set file pos to first line after middle.
    findMiddleLine(in, startpos, endpos);

    // When done searching, midpos points to a matching line, if any
    // Until the search is done, both endpos and startpos point
    // at non-matching lines (or EOF), and startpos < prevpos < endpos
    long prevpos = in.getFilePointer();
    do {
        line = in.readLine();
        if (line == null) {
            log.debug("Internal: Ran past end of file in '" + in + "' at " + endpos);
            return -1;
        }
        int cmp = compare(line, find);
        if (cmp > 0) {
            endpos = prevpos;
        } else if (cmp < 0) {
            startpos = prevpos;
        } else {
            return prevpos;
        }
        if (startpos == endpos) {
            return -1;
        }
        prevpos = findMiddleLine(in, startpos, endpos);
        if (prevpos == -1) {
            return -1;
        }
    } while (true);
}

From source file:com.grarak.kerneladiutor.utils.kernel.cpu.CPUFreq.java

private static Usage[] getUsages() {
    try {/*from  w  w  w . java  2  s .c  o m*/
        RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
        Usage[] usage = new Usage[getCpuCount() + 1];
        for (int i = 0; i < usage.length; i++)
            usage[i] = new Usage(reader.readLine());
        reader.close();
        return usage;
    } catch (FileNotFoundException e) {
        Log.i(TAG, "/proc/stat does not exist");
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:ListOfNumbers2.java

public void readList(String fileName) {
    String line = null;/* w  ww . j a va2s .  c  o  m*/
    try {
        RandomAccessFile raf = new RandomAccessFile(fileName, "r");
        while ((line = raf.readLine()) != null) {
            Integer i = new Integer(Integer.parseInt(line));
            System.out.println(i);
            victor.addElement(i);
        }
    } catch (FileNotFoundException fnf) {
        System.err.println("File: " + fileName + " not found.");
    } catch (IOException io) {
        System.err.println(io.toString());
    }
}

From source file:org.xwalk.runtime.extension.api.device_capabilities.DeviceCapabilitiesCPU.java

/**
 * The algorithm here can be found at://from www.  j  a  va 2  s  .  c  om
 * http://stackoverflow.com/questions/3017162/how-to-get-total-cpu-usage-in-linux-c
 */
private boolean getCPULoad() {
    try {
        RandomAccessFile file = new RandomAccessFile(SYSTEM_INFO_STAT_FILE, "r");
        String line = file.readLine();

        String[] arrs = line.split("\\s+");
        long total1 = 0;
        for (int i = 1; i < arrs.length; ++i) {
            total1 += Long.parseLong(arrs[i]);
        }
        // arrs[4] is the time spent in idle tasks.
        long used1 = total1 - Long.parseLong(arrs[4]);
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            mCPULoad = 0.0;
            return false;
        }

        file.seek(0);
        line = file.readLine();
        file.close();

        arrs = line.split("\\s+");
        long total2 = 0;
        for (int i = 1; i < arrs.length; ++i) {
            total2 += Long.parseLong(arrs[i]);
        }
        // arrs[4] is the time spent in idle tasks.
        long used2 = total2 - Long.parseLong(arrs[4]);
        if (total2 == total1) {
            mCPULoad = 0.0;
        } else {
            mCPULoad = (double) (used2 - used1) / (total2 - total1);
        }
    } catch (IOException e) {
        mCPULoad = 0.0;
        return false;
    }
    return true;
}