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) throws Exception {

    String s = "Hello World from java2s.com";
    byte[] b = { 'e', 'x', 'a', 'm', 'p', 'l', 'e' };

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeObject(s);/*from   www .  j  a  v  a 2 s .c  o m*/
    oout.writeObject(b);
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print an object and cast it as string
    System.out.println((String) ois.readObject());

    // read and print an object and cast it as string
    byte[] read = (byte[]) ois.readObject();
    String s2 = new String(read);
    System.out.println(s2);
    ois.close();
}

From source file:PrintFile.java

public static void main(String args[]) throws Exception {
    if (args.length != 2) {
        System.err.println("usage : java PrintFile port file");
        System.err.println("sample: java PrintFile LPT1 sample.prn");
        System.exit(-1);/*from   w  w w  .  j  a v  a 2  s  . com*/
    }

    String portname = args[0];
    String filename = args[1];

    // Get port
    CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portname);

    // Open port
    // Requires owner name and timeout
    CommPort port = portId.open("Java Printing", 30000);

    // Setup reading from file
    FileInputStream fis = new FileInputStream(filename);
    BufferedInputStream bis = new BufferedInputStream(fis);

    // Setup output
    OutputStream os = port.getOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(os);

    int c;
    while ((c = bis.read()) != -1) {
        bos.write(c);
    }

    // Close
    bos.close();
    bis.close();
    port.close();
}

From source file:Main.java

public static void main(String[] args) {
    boolean areFilesIdentical = true;
    File file1 = new File("c:\\file1.txt");
    File file2 = new File("c:\\file2.txt");

    if (!file1.exists() || !file2.exists()) {
        System.out.println("One or both files do not exist");
        System.out.println(false);
    }// w  ww .  ja  v  a 2 s.  co  m

    System.out.println("length:" + file1.length());
    if (file1.length() != file2.length()) {
        System.out.println("lengths not equal");
        System.out.println(false);
    }

    try {
        FileInputStream fis1 = new FileInputStream(file1);
        FileInputStream fis2 = new FileInputStream(file2);
        int i1 = fis1.read();
        int i2 = fis2.read();
        while (i1 != -1) {
            if (i1 != i2) {
                areFilesIdentical = false;
                break;
            }
            i1 = fis1.read();
            i2 = fis2.read();
        }
        fis1.close();
        fis2.close();
    } catch (IOException e) {
        System.out.println("IO exception");
        areFilesIdentical = false;
    }
    System.out.println(areFilesIdentical);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    ZipOutputStream fout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(args[0])));

    for (int n = 1; n < args.length; n++) {

        BufferedInputStream fin = new BufferedInputStream(new FileInputStream(args[n]));
        ZipEntry ze = new ZipEntry(rmPath(args[n]));

        fout.putNextEntry(ze);//  w  ww  .j  ava2 s . c  o m

        int i;
        do {
            i = fin.read();
            if (i != -1)
                fout.write(i);
        } while (i != -1);

        fout.closeEntry();
        fin.close();

        System.out.println("Compressing " + args[n]);
        System.out.println(
                " Original Size: " + ze.getSize() + " Compressed Size: " + ze.getCompressedSize() + "\n");
    }
    fout.close();
}

From source file:Main.java

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

    String s = "Hello World from java2s.com";

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeUTF(s);//from   ww w. j  av a  2 s.c om
    oout.flush();

    // create an ObjectInputStream for the file we created before
    Main ois = new Main(new FileInputStream("test.txt"));

    // enable object resolving
    ois.enableResolveObject(true);

    // get the class for string and print the name
    System.out.println(ois.resolveObject(ois.readUTF()));
    ois.close();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "I", "P", "E", "G", "P" };
    Set set = new HashSet(Arrays.asList(elements));
    Set set2 = ((Set) ((HashSet) set).clone());
    System.out.println(set2);//from  w ww .  j av  a 2  s.  co  m
    FileOutputStream fos = new FileOutputStream("set.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(set);
    oos.close();
    FileInputStream fis = new FileInputStream("set.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Set set3 = (Set) ois.readObject();
    ois.close();
    System.out.println(set3);
}

From source file:NewMain.java

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

    FileInputStream myStream = new FileInputStream("D:\\logout.png");

    byte[] imageInBytes = IOUtils.toByteArray(myStream);

    System.out.println(imageInBytes);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File source = new File(args[0]);
    File target = new File(source + ".new");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    InputStream in = new FileInputStream(source);
    XMLEventReader reader = inputFactory.createXMLEventReader(in);

    OutputStream out = new FileOutputStream(target);
    XMLEventWriter writer = outputFactory.createXMLEventWriter(out);
    XMLEvent event;/*  w  w  w. j  a  va2 s  .  c  o  m*/

    boolean deleteSection = false;
    while (reader.hasNext()) {
        event = reader.nextEvent();
        if (event.getEventType() == XMLStreamConstants.START_ELEMENT
                && event.asStartElement().getName().toString().equalsIgnoreCase("companies")) {
            deleteSection = true;
            continue;
        } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT
                && (event.asEndElement().getName().toString().equalsIgnoreCase("companies"))) {
            deleteSection = false;
            continue;
        } else if (deleteSection) {
            continue;
        } else {
            writer.add(event);
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = docBuilderFactory.newDocumentBuilder();
    Document node = documentBuilder.parse(new FileInputStream("data.xml"));
    cleanEmptyTextNodes(node);/*  w  w  w  .j a va2  s .c  om*/
    StreamResult result = new StreamResult(new StringWriter());

    Transformer transformer = TransformerFactory.newInstance().newTransformer();

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(4));

    transformer.transform(new DOMSource(node), result);
    System.out.println(result.getWriter().toString());
}

From source file:MainClass.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("Constructing objects:");
    MyBean myBean = new MyBean("A String ", 47);
    System.out.println(myBean);/*w ww  . j  a v a 2 s.  c o  m*/
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("MyBean.out"));
    System.out.println("Saving object:");
    o.writeObject(myBean);
    o.close();

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("MyBean.out"));
    System.out.println("Recovering:");
    myBean = (MyBean) in.readObject();
    System.out.println(myBean);
}