List of usage examples for java.io FileInputStream read
public int read() throws IOException
From source file:Main.java
public static void main(String[] args) { char[] arr = { 'H', 'e', 'l', 'l', 'o' }; try {/* w w w .j a va 2s . com*/ 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:ShowFile.java
public static void main(String args[]) throws IOException { int i;//from w w w . j a va2 s.c o m FileInputStream fin; try { fin = new FileInputStream(args[0]); } catch (FileNotFoundException e) { System.out.println("File Not Found"); return; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Usage: ShowFile File"); return; } do { i = fin.read(); if (i != -1) System.out.print((char) i); } while (i != -1); fin.close(); }
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:Main.java
public static void main(String[] args) { try {/* w w w. ja v a2s. 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); // flush the stream writer.flush(); // get and print the encoding for this stream System.out.println(writer.getEncoding()); // read what we write System.out.println((char) in.read()); writer.close(); os.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
/** * Helper method//from w w w . j a v a 2 s .c o m * <p/> * Used by {@link OfflineUtilities#getUncomplessedOfflineFile(FileInputStream)} * Takes in the input stream for the offline storage and reads the contents into a byte array * * @param file The input stream for the offline storage {@see android.content.Context#openFileInput(String)} * @return The byte array representation of the offline file * @throws IOException If something goes wrong with the file reading */ private static byte[] fileToArr(FileInputStream file) throws IOException { List<Byte> tripFile = new ArrayList<>(); for (byte b; (b = (byte) file.read()) != -1;) { tripFile.add(b); } byte[] tripBytes = new byte[tripFile.size()]; int pos = 0; for (Byte b : tripFile) { tripBytes[pos++] = b.byteValue(); } return tripBytes; }
From source file:Main.java
/** * Simple copy. Horrible performance for large files. * Good performance for alphabets.//from w w w . ja va 2 s. co m */ public static void copyFile(File source, File dest) throws Exception { FileInputStream fis = new FileInputStream(source); try { FileOutputStream fos = new FileOutputStream(dest); try { int read = fis.read(); while (read != -1) { fos.write(read); read = fis.read(); } } finally { fos.close(); } } finally { fis.close(); } }
From source file:controller.file.FileUploader.java
public static void fileDownloader(HttpServletRequest request, HttpServletResponse response) { PrintWriter out = null;//from w w w . j av a 2s .c om try { String filename = "foo.xml"; String filepath = "/tmp/"; out = response.getWriter(); response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath + filename); int i; while ((i = fileInputStream.read()) != -1) { out.write(i); } fileInputStream.close(); out.close(); } catch (IOException ex) { Logger.getLogger(FileUploader.class.getName()).log(Level.SEVERE, null, ex); } finally { out.close(); } }
From source file:Main.java
/** * Use the file and count the chars in the file, so we can use static arrays * // w w w.j ava 2 s.c o m * @param fileName * @return integer with the size of the file */ public static int countBytes(String fileName) { FileInputStream fin; int charCount = 0; try { // Open an input stream fin = new FileInputStream(fileName); while (fin.available() != 0) { fin.read(); charCount++; } fin.close(); } // Catches any error conditions catch (IOException e) { System.err.println("Unable to read image"); System.exit(1); } return charCount; }
From source file:Main.java
public static int[] fileToIntArray(Context myContext, String filename, int size) { int[] array = new int[size]; int i = 0;// ww w. j a v a 2s .c o m FileInputStream inputStream; try { int c; inputStream = myContext.openFileInput(filename); StringWriter writer = new StringWriter(); while ((c = inputStream.read()) != -1) { writer.append((char) c); } String ints[] = writer.toString().split("\n"); for (String s : ints) { array[i++] = Integer.parseInt(s); } } catch (Exception e) { e.printStackTrace(); } return array; }
From source file:Main.java
private static boolean isOptedOut(Context appContext, boolean shouldAsk) { boolean optedOut = false; FileInputStream in = null; try {//from w ww . j av a 2 s . c o m in = appContext.openFileInput(QCMEASUREMENT_OPTOUT_STRING); optedOut = in.read() != 0; } catch (FileNotFoundException e) { if (shouldAsk) askEveryone(appContext, false, false); } catch (IOException ignored) { } finally { try { if (in != null) { in.close(); } } catch (IOException ignored) { } } return optedOut; }