FileDocCategorySizeDatePackage
FileIO.javaAPI DocExample3596Tue Jan 29 10:56:38 GMT 2002com.darwinsys.util

FileIO

public class FileIO extends Object
Some simple file I-O primitives reimplemented in Java. All methods are static, since there is no state.
version
$Id: FileIO.java,v 1.14 2002/01/29 15:56:38 ian Exp $

Fields Summary
protected static final int
BLKSIZ
The size of blocking to use
Constructors Summary
private FileIO()
Nobody should need to create an instance; all methods are static

 
Methods Summary
public static voidcopyFile(java.lang.String inName, java.lang.String outName)
Copy a file from one filename to another

		BufferedInputStream is = 
			new BufferedInputStream(new FileInputStream(inName));
		BufferedOutputStream os = 
			new BufferedOutputStream(new FileOutputStream(outName));
		copyFile(is, os, true);
	
public static voidcopyFile(java.io.InputStream is, java.io.OutputStream os, boolean close)
Copy a file from an opened InputStream to opened OutputStream

		int b;				// the byte read from the file
		while ((b = is.read()) != -1) {
			os.write(b);
		}
		is.close();
		if (close)
			os.close();
    
public static voidcopyFile(java.io.Reader is, java.io.Writer os, boolean close)
Copy a file from an opened Reader to opened Writer

		int b;				// the byte read from the file
		while ((b = is.read()) != -1) {
			os.write(b);
		}
		is.close();
		if (close)
			os.close();
    
public static voidcopyFile(java.lang.String inName, java.io.PrintWriter pw, boolean close)
Copy a file from a filename to a PrintWriter.

		BufferedReader is = new BufferedReader(new FileReader(inName));
		copyFile(is, pw, close);
	
public voidcopyFileBuffered(java.lang.String inName, java.lang.String outName)
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.


	                             	 
	      
			  
		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();
	
public static java.lang.StringinputStreamToString(java.io.InputStream is)
Read the content of a Stream into a String

		return readerToString(new InputStreamReader(is));
	
public static java.io.BufferedReaderopenFile(java.lang.String fileName)
Open a BufferedReader from a named file.

		return new BufferedReader(new FileReader(fileName));
	
public static java.lang.StringreadLine(java.lang.String inName)
Open a file and read the first line from it.

		BufferedReader is = new BufferedReader(new FileReader(inName));
		String line = null;
		line = is.readLine();
		is.close();
		return line;
	
public static java.lang.StringreaderToString(java.io.Reader is)
Read the entire content of a Reader into a String

		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();
	
public static voidstringToFile(java.lang.String text, java.lang.String fileName)
Write a String as the entire content of a File

		BufferedWriter os = new BufferedWriter(new FileWriter(fileName));
		os.write(text);
		os.flush();
		os.close();