Mega Code Archive

 
Categories / Java / File Input Output
 

Some simple file I-O primitives reimplemented in Java

/*  * Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.  * All rights reserved. Software written by Ian F. Darwin and others.  * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $  *  * Redistribution and use in source and binary forms, with or without  * modification, are permitted provided that the following conditions  * are met:  * 1. Redistributions of source code must retain the above copyright  *    notice, this list of conditions and the following disclaimer.  * 2. Redistributions in binary form must reproduce the above copyright  *    notice, this list of conditions and the following disclaimer in the  *    documentation and/or other materials provided with the distribution.  *  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  * POSSIBILITY OF SUCH DAMAGE.  *   * Java, the Duke mascot, and all variants of Sun's Java "steaming coffee  * cup" logo are trademarks of Sun Microsystems. Sun's, and James Gosling's,  * pioneering role in inventing and promulgating (and standardizing) the Java   * language and environment is gratefully acknowledged.  *   * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for  * inventing predecessor languages C and C++ is also gratefully acknowledged.  */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; /**  * Some simple file I-O primitives reimplemented in Java. All methods are  * static, since there is no state.  *   * @version $Id: FileIO.java,v 1.18 2004/05/30 01:39:27 ian Exp $  */ public class FileIO {   /** Nobody should need to create an instance; all methods are static */   private FileIO() {     // Nothing to do   }   /** Copy a file from one filename to another */   public static void copyFile(String inName, String outName)       throws FileNotFoundException, IOException {     BufferedInputStream is = new BufferedInputStream(new FileInputStream(         inName));     BufferedOutputStream os = new BufferedOutputStream(         new FileOutputStream(outName));     copyFile(is, os, true);   }   /** Copy a file from an opened InputStream to opened OutputStream */   public static void copyFile(InputStream is, OutputStream os, boolean close)       throws IOException {     byte[] b = new byte[BLKSIZ]; // the byte read from the file     int i;     while ((i = is.read(b)) != -1) {       os.write(b, 0, i);     }     is.close();     if (close)       os.close();   }   /** Copy a file from an opened Reader to opened Writer */   public static void copyFile(Reader is, Writer os, boolean close)       throws IOException {     int b; // the byte read from the file     BufferedReader bis = new BufferedReader(is);     while ((b = is.read()) != -1) {       os.write(b);     }     is.close();     if (close)       os.close();   }   /** Copy a file from a filename to a PrintWriter. */   public static void copyFile(String inName, PrintWriter pw, boolean close)       throws FileNotFoundException, IOException {     BufferedReader ir = new BufferedReader(new FileReader(inName));     copyFile(ir, pw, close);   }   /** Open a file and read the first line from it. */   public static String readLine(String inName) throws FileNotFoundException,       IOException {     BufferedReader is = new BufferedReader(new FileReader(inName));     String line = null;     line = is.readLine();     is.close();     return line;   }   /** The size of blocking to use */   protected static final int BLKSIZ = 16384;   /**    * Copy a data file from one filename to another, alternate method. As the    * name suggests, use my own buffer instead of letting the BufferedReader    * allocate and use the buffer.    */   public void copyFileBuffered(String inName, String outName)       throws FileNotFoundException, IOException {     InputStream is = new FileInputStream(inName);     OutputStream os = new FileOutputStream(outName);     int count = 0; // the byte count     byte[] b = new byte[BLKSIZ]; // the bytes read from the file     while ((count = is.read(b)) != -1) {       os.write(b, 0, count);     }     is.close();     os.close();   }   /** Read the entire content of a Reader into a String */   public static String readerToString(Reader is) throws IOException {     StringBuffer sb = new StringBuffer();     char[] b = new char[BLKSIZ];     int n;     // Read a block. If it gets any chars, append them.     while ((n = is.read(b)) > 0) {       sb.append(b, 0, n);     }     // Only construct the String object once, here.     return sb.toString();   }   /** Read the content of a Stream into a String */   public static String inputStreamToString(InputStream is) throws IOException {     return readerToString(new InputStreamReader(is));   }   /** Write a String as the entire content of a File */   public static void stringToFile(String text, String fileName)       throws IOException {     BufferedWriter os = new BufferedWriter(new FileWriter(fileName));     os.write(text);     os.flush();     os.close();   }   /** Open a BufferedReader from a named file. */   public static BufferedReader openFile(String fileName) throws IOException {     return new BufferedReader(new FileReader(fileName));   } }