我将文件的内容读到一InputStream中。请问(急)......
本文导语: 如下: InputStream inputStream = getClass().getResourceAsStream("dao.txt"); 请问如何得到dao.txt文件的大小,或得到InputStream的大小或长度?急! | import java.io.*; import java.util.zip.*; import java.util.*; /** Provides us...
InputStream inputStream = getClass().getResourceAsStream("dao.txt");
请问如何得到dao.txt文件的大小,或得到InputStream的大小或长度?急!
|
import java.util.zip.*;
import java.util.*;
/**
Provides useful operations on files. Most of the operations are intended
only for text files.
@date: 7/6/00
@author G. Bright
**/
public class FileTool
{
//_______________________________________________________________________
/** Append a given string to the text file with the given filename.
@param strstring to append to file
@param filenamename of file
**/
public void appendToFile(String str, String filename) throws Exception
{
// Open up an outputstream writer to the file, and append str to it.
FileOutputStream stream;//provides file access
OutputStreamWriter writer;//writes to the file
try
{
stream = new FileOutputStream(filename, true);
writer = new OutputStreamWriter(stream);
writer.write(str);
writer.close();
stream.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//appendToFile
//_______________________________________________________________________
//_______________________________________________________________________
/** Prepend a given string to the text file with the given filename.
@param strstring to prepend to file
@param filenamename of file
**/
public void prependToFile(String str, String filename) throws Exception
{
// Read the file, then prepend str to the file text and write back
// to the file.
String newStr;//final string to be written
try
{
newStr = str + readFile(filename);
writeFile(newStr, filename);
}//try
catch(Exception e)
{
throw e;
}//catch
}//prependToFile
//_______________________________________________________________________
//_______________________________________________________________________
/** Read the text file with the given filename and return as a string.
@param filenamename of file to read
@returnstring of text of file
**/
public String readFile(String filename) throws Exception
{
//Read the file into a string buffer, then return as a string.
StringBuffer buf;//the intermediary, mutable buffer
BufferedReader breader;//reader for the template files
try
{
breader = new BufferedReader(new FileReader(filename));//header
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return buf.toString();
}//readFile
//_______________________________________________________________________
//_______________________________________________________________________
/** Read the given text file and return as a vector of lines.
@param filenamename of file to read
@returnvector, in which the ith element is a string of the
ith line of the text file.
**/
public List readFileToList(String filename) throws Exception
{
//Read the file into a List, then return.
BufferedReader breader;//reader for the template files
List list;//target vector
String line;//line from file
list = new ArrayList();
try
{
breader = new BufferedReader(new FileReader(filename));//header
while((line = breader.readLine()) != null)
list.add(line);
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return list;
}//readFileToVector
//_______________________________________________________________________
//_______________________________________________________________________
/** Write the given string to the file with the given filename.
@param strstring to write to file
@param filenamename of file
Note: The pre-existing contents of the file are completely over written.
**/
public void writeFile(String str, String filename) throws Exception
{
// Open a writer to the file, then write the string.
BufferedWriter bwriter;//writer to the file
String fullfilepath;//path for the output file
try
{
bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
bwriter.write(str);
bwriter.flush();
bwriter.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//writeFile
//_______________________________________________________________________
//_______________________________________________________________________
/** Copy a file to another location.
@param sourcenamename of file to copy
@param targetnamename of file to copy to
**/
public void copyFile(String sourcename, String targetname) throws Exception
{
// Open up a reader from sourcename and a writer to targetname.
// Write each character from sourcename to targetname, then close.
BufferedReader breader;//reader from source
BufferedWriter bwriter;//writer to target
try
{
breader = new BufferedReader(new FileReader(sourcename));
bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetname)));
while(breader.ready())
bwriter.write(breader.read());
breader.close();
bwriter.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//copyFile
//_______________________________________________________________________
//_______________________________________________________________________
/** Read a text file from a given zip file and return as a string.
@param textFilenamethe zipped text file to read
@param zipFilenamezipp file to open
@returntext of the file
**/
public String readZippedFile(String textFilename, String zipFilename) throws Exception
{
// Open a stream from the entry in the zip file for the file.
// Read the file into a string buffer, then return as a string.
StringBuffer buf;//the intermediary, mutable buffer
BufferedReader breader;//reader for the template files
ZipFile zipFile;//zip file
ZipEntry zipEntry;//entry in zip
InputStream iStream;//input stream of file
try
{
zipFile = new ZipFile(zipFilename);
zipEntry = zipFile.getEntry(textFilename);
iStream = zipFile.getInputStream(zipEntry);
breader = new BufferedReader(new InputStreamReader(iStream));
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return buf.toString();
}//readZippedFile
//_______________________________________________________________________
//______________________________________________________________
/** Read a line from System.in. (block)
@return input read from console.
**/
public String readConsole() throws Exception
{
// Create a buffered reader with System.in, then
// read a line from it.
BufferedReader breader;
breader = new BufferedReader(new InputStreamReader(System.in));
return breader.readLine();
}//readConsole
//______________________________________________________________
//______________________________________________________________
/** Return a properties object loaded from the given file.
@parampropsFilename of props file to load
@return properties object from file
*/
public Properties loadProperties(String propsFile) throws Exception
{
// Open up an input stream from the file, then read it into
// a properties hashtable and return.
FileInputStream instream;//the input stream
Properties props;//props object
instream = null;
props = new Properties();
try
{
instream = new FileInputStream(propsFile);
props.load(instream);
}//try
finally
{
try
{
instream.close();
}//try
catch(Exception e1)
{
}
}//finally
return props;
}//loadProperties
//______________________________________________________________
}//FileTool