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:Main.java

public static void main(String[] args) throws Exception {
    ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream("filename"));
    ZipEntry zipentry = zipinputstream.getNextEntry();
    while (zipentry != null) {
        String entryName = zipentry.getName();
        File newFile = new File(entryName);
        String directory = newFile.getParent();
        if (directory == null) {
            if (newFile.isDirectory())
                break;
        }/*from www  . j  a v a2 s . c om*/
        RandomAccessFile rf = new RandomAccessFile(entryName, "r");
        String line;
        if ((line = rf.readLine()) != null) {
            System.out.println(line);
        }
        rf.close();
        zipinputstream.closeEntry();
        zipentry = zipinputstream.getNextEntry();
    }
    zipinputstream.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    RandomAccessFile file = new RandomAccessFile(new File("scores.html"), "rw");
    for (int i = 1; i <= 6; i++) {
        System.out.println(file.readLine());
    }/*from w  ww. jav a  2 s.c o  m*/
    file.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    RandomAccessFile file = new RandomAccessFile("scores.html", "rw");
    for (int i = 1; i <= 6; i++) {
        System.out.println(file.readLine());
    }//  ww w  .  ja  v  a  2 s .  c  o m
    long current = file.getFilePointer();
    file.seek(current + 6);
    file.write("34".getBytes());
    for (int i = 1; i <= 5; i++) {
        System.out.println(file.readLine());
    }
    current = file.getFilePointer();
    file.seek(current + 6);
    file.write("27".getBytes());
    file.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from  w w w.  j av  a 2s  .  co m*/
        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeBytes("Hello World from java2s.com");

        raf.seek(0);

        System.out.println(raf.readLine());

        raf.seek(0);

        raf.writeBytes("This is an example from java2s.com");

        raf.seek(0);

        System.out.println(raf.readLine());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {/*from  ww  w .  j  a va  2 s . co  m*/
        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        // write something in the file
        raf.writeUTF("Hello World");

        // set the file pointer at 0 position
        raf.seek(0);

        // print the line
        System.out.println(raf.readLine());

        // set the file pointer at 0 position
        raf.seek(0);

        raf.writeUTF("This is an example \n Hello World");

        raf.seek(0);
        // print the line
        System.out.println(raf.readLine());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {/*w  w  w . j av a  2  s  .c  om*/

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeUTF("Hello World from java2s.com");

        // set the file pointer at 0 position
        raf.seek(0);

        // print the string
        System.out.println(raf.readUTF());

        // set the file pointer at 0 position
        raf.seek(0);

        // attempt to skip 10 bytes
        System.out.println(raf.skipBytes(10));

        System.out.println(raf.readLine());

        // set the file pointer to position 8
        raf.seek(8);

        // attempt to skip 10 more bytes
        System.out.println(raf.skipBytes(10));
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Cat.java

public static void concatenate(String fileName) {
    RandomAccessFile file = null;
    String line = null;/*from w  ww  . j  a v  a2 s .  c o  m*/

    try {
        file = new RandomAccessFile(fileName, "r");
        while ((line = file.readLine()) != null) {
            System.out.println(line);
        }
        return;
    } catch (FileNotFoundException fnf) {
        System.err.println("File: " + fileName + " not found.");
    } catch (Exception e) {
        System.err.println(e.toString());
    } finally {
        if (file != null) {
            try {
                file.close();
            } catch (IOException io) {
            }
        }
    }

}

From source file:Main.java

@SuppressWarnings("resource")
public static String getTotalRAM() {
    RandomAccessFile reader = null;
    String load = null;//from w  ww. j av a  2s.  c o m
    try {
        reader = new RandomAccessFile("/proc/meminfo", "r");
        load = reader.readLine();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        // Streams.close(reader);
    }

    return load.replaceAll("MemTotal:", "").replaceAll("kB", "").trim();
}

From source file:Main.java

public static double getCpuUsage1() {
    try {/*from w ww. j  a  v  a  2s. com*/
        RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
        String load = reader.readLine();
        String[] toks = load.split(" ");

        double user1 = Double.parseDouble(toks[2]);
        double system1 = Double.parseDouble(toks[4]);
        double irq1 = Double.parseDouble(toks[7]);
        double idle1 = Double.parseDouble(toks[5]);
        try {
            Thread.sleep(360);
        } catch (Exception e) {
            e.printStackTrace();
        }
        reader.seek(0);
        load = reader.readLine();
        reader.close();
        toks = load.split(" ");
        double user2 = Double.parseDouble(toks[2]);
        double system2 = Double.parseDouble(toks[4]);
        double irq2 = Double.parseDouble(toks[7]);
        double idle2 = Double.parseDouble(toks[5]);

        double user_pass = user2 - user1;
        double system_pass = system2 - system1;
        double irq_pass = irq2 - irq1;
        double idle_pass = idle2 - idle1;
        double usage = (user_pass + system_pass + irq_pass) * 100.00
                / (user_pass + irq_pass + system_pass + idle_pass);
        BigDecimal b = new BigDecimal(usage);
        double res = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return res;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    return 0;

}

From source file:Main.java

public static String read(String s) throws IOException {
    String s1;/* w ww.j a v  a2 s .c  om*/
    RandomAccessFile randomaccessfile;
    s1 = "";
    randomaccessfile = null;
    File file = new File(s);
    randomaccessfile = new RandomAccessFile(file, "r");
    s1 = randomaccessfile.readLine();
    randomaccessfile.close();
    return s1;
}