List of usage examples for java.io FileInputStream close
public void close() throws IOException
From source file:Main.java
public static void main(String[] args) throws Exception { FileInputStream fin = null; FileOutputStream fout = null; File file = new File("C:/myfile1.txt"); fin = new FileInputStream(file); fout = new FileOutputStream("C:/myfile2.txt"); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fin.read(buffer)) > 0) { fout.write(buffer, 0, bytesRead); }/*from ww w . j a va2 s. c om*/ fin.close(); fout.close(); }
From source file:CopyBytes.java
public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null;//from ww w.j a va 2 s. c om try { in = new FileInputStream("xanadu.txt"); out = new FileOutputStream("outagain.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } }
From source file:MainClass.java
public static void main(String args[]) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); SecretKey key = KeyGenerator.getInstance("DES").generateKey(); // for CBC; must be 8 bytes byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 }; AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector); Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding"); Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding"); m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec); m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec); FileInputStream fis = new FileInputStream("cipherTest.in"); FileOutputStream fos = new FileOutputStream("cipherTest.out"); int dataInputSize = fis.available(); byte[] inputBytes = new byte[dataInputSize]; fis.read(inputBytes);/*from www . j av a 2s. co m*/ write(inputBytes, fos); fos.flush(); fis.close(); fos.close(); String inputFileAsString = new String(inputBytes); System.out.println("INPUT FILE CONTENTS\n" + inputFileAsString + "\n"); System.out.println("File encrypted and saved to disk\n"); fis = new FileInputStream("cipherTest.out"); byte[] decrypted = new byte[dataInputSize]; read(decrypted, fis); fis.close(); String decryptedAsString = new String(decrypted); System.out.println("DECRYPTED FILE:\n" + decryptedAsString + "\n"); }
From source file:Main.java
public static void main(String[] args) { char[] arr = { 'H', 'e', 'l', 'l', 'o' }; try {/*from w w w . j ava 2 s. co m*/ 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 Exception { FileInputStream fis = new FileInputStream("/home/username/data.txt"); int i = 0;/*from ww w. j av a 2s . c o m*/ int count = 0; while ((i = fis.read()) != -1) { if (i != -1) { System.out.printf("%02X ", i); count++; } if (count == 16) { System.out.println(""); count = 0; } } fis.close(); }
From source file:cc.twittertools.util.ExtractSubcollection.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withArgName("dir").hasArg().withDescription("source collection directory") .create(COLLECTION_OPTION)); options.addOption(//from w ww .j a va 2s. c o m OptionBuilder.withArgName("file").hasArg().withDescription("list of tweetids").create(ID_OPTION)); CommandLine cmdline = null; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); System.exit(-1); } if (!cmdline.hasOption(COLLECTION_OPTION) || !cmdline.hasOption(ID_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(ExtractSubcollection.class.getName(), options); System.exit(-1); } String collectionPath = cmdline.getOptionValue(COLLECTION_OPTION); LongOpenHashSet tweetids = new LongOpenHashSet(); File tweetidsFile = new File(cmdline.getOptionValue(ID_OPTION)); if (!tweetidsFile.exists()) { System.err.println("Error: " + tweetidsFile + " does not exist!"); System.exit(-1); } LOG.info("Reading tweetids from " + tweetidsFile); FileInputStream fin = new FileInputStream(tweetidsFile); BufferedReader br = new BufferedReader(new InputStreamReader(fin)); String s; while ((s = br.readLine()) != null) { tweetids.add(Long.parseLong(s)); } br.close(); fin.close(); LOG.info("Read " + tweetids.size() + " tweetids."); File file = new File(collectionPath); if (!file.exists()) { System.err.println("Error: " + file + " does not exist!"); System.exit(-1); } PrintStream out = new PrintStream(System.out, true, "UTF-8"); StatusStream stream = new JsonStatusCorpusReader(file); Status status; while ((status = stream.next()) != null) { if (tweetids.contains(status.getId())) { out.println(status.getJsonObject().toString()); } } stream.close(); out.close(); }
From source file:FileIOApp.java
public static void main(String args[]) throws IOException { FileOutputStream outStream = new FileOutputStream("test.txt"); String s = "This is a test."; for (int i = 0; i < s.length(); ++i) outStream.write(s.charAt(i));//ww w. j a v a 2 s . c o m outStream.close(); FileInputStream inStream = new FileInputStream("test.txt"); int inBytes = inStream.available(); System.out.println("inStream has " + inBytes + " available bytes"); byte inBuf[] = new byte[inBytes]; int bytesRead = inStream.read(inBuf, 0, inBytes); System.out.println(bytesRead + " bytes were read"); System.out.println("They are: " + new String(inBuf)); inStream.close(); File f = new File("test.txt"); f.delete(); }
From source file:Main.java
public static void main(String args[]) throws IOException, ClassNotFoundException { File file = new File("test.txt"); FileOutputStream outFile = new FileOutputStream(file); ObjectOutputStream outStream = new ObjectOutputStream(outFile); TestClass1 t1 = new TestClass1(true, 9, 'A', 0.0001, "java"); TestClass2 t2 = new TestClass2(); String t3 = "This is a test."; Date t4 = new Date(); outStream.writeObject(t1);// w ww .ja v a2 s .c om outStream.writeObject(t2); outStream.writeObject(t3); outStream.writeObject(t4); outStream.close(); outFile.close(); FileInputStream inFile = new FileInputStream(file); ObjectInputStream inStream = new ObjectInputStream(inFile); System.out.println(inStream.readObject()); System.out.println(inStream.readObject()); System.out.println(inStream.readObject()); System.out.println(inStream.readObject()); inStream.close(); inFile.close(); file.delete(); }
From source file:Main.java
public static void main(String[] args) { try {/*from w ww.j a v a 2 s. c o m*/ 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(70); writer.write(71); writer.write(72); // 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:cc.twittertools.util.VerifySubcollection.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withArgName("dir").hasArg().withDescription("source collection directory") .create(COLLECTION_OPTION)); options.addOption(/*w w w. j a v a 2 s . c o m*/ OptionBuilder.withArgName("file").hasArg().withDescription("list of tweetids").create(ID_OPTION)); CommandLine cmdline = null; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); System.exit(-1); } if (!cmdline.hasOption(COLLECTION_OPTION) || !cmdline.hasOption(ID_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(ExtractSubcollection.class.getName(), options); System.exit(-1); } String collectionPath = cmdline.getOptionValue(COLLECTION_OPTION); LongOpenHashSet tweetids = new LongOpenHashSet(); File tweetidsFile = new File(cmdline.getOptionValue(ID_OPTION)); if (!tweetidsFile.exists()) { System.err.println("Error: " + tweetidsFile + " does not exist!"); System.exit(-1); } LOG.info("Reading tweetids from " + tweetidsFile); FileInputStream fin = new FileInputStream(tweetidsFile); BufferedReader br = new BufferedReader(new InputStreamReader(fin)); String s; while ((s = br.readLine()) != null) { tweetids.add(Long.parseLong(s)); } br.close(); fin.close(); LOG.info("Read " + tweetids.size() + " tweetids."); File file = new File(collectionPath); if (!file.exists()) { System.err.println("Error: " + file + " does not exist!"); System.exit(-1); } LongOpenHashSet seen = new LongOpenHashSet(); TreeMap<Long, String> tweets = Maps.newTreeMap(); PrintStream out = new PrintStream(System.out, true, "UTF-8"); StatusStream stream = new JsonStatusCorpusReader(file); Status status; int cnt = 0; while ((status = stream.next()) != null) { if (!tweetids.contains(status.getId())) { LOG.error("tweetid " + status.getId() + " doesn't belong in collection"); continue; } if (seen.contains(status.getId())) { LOG.error("tweetid " + status.getId() + " already seen!"); continue; } tweets.put(status.getId(), status.getJsonObject().toString()); seen.add(status.getId()); cnt++; } LOG.info("total of " + cnt + " tweets in subcollection."); for (Map.Entry<Long, String> entry : tweets.entrySet()) { out.println(entry.getValue()); } stream.close(); out.close(); }