public class IOUtil extends Object
byte buff[] = ...
String str = new String(buff, IOUtil.UTF_8);
Pumping:
CharArrayWriter cout = new CharArrayWriter();
IOUtil.pump(new FileReader(file), cout, true, true);
String content = cout.toString();
To simplify code, pump(...) method returns output; So the above code could be written in single line as follows:
String content = IOUtil.pump(new FileReader(file), new CharArrayWriter(), true, true).toString();if output is not specified, it defaults to
CharArrayWriter2. so the same code can be written as:
String content = IOUtil.pump(new FileReader(file), true).toString();
Similar versions of pump(...) methods are available for byte-streams also;
IOUtil.pump(new FileInputStream(fromFile), new FileOutputStream(toFile), true, true);
To create zip file:
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
for(File file: files){
zipOut.putNextEntry(new ZipEntry(file.getName());
IOUtil.pump(new FileInputStream(file), zipOut, true, false); // note that last arg is false
zipOut.closeEntry();
}
zipOut.close();
To create file with given string:
String content = ...
IOUtil.pump(new StringReader(content), new FileWriter(file), true, true);
to read a file content into byte array:
byte bytes[] = IOUtil.pump(new FileInputStream(file), true).toByteArray(); // output defaults toByteArrayOutputStream2
| Modifier and Type | Field and Description |
|---|---|
static Charset |
ISO_8859_1 |
static Charset |
US_ASCII |
static Charset |
UTF_16 |
static Charset |
UTF_16BE |
static Charset |
UTF_16LE |
static Charset |
UTF_32 |
static Charset |
UTF_32BE |
static Charset |
UTF_32LE |
static Charset |
UTF_8 |
| Constructor and Description |
|---|
IOUtil() |
| Modifier and Type | Method and Description |
|---|---|
static ByteArrayOutputStream2 |
pump(InputStream is,
boolean closeIn)
|
static <T extends OutputStream> |
pump(InputStream is,
T os,
boolean closeIn,
boolean closeOut)
Reads data from
is and writes it into os.is and os are closed if closeIn and closeOut
are true respectively. |
static CharArrayWriter2 |
pump(Reader reader,
boolean closeReader)
|
static <T extends Writer> |
pump(Reader reader,
T writer,
boolean closeReader,
boolean closeWriter)
Reads data from
reader and writes it into writer.reader and writer are closed if closeReader and closeWriter
are true respectively. |
static int |
readFully(InputStream in,
byte[] b)
Reads data from given inputstream into specified buffer.
If the given inputstream doesn't have number of bytes equal to the length of the buffer available, it simply reads only the available number of bytes. |
static int |
readFully(InputStream in,
byte[] b,
int off,
int len)
Reads
len bytes from given input stream into specified buffer.If the given inputstream doesn't have len bytes available,
it simply reads only the availabel number of bytes. |
static int |
readFully(Reader reader,
char[] ch)
Reads data from given reader into specified buffer.
If the given reader doesn't have number of chars equal to the length of the buffer available, it simply reads only the available number of chars. |
static int |
readFully(Reader reader,
char[] ch,
int off,
int len)
Reads
len chars from given reader into specified buffer.If the given reader doesn't have len chars available,
it simply reads only the availabel number of chars. |
public static final Charset US_ASCII
public static final Charset ISO_8859_1
public static final Charset UTF_8
public static final Charset UTF_16BE
public static final Charset UTF_16LE
public static final Charset UTF_16
public static final Charset UTF_32BE
public static final Charset UTF_32LE
public static final Charset UTF_32
public static ByteArrayOutputStream2 pump(InputStream is, boolean closeIn) throws IOException
is - inputstream from which data is readcloseIn - close inputstream or notByteArrayOutputStream2 into which data is writtenIOException - if an I/O error occurs.public static <T extends OutputStream> T pump(InputStream is, T os, boolean closeIn, boolean closeOut) throws IOException
is and writes it into os.is and os are closed if closeIn and closeOut
are true respectively.is - inputstream from which data is reados - outputstream into which data is writtencloseIn - close inputstream or notcloseOut - close outputstream or notosIOException - if an I/O error occurs.public static CharArrayWriter2 pump(Reader reader, boolean closeReader) throws IOException
reader - reader from which data is readcloseReader - close reader or notCharArrayWriter2 into which data is writtenIOException - if an I/O error occurs.public static <T extends Writer> T pump(Reader reader, T writer, boolean closeReader, boolean closeWriter) throws IOException
reader and writes it into writer.reader and writer are closed if closeReader and closeWriter
are true respectively.reader - reader from which data is readwriter - writer into which data is writtencloseReader - close reader or notcloseWriter - close writer or notwriterIOException - if an I/O error occurs.public static int readFully(InputStream in, byte[] b) throws IOException
in - input stream from which data is readb - the buffer into which the data is read.IOException - if an I/O error occurs.public static int readFully(InputStream in, byte[] b, int off, int len) throws IOException
len bytes from given input stream into specified buffer.len bytes available,
it simply reads only the availabel number of bytes.in - input stream from which data is readb - the buffer into which the data is read.off - an int specifying the offset into the data.len - an int specifying the number of bytes to read.len bytes available
it returns the the number of bytes readIOException - if an I/O error occurs.public static int readFully(Reader reader, char[] ch) throws IOException
reader - reader from which data is readch - the buffer into which the data is read.IOException - if an I/O error occurs.public static int readFully(Reader reader, char[] ch, int off, int len) throws IOException
len chars from given reader into specified buffer.len chars available,
it simply reads only the availabel number of chars.reader - input stream from which data is readch - the buffer into which the data is read.off - an int specifying the offset into the data.len - an int specifying the number of chars to read.len chars available
it returns the the number of chars readIOException - if an I/O error occurs.Copyright © 2021. All rights reserved.