List of usage examples for java.io FileInputStream read
public int read(byte b[], int off, int len) throws IOException
len
bytes of data from this input stream into an array of bytes. 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;/* w w w . jav a2 s . c om*/ 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) throws IOException { byte[] bs = new byte[4]; FileInputStream fis = new FileInputStream("C://test.txt"); // read bytes to the buffer int i = fis.read(bs, 2, 1); System.out.println("Number of bytes read: " + i); // for each byte in buffer for (byte b : bs) { // converts byte to character char c = (char) b; if (b == 0) { c = '-'; }//from w w w . ja va 2 s .co m System.out.println(c); } }
From source file:MainClass.java
public static void main(String[] args) { int bufferSize = 8192; // create output stream String sourceFileName = "data.txt"; String zipname = sourceFileName + ".gz"; GZIPOutputStream zipout;/*from w w w . j ava 2 s .c o m*/ try { FileOutputStream out = new FileOutputStream(zipname); zipout = new GZIPOutputStream(out); } catch (IOException e) { System.out.println("Couldn't create " + zipname + "."); return; } byte[] buffer = new byte[bufferSize]; // compress the file try { FileInputStream in = new FileInputStream(sourceFileName); int length; while ((length = in.read(buffer, 0, bufferSize)) != -1) zipout.write(buffer, 0, length); in.close(); } catch (IOException e) { System.out.println("Couldn't compress " + sourceFileName + "."); } try { zipout.close(); } catch (IOException e) { } }
From source file:FileSplitter.java
public static void main(String args[]) throws Exception { FileInputStream fis = new FileInputStream(args[0]); int size = 1024; byte buffer[] = new byte[size]; int count = 0; while (true) { int i = fis.read(buffer, 0, size); if (i == -1) break; String filename = args[1] + count; FileOutputStream fos = new FileOutputStream(filename); fos.write(buffer, 0, i);/* ww w .jav a2 s .c o m*/ fos.flush(); fos.close(); ++count; } }
From source file:Main.java
public static void main(String[] args) throws Exception { CheckedOutputStream checksum = new CheckedOutputStream(new FileOutputStream("data.zip"), new Adler32()); ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum)); int size = 0; byte[] buffer = new byte[1024]; File dir = new File("."); String[] files = dir.list();//from w w w . ja v a 2 s .c o m for (int i = 0; i < files.length; i++) { System.out.println("Compressing: " + files[i]); FileInputStream fis = new FileInputStream(files[i]); ZipEntry zipEntry = new ZipEntry(files[i]); zos.putNextEntry(zipEntry); while ((size = fis.read(buffer, 0, buffer.length)) > 0) { zos.write(buffer, 0, size); } zos.closeEntry(); fis.close(); } zos.close(); System.out.println("Checksum : " + checksum.getChecksum().getValue()); }
From source file:org.vuphone.vandyupon.test.ImagePostTester.java
public static void main(String[] args) { try {// ww w.j a v a2 s .c om File image = new File("2008-05-8 ChrisMikeNinaGrad.jpg"); byte[] bytes = new byte[(int) image.length()]; FileInputStream fis = new FileInputStream(image); int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = fis.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } fis.close(); HttpClient c = new DefaultHttpClient(); HttpPost post = new HttpPost( "http://afrl-gift.dre.vanderbilt.edu:8080/vandyupon/events/?type=eventimagepost&eventid=2" + "&time=" + System.currentTimeMillis() + "&resp=xml"); post.addHeader("Content-Type", "image/jpeg"); ByteArrayEntity bae = new ByteArrayEntity(bytes); post.setEntity(bae); try { HttpResponse resp = c.execute(post); resp.getEntity().writeTo(System.out); } catch (IOException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } }
From source file:UDPSend.java
public static void main(String args[]) { try {//from w w w. j a va 2 s.co m // Check the number of arguments if (args.length < 3) throw new IllegalArgumentException("Wrong number of args"); // Parse the arguments String host = args[0]; int port = Integer.parseInt(args[1]); // Figure out the message to send. // If the third argument is -f, then send the contents of the file // specified as the fourth argument. Otherwise, concatenate the // third and all remaining arguments and send that. byte[] message; if (args[2].equals("-f")) { File f = new File(args[3]); int len = (int) f.length(); // figure out how big the file is message = new byte[len]; // create a buffer big enough FileInputStream in = new FileInputStream(f); int bytes_read = 0, n; do { // loop until we've read it all n = in.read(message, bytes_read, len - bytes_read); bytes_read += n; } while ((bytes_read < len) && (n != -1)); } else { // Otherwise, just combine all the remaining arguments. String msg = args[2]; for (int i = 3; i < args.length; i++) msg += " " + args[i]; // Convert the message to bytes using UTF-8 encoding message = msg.getBytes("UTF-8"); } // Get the internet address of the specified host InetAddress address = InetAddress.getByName(host); // Initialize a datagram packet with data and address DatagramPacket packet = new DatagramPacket(message, message.length, address, port); // Create a datagram socket, send the packet through it, close it. DatagramSocket dsocket = new DatagramSocket(); dsocket.send(packet); dsocket.close(); } catch (Exception e) { System.err.println(e); System.err.println(usage); } }
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));//from ww w. ja 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 boolean isWAVFile(String fileName) { byte header[] = new byte[16]; try {/*from w w w .j av a 2 s . c o m*/ File f = new File(fileName); if (!f.exists()) { Log.d("OpusTool", fileName + ":" + "File does not exist."); return false; } long actualLength = f.length(); FileInputStream io = new FileInputStream(f); io.read(header, 0, 16); io.close(); String tag = new String(header, 0, 4) + new String(header, 8, 8); if (!tag.equals("RIFFWAVEfmt ")) { Log.d("OpusTool", fileName + ":" + "It's not a WAV file!"); return false; } long paraLength = (header[4] & 0x000000ff) | ((header[5] << 8) & 0x0000ff00) | ((header[6] << 16) & 0x00ff0000) | ((header[7] << 24) & 0xff000000); if (paraLength != actualLength - 8) { Log.d("OpusTool", fileName + ":" + "It might be a WAV file, but it's corrupted!"); return false; } return true; } catch (Exception e) { Log.d("OpusTool", fileName + ":" + "File Error"); return false; } }
From source file:Main.java
static byte[] read(String fname) throws Exception { long offset = 0; File f = new File(fname); long length = f.length(); byte[] image = new byte[(int) length]; FileInputStream fis = new FileInputStream(f); while (offset < length) { offset += fis.read(image, (int) offset, (int) (length - offset)); }//from w w w .j a v a 2 s . c o m fis.close(); return image; }