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

public static void main(String[] args) {

    char[] arr = { 'H', 'e', 'l', 'l', 'o' };

    try {/*ww w . j a  v a 2  s. c  om*/

        OutputStream os = new FileOutputStream("test.txt");
        OutputStreamWriter writer = new OutputStreamWriter(os);

        // create a new FileInputStream to read what we write
        FileInputStream in = new FileInputStream("test.txt");

        // write something in the file
        writer.write(arr, 0, 3);

        // flush the stream
        writer.flush();

        // read what we write
        for (int i = 0; i < 3; i++) {
            System.out.print((char) in.read());
        }

        writer.close();
        in.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {

    byte b = 66;//from ww  w  .  j  a v  a 2  s .  c  om
    int i = 0;
    FileOutputStream fos = new FileOutputStream(new File("C://test1.txt"));

    fos.write(b);

    fos.flush();

    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
        // convert integer to character
        char c = (char) i;

        System.out.print(c);
    }
    fos.close();
    fis.close();
}

From source file:MainClass.java

public static void main(String[] args) {
    File aFile = new File("primes.txt");
    FileInputStream inFile = null;
    try {/*from   ww w.j a va  2 s.c om*/
        inFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel inChannel = inFile.getChannel();
    try {
        ByteBuffer lengthBuf = ByteBuffer.allocate(8);
        while (true) {
            if (inChannel.read(lengthBuf) == -1) {
                break;
            }
            lengthBuf.flip();
            int strLength = (int) lengthBuf.getDouble();
            ByteBuffer buf = ByteBuffer.allocate(2 * strLength + 8);
            if (inChannel.read(buf) == -1) {
                break;
            }
            buf.flip();
            byte[] strChars = new byte[2 * strLength];
            buf.get(strChars);
            System.out.println(strLength);
            System.out.println(ByteBuffer.wrap(strChars).asCharBuffer());
            System.out.println(buf.getLong());
            lengthBuf.clear();
        }
        inFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {

    byte b = 66;//from   w  w  w . j ava 2s .c om
    int i = 0;
    FileOutputStream fos = new FileOutputStream(new File("C://test1.txt"), true);

    fos.write(b);

    fos.flush();

    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
        // convert integer to character
        char c = (char) i;

        System.out.print(c);
    }
    fos.close();
    fis.close();
}

From source file:Main.java

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

    props = new java.util.Properties();
    String path = new Main().getClass().getProtectionDomain().getCodeSource().getLocation().toString()
            .substring(6);/* w ww  . j a  va  2s  . co  m*/
    FileInputStream fis = new FileInputStream(new File(path + "\\myprops.props"));
    props.load(fis);
    System.out.println(props);

}

From source file:MainClass.java

public static void main(String[] args) {

    for (int i = 0; i < args.length; i++) {
        try {//  w  w w.j  a va  2s  .  co m
            InputStream fin = new FileInputStream(args[i]);
            OutputStream fout = new FileOutputStream(args[i] + GZIP_SUFFIX);
            GZIPOutputStream gzout = new GZIPOutputStream(fout);
            for (int c = fin.read(); c != -1; c = fin.read()) {
                gzout.write(c);
            }
            gzout.close();
        } catch (IOException ex) {
            System.err.println(ex);
        }
    }
}

From source file:NIOCopy.java

public static void main(String args[]) throws Exception {
    FileInputStream fIn;//from   ww w . ja v a 2s . c o  m
    FileOutputStream fOut;
    FileChannel fIChan, fOChan;
    long fSize;
    MappedByteBuffer mBuf;

    fIn = new FileInputStream(args[0]);
    fOut = new FileOutputStream(args[1]);

    fIChan = fIn.getChannel();
    fOChan = fOut.getChannel();
    fSize = fIChan.size();

    mBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
    fOChan.write(mBuf); // this copies the file
    fIChan.close();
    fIn.close();

    fOChan.close();
    fOut.close();
}

From source file:Main.java

public static void main(String[] args) throws IOException {

    byte b = 66;/*from  ww w.  j  a  va 2s  .com*/
    int i = 0;
    FileOutputStream fos = new FileOutputStream(FileDescriptor.out);

    fos.write(b);

    fos.flush();

    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
        // convert integer to character
        char c = (char) i;

        System.out.print(c);
    }
    fos.close();
    fis.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InputStream is = new BufferedInputStream(new FileInputStream("s.gif"));
    Image image = ImageIO.read(is);

    JFrame frame = new JFrame();
    JLabel label = new JLabel(new ImageIcon(image));
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.pack();/* w ww. j  av a2  s.c om*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    String s = "from java2s.com!";

    try {// w  w w.  j  a  v a 2s .co  m

        OutputStream os = new FileOutputStream("test.txt");
        OutputStreamWriter writer = new OutputStreamWriter(os);

        FileInputStream in = new FileInputStream("test.txt");

        writer.write(s, 0, 5);

        writer.flush();

        for (int i = 0; i < 5; i++) {
            System.out.print((char) in.read());
        }
        writer.close();
        in.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}