Example usage for java.io FileInputStream FileInputStream

List of usage examples for java.io FileInputStream FileInputStream

Introduction

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

Prototype

public FileInputStream(FileDescriptor fdObj) 

Source Link

Document

Creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    createFile();//w w w  .j  a v a2 s  .c o m

    File aFile = new File("C:/primes.bin");
    FileInputStream inFile = new FileInputStream(aFile);

    FileChannel inChannel = inFile.getChannel();

    final int PRIMESREQUIRED = 10;
    ByteBuffer buf = ByteBuffer.allocate(8 * PRIMESREQUIRED);

    long[] primes = new long[PRIMESREQUIRED];
    int index = 0; // Position for a prime in the file

    // Count of primes in the file
    final int PRIMECOUNT = (int) inChannel.size() / 8;

    // Read the number of random primes required
    for (int i = 0; i < PRIMESREQUIRED; i++) {
        index = 8 * (int) (PRIMECOUNT * Math.random());
        inChannel.read(buf, index); // Read the value
        buf.flip();
        primes[i] = buf.getLong(); // Save it in the array
        buf.clear();
    }

    for (long prime : primes) {
        System.out.printf("%12d", prime);
    }
    inFile.close(); // Close the file and the channel

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File("yourXML.xml");
    FileInputStream inputStream = new FileInputStream(file);
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(inputStream);

    System.out.println(reader.getVersion());
    System.out.println(reader.isStandalone());
    System.out.println(reader.standaloneSet());
    System.out.println(reader.getEncoding());
    System.out.println(reader.getCharacterEncodingScheme());

    parseRestOfDocument(reader);/*  w ww  . j  a va 2s .c  o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int sChunk = 8192;

    String zipname = "a.gz";
    FileOutputStream out = new FileOutputStream(zipname);
    GZIPOutputStream zipout = new GZIPOutputStream(out);
    byte[] buffer = new byte[sChunk];

    FileInputStream in = new FileInputStream(args[0]);
    int length;//from ww w .  ja v a 2  s .  co m
    while ((length = in.read(buffer, 0, sChunk)) != -1)
        zipout.write(buffer, 0, length);
    in.close();
    zipout.close();

}

From source file:Main.java

public static void main(String[] args) {
    byte[] b = { 'h', 'e', 'l', 'l', 'o' };
    try {//from w  w w  . jav a  2  s.c o m

        // create a new output stream
        OutputStream os = new FileOutputStream("test.txt");

        // craete a new input stream
        InputStream is = new FileInputStream("test.txt");

        // write something
        os.write(b);

        // read what we wrote
        for (int i = 0; i < b.length; i++) {
            System.out.print((char) is.read());
        }
        os.close();
        is.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:Grep.java

public static void main(String args[]) throws Exception {
    String regex = "";
    InputStream in = System.in;

    regex = args[0];//  w  w w . j a  va2s  . co  m
    in = new BufferedInputStream(new FileInputStream(args[1]));
    Pattern p = null;

    p = Pattern.compile(regex);

    BufferedReader buff = new BufferedReader(new InputStreamReader(in));

    String a;
    for (a = buff.readLine(); a != null; a = buff.readLine()) {
        Matcher m = p.matcher(a);
        if (m.find()) {
            System.out.println(a);
        }
    }
}

From source file:ScriptExecutionReaderDemo.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine jsEengine = manager.getEngineByExtension("js");
    Reader reader = new InputStreamReader(new FileInputStream("yourJavaScript.js"));
    jsEengine.eval(reader);//ww w.j  ava2s  .  c  o  m
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    PrintStream console = System.out;
    BufferedInputStream in = new BufferedInputStream(new FileInputStream("Redirecting.java"));
    PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out")));
    System.setIn(in);//from w ww. j a  va  2  s .  co  m
    System.setOut(out);
    System.setErr(out);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s;
    while ((s = br.readLine()) != null)
        System.out.println(s);
    out.close();
    System.setOut(console);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Properties p = new Properties();

    p.put("today", new Date().toString());
    p.put("user", "A");

    FileOutputStream out = new FileOutputStream("user.props");
    p.storeToXML(out, "updated");

    FileInputStream in = new FileInputStream("user.props");

    p.loadFromXML(in);/*w  w  w.j a v a 2  s . c o m*/
    p.list(System.out);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();// ww  w . jav a2s.c  om

    // From file
    InputStream is = new BufferedInputStream(new FileInputStream(new File("midifile")));

    // From URL
    //    is = new BufferedInputStream(new URL("http://hostname/rmffile")
    //      .openStream());

    sequencer.setSequence(is);

    // Start playing
    sequencer.start();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();/*from   w  w  w  . j a v a  2 s  .  com*/

    // From file
    InputStream input = new BufferedInputStream(new FileInputStream(new File("midiaudiofile")));

    // From URL
    input = new BufferedInputStream(new URL("http://hostname/rmffile").openStream());

    sequencer.setSequence(input);

    // Start playing
    sequencer.start();
}