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

public static void main(String[] args) throws Exception {
    File file = new File("text.xml");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(file));

    System.out.println("Unfiltered Count = " + countEvents(reader));

    reader = inputFactory.createXMLEventReader(new FileInputStream(file));

    EventFilter filter = new ElementOnlyFilter();
    reader = inputFactory.createFilteredReader(reader, filter);

    System.out.println("Filtered Count = " + countEvents(reader));
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    String outputFile = "new.zip";
    // Default to maximum compression
    int level = 9;
    int start = 1;

    FileOutputStream fout = new FileOutputStream(outputFile);
    ZipOutputStream zout = new ZipOutputStream(fout);
    zout.setLevel(level);//from w w w  .j a  v  a 2 s .com
    for (int i = start; i < args.length; i++) {
        ZipEntry ze = new ZipEntry(args[i]);
        FileInputStream fin = new FileInputStream(args[i]);
        try {
            System.out.println("Compressing " + args[i]);
            zout.putNextEntry(ze);
            for (int c = fin.read(); c != -1; c = fin.read()) {
                zout.write(c);
            }
        } finally {
            fin.close();
        }
    }
    zout.close();
}

From source file:MainClass.java

public static void main(String[] args) {
    try {/*from   w  w  w.jav  a  2  s  .co  m*/
        char[] chars = new char[2];
        chars[0] = '\u4F60';
        chars[1] = '\u597D';
        String encoding = "GB18030";
        File textFile = new File("C:\\temp\\myFile.txt");
        PrintWriter writer = new PrintWriter(textFile,

                encoding);
        writer.write(chars);
        writer.close();

        // read back
        InputStreamReader reader = new InputStreamReader(new FileInputStream(textFile), encoding);
        char[] chars2 = new char[2];
        reader.read(chars2);
        System.out.print(chars2[0]);
        System.out.print(chars2[1]);
        reader.close();
    } catch (IOException e) {
        System.out.println(e.toString());
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    List mylist = new ArrayList();
    FileInputStream in = new FileInputStream(args[0]);
    Certificate c = cf.generateCertificate(in);
    mylist.add(c);//from  w w w .  j a  va  2 s. co  m

    CertPath cp = cf.generateCertPath(mylist);

    Certificate trust = cf.generateCertificate(in);
    TrustAnchor anchor = new TrustAnchor((X509Certificate) trust, null);
    PKIXParameters params = new PKIXParameters(Collections.singleton(anchor));
    params.setRevocationEnabled(false);
    CertPathValidator cpv = CertPathValidator.getInstance("PKIX");
    PKIXCertPathValidatorResult result = (PKIXCertPathValidatorResult) cpv.validate(cp, params);
    System.out.println(result);
}

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);

    oout.writeUTF(s);//w w w.  ja v a  2s .  c om
    oout.flush();
    oout.close();

    Main ois = new Main(new FileInputStream("test.txt"));

    String[] list = { Serializable.class.getName() };

    System.out.println(ois.resolveProxyClass(list));
    ois.close();
}

From source file:Redirecting.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);// w  w  w .  ja va  2s .  c  o  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(); // Remember this!
    System.setOut(console);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Class.forName("COM.cloudscape.core.JDBCDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:cloudscape:GAMETRADER");

    conn.setAutoCommit(false);/*  w  ww.j a v  a  2 s .c o m*/

    Statement s = conn.createStatement();
    s.executeUpdate("CREATE TABLE MANUALS(GAMEID INT, MANUAL LONG VARCHAR)");
    conn.commit();

    File file = new File("manuals.xml");
    InputStream is = new FileInputStream(file);

    PreparedStatement ps = conn.prepareStatement("INSERT INTO MANUALS VALUES(?,?)");

    ps.setInt(1, 1285757);

    ps.setAsciiStream(2, is, (int) file.length());

    ps.execute();
    conn.commit();
}

From source file:BufferToText.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new FileOutputStream("data2.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text".getBytes()));
    fc.close();//from w  w  w.j a va 2 s  . c  o  m
    fc = new FileInputStream("data2.txt").getChannel();
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    fc.read(buff);
    buff.flip();
    // Doesn't work:
    System.out.println(buff.asCharBuffer());
    // Decode using this system's default Charset:
    buff.rewind();
    String encoding = System.getProperty("file.encoding");
    System.out.println("Decoded using " + encoding + ": " + Charset.forName(encoding).decode(buff));
    // Or, we could encode with something that will print:
    fc = new FileOutputStream("data2.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text".getBytes("UTF-16BE")));
    fc.close();
    // Now try reading again:
    fc = new FileInputStream("data2.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(buff.asCharBuffer());
    // Use a CharBuffer to write through:
    fc = new FileOutputStream("data2.txt").getChannel();
    buff = ByteBuffer.allocate(24); // More than needed
    buff.asCharBuffer().put("Some text");
    fc.write(buff);
    fc.close();
    // Read and display:
    fc = new FileInputStream("data2.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(buff.asCharBuffer());
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SSLContext context;// ww w . j a v a2s.com
    KeyManagerFactory kmf;
    KeyStore ks;
    char[] storepass = "newpass".toCharArray();
    char[] keypass = "wshr.ut".toCharArray();
    String storename = "newstore";

    context = SSLContext.getInstance("TLS");
    kmf = KeyManagerFactory.getInstance("SunX509");
    FileInputStream fin = new FileInputStream(storename);
    ks = KeyStore.getInstance("JKS");
    ks.load(fin, storepass);

    kmf.init(ks, keypass);
    context.init(kmf.getKeyManagers(), null, null);
    SSLServerSocketFactory ssf = context.getServerSocketFactory();

    ServerSocket ss = ssf.createServerSocket(5432);
    while (true) {
        Socket s = ss.accept();
        PrintStream out = new PrintStream(s.getOutputStream());
        out.println("Hi");
        out.close();
        s.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  .j  a  v a2 s  .  com*/
    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);
}