javaioutils写⼊⽂件_⽂件输⼊输出流⼯具:IOUtils使⽤总结序⾔
以前写⽂件的复制很⿇烦,需要各种输⼊流,然后读取line,输出到输出流...其实apachemons.io⾥⾯提供了输⼊流输出流的常⽤⼯具⽅法,⾮常⽅便。下⾯就结合源码,看看IOUTils都有什么⽤处吧!
常⽤的静态变量
在IOUtils中还是有很多常⽤的⼀些变量的,⽐如换⾏符等等
public static final char DIR_SEPARATOR_UNIX = '/';
public static final char DIR_SEPARATOR_WINDOWS = '\\';
public static final char DIR_SEPARATOR;
public static final String LINE_SEPARATOR_UNIX = "\n";
public static final String LINE_SEPARATOR_WINDOWS = "\r\n";
public static final String LINE_SEPARATOR;
static {
DIR_SEPARATOR = File.separatorChar;
StringBuilderWriter buf = new StringBuilderWriter(4);
PrintWriter out = new PrintWriter(buf);
out.println();
LINE_SEPARATOR = String();
out.close();
}
常⽤⽅法
copy
这个⽅法可以拷贝流,算是这个⼯具类中使⽤最多的⽅法了。⽀持多种数据间的拷贝:
copy(inputstream,outputstream)
copy(inputstream,writer)
copy(inputstream,writer,encoding)
copy(reader,outputstream)
copy(reader,writer)
copy(reader,writer,encoding)
copy内部使⽤的其实还是copyLarge⽅法。因为copy能拷贝Integer.MAX_VALUE的字节数据,即2^31-1。
copyLarge
这个⽅法适合拷贝较⼤的数据流,⽐如2G以上。
copyLarge(reader,writer) 默认会⽤1024*4的buffer来读取
copyLarge(reader,writer,buffer)
内部的细节可以参考:
public static long copyLarge(Reader input, Writer output, char [] buffer) throws IOException { long count = 0;
int n = 0;
while (EOF != (n = ad(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
这个⽅法会⽤⼀个固定⼤⼩的Buffer,持续不断的读取数据,然后写⼊到输出流中。
read
从⼀个流中读取内容
read(inputstream,byte[])
read(inputstream,byte[],offset,length)
//offset是buffer的偏移值,length是读取的长度
read(reader,char[])
read(reader,char[],offset,length)
这⾥我写了个⼩例⼦,可以测试read⽅法的效果:
@Test
public void readTest(){
try{
byte[] bytes = new byte[4];
InputStream is = InputStream("hello world");
System.out.println(new String(bytes));
bytes = new byte[10];
is = InputStream("hello world");
System.out.println(new String(bytes));
} catch (IOException e) {
e.printStackTrace();
}
}
得到的结果是:
hell
□□hell□□□□
readFully
这个⽅法会读取指定长度的流,如果读取的长度不够,就会抛出异常readFully(inputstream,byte[])
readFully(inputstream,byte[],offset,length)
readFully(reader,charp[])
readFully(reader,char[],offset,length)
⽐如:
@Test
public void readFullyTest(){
byte[] bytes = new byte[4];
InputStream is = InputStream("hello world");
try {
System.out.println(new String(bytes));
} catch (IOException e) {
e.printStackTrace();
}
}
输出
hell
但是如果读取20个byte,就会出错了
java.io.EOFException: Length to read: 20 actual: 11
at org.apachemons.adFully(IOUtils.java:2539) at org.apachemons.adFully(IOUtils.java:2558) at test.adFullyTest(IOUtilsTest.java:22) ...
writelines()方法将什么写入文件readLines
readLines⽅法可以从流中读取内容,并转换为String的list readLines(inputstream)
readLines(inputstream,charset)
readLines(inputstream,encoding)
readLines(reader)
这个⽅法极⼤简化了之前原始的读取⽅法:
@Test
public void readLinesTest(){
try{
InputStream is = new FileInputStream("D://"); List lines = adLines(is);
for(String line : lines){
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
输出内容:
hello
world
nihao
ioutils
skip
这个⽅法⽤于跳过指定长度的流,
skip(inputstream,skip_length)
skip(ReadableByteChannel,skip_length)
skip(reader,skip_length)
例如:
@Test
public void skipTest(){
InputStream is = InputStream("hello world"); try {
IOUtils.skip(is,4);
System.out.String(is,"utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
skipFully
这个⽅法类似skip,只是如果忽略的长度⼤于现有的长度,就会抛出异常skipFully(inputstream,toSkip)
skipFully(readableByteChannel,toSkip)
skipFully(inputstream,toSkip)
例如
@Test
public void skipFullyTest(){
InputStream is = InputStream("hello world");
try {
IOUtils.skipFully(is,30);
System.out.String(is,"utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
write
这个⽅法可以把数据写⼊到输出流中
write(byte[] data, OutputStream output)
write(byte[] data, Writer output)
write(byte[] data, Writer output, Charset encoding)
write(byte[] data, Writer output, String encoding)
write(char[] data, OutputStream output)
write(char[] data, OutputStream output, Charset encoding)
write(char[] data, OutputStream output, String encoding)
write(char[] data, Writer output)
write(CharSequence data, OutputStream output)
write(CharSequence data, OutputStream output, Charset encoding) write(CharSequence data, OutputStream output, String encoding) write(CharSequence data, Writer output)
write(StringBuffer data, OutputStream output)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论