分享 commons io 工具类 代码

本文提供多个Java IO操作的示例代码,包括输入流复制到输出流、文本写入指定文件、将输入流转换成文本、文件复制及网络流保存为本地文件等。还介绍了文件目录的操作方法,如清空目录、删除目录及其下的文件,以及如何获取目录的大小。
摘要由CSDN通过智能技术生成
来自:http://www.iteye.com/topic/575996

输入流复制到输出流


public class IoTest {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Writer write = new FileWriter("c:\\kk.dat");

InputStream ins = new FileInputStream(new File("c:\\text.txt"));

IOUtils.copy(ins, write);
write.close();

ins.close();
}

}



文本写入指定文件


public class FileWirterTest {

/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub

String name = "my name is panxiuyan";

File file = new File("c:\\name.txt");

FileUtils.writeStringToFile(file, name);

}

}


将输入流转换成文本

public class URLIoTest {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
URL url = new URL("http://www.dimurmill.com");

InputStream ins = url.openStream();

String contents = IOUtils.toString(ins,"UTF-8");
System.out.println( "Slashdot: " + contents );


}

}





关闭相关流


public class IoCloseTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

File file = null;

InputStream ins = null;
try{
file = new File("C:\\Test.java");

ins = new FileInputStream(file);
}catch(Exception e){
e.printStackTrace();
}finally{
IOUtils.closeQuietly(ins);
}

}
}



文件复制


public class FileCopyTest {

/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub

File srcfile = new File("c:\\Test.java");

File destfile = new File("c:\\Test.java.bak");


FileUtils.copyFile(srcfile, destfile);

}

}



网络流保存为文件

public class URLToFileTest {

/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub

URL url = new URL("http://www.163.com");

File file = new File("c:\\163.html");

FileUtils.copyURLToFile(url, file);

}

}



文件目录操作


public class DirOper {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

File dir = new File("c:\\test");

FileUtils.cleanDirectory(dir);//清空目录下的文件

FileUtils.deleteDirectory(dir);//删除目录和目录下的文件

}

}


目录大小

long size = FileUtils.sizeOfDirectory(dir);



目录操作

File testFile = new File( "testFile.txt" );

//如果不存在,新建

// 如果存在,修改文件修改时间

FileUtils.touch( testFile );





记录流的读取写入字节数




File test = new File( "test.dat" );

//输出流的统计
CountingOutputStream countStream = null;

//输入流的统计
//CountingInputStream countStream = null;




try {

FileOutputStream fos = new FileOutputStream( test );

countStream = new CountingOutputStream( fos );

countStream.write( "Hello".getBytes( ) );

} catch( IOException ioe ) {

System.out.println( "Error writing bytes to file." );

} finally {

IOUtils.closeQuietly( countStream );

}



if( countStream != null ) {

int bytesWritten = countStream.getCount( );

System.out.println( "Wrote " + bytesWritten + " bytes to test.dat" );

}



相同的内容写入不同的文本
File test1 = new File("split1.txt");

File test2 = new File("split2.txt");

OutputStream outStream = null;



try {

FileOutputStream fos1 = new FileOutputStream( test1 );

FileOutputStream fos2 = new FileOutputStream( test2 );

//包含不同的文本
outStream = new TeeOutputStream( fos1, fos2 );



outStream.write( "One Two Three, Test".getBytes( ) );

outStream.flush( );

} catch( IOException ioe ) {

System.out.println( "Error writing to split output stream" );

} finally {

IOUtils.closeQuietly( outStream );

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值