Commons-IO学习之IOUtils

本文详细介绍了IOUtils工具类的功能及使用方法,包括文件内容比较、流操作、字符集转换等核心方法。提供了丰富的示例代码帮助理解如何利用该工具类简化输入输出操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class IOUtils extends Object
General IO stream manipulation utilities. 

public static boolean contentEquals(InputStream input1,InputStream input2) throws IOException  用于比较两个文件内的内容是否一致

public static boolean contentEquals(Reader input1,Reader input2)  throws IOException  用于比较两个文件内的内容是否一致

public static int copy(InputStream input,OutputStream output)throws IOException

public static void copy(InputStream input,Writer output)throws IOException
IOUtils.copy(new FileInputStream(new File("e:/1.txt")),new FileOutputStream(new File("f:/1.txt")));

public static long copyLarge(InputStream input,OutputStream output)
throws IOException

public static long copyLarge(Reader input,Writer output) throws IOException

public static void copy(Reader input,OutputStream output) throws IOException

public static void copy(Reader input,OutputStream output,String encoding)throws IOException

public static LineIterator lineIterator(InputStream input,Charset encoding)throws IOException
FileInputStream stream=new FileInputStream(new File("f:/1.txt"));
try {
	LineIterator it = IOUtils.lineIterator(stream, "utf-8");
	while (it.hasNext()) {
		String line = it.nextLine();
		System.out.println(line);
	}
} finally {
	IOUtils.closeQuietly(stream);
}

public static LineIterator lineIterator(Reader reader)
Return an Iterator for the lines in a Reader.

public static List<String> readLines(InputStream input)throws IOException
Get the contents of an InputStream as a list of Strings, one entry per line, using the default character encoding of the platform.

public static InputStream toBufferedInputStream(InputStream input)
throws IOException
Fetches entire contents of an InputStream and represent same data as result InputStream. 

public static BufferedReader toBufferedReader(Reader reader)
Returns the given reader if it is a BufferedReader, otherwise creates a toBufferedReader for the given reader. 

public static byte[] toByteArray(InputStream input)throws IOException
Get the contents of an InputStream as a byte[]. 

public static byte[] toByteArray(InputStream input,int size)throws IOException

public static byte[] toByteArray(Reader input)throws IOException

public static byte[] toByteArray(Reader input,Charset encoding)
throws IOException

public static byte[] toByteArray(URI uri)throws IOException

public static byte[] toByteArray(URL url)throws IOException
Get the contents of a URL as a byte[]. 

public static char[] toCharArray(InputStream is)throws IOException

public static char[] toCharArray(InputStream is,String encoding)
throws IOException

public static char[] toCharArray(InputStream is,Charset encoding)
throws IOException
public static Charset forName(String charsetName)
返回指定 charset 的 charset 对象。

public static InputStream toInputStream(CharSequence input)
Convert the specified CharSequence to an input stream, encoded as bytes using the default character encoding of the platform. 
java.lang 
Interface CharSequence
All Known Subinterfaces: 
Name 
All Known Implementing Classes: 
CharBuffer, Segment, String, StringBuffer, StringBuilder 


public static InputStream toInputStream(CharSequence input,Charset encoding)
Convert the specified CharSequence to an input stream, encoded as bytes using the specified character encoding. 

public static InputStream toInputStream(CharSequence input,String encoding)throws IOException

public static InputStream toInputStream(String input)

public static String toString(Reader input)throws IOException

public static String toString(URI uri)throws IOException

public static String toString(URI uri,Charset encoding)throws IOException

public static String toString(URI uri,String encoding)throws IOException
Gets the contents at the given URI. 

public static String toString(URL url)throws IOException
Gets the contents at the given URL. 

public static void write(byte[] data,OutputStream output)throws IOException
Writes bytes from a byte[] to an OutputStream. 

public static void write(byte[] data,Writer output)throws IOException
	Writes bytes from a byte[] to chars on a Writer using the default character encoding of the platform. 

public static void write(byte[] data,Writer output,Charset encoding)
throws IOException

public static void write(char[] data,OutputStream output)throws IOException
Writes chars from a char[] to bytes on an OutputStream. 


 

参考commons-io api

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wiksys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值