利用org.apache.commons.io.FileUtils快速读写文件
利用org.apache.commons.io.FileUtils快速读写文件http://php.11519.net/5jblog/?p=475
String fileName = "C://11.txt";
File file = new File(fileName);
String fileContent = "";
try {
fileContent = org.apache.commons.io.FileUtils.readFileToString(file, "GBK");
} catch (IOException e) {
e.printStackTrace();
}
fileContent +="Helloworld";
try {
org.apache.commons.io.FileUtils.writeStringToFile(file, fileContent, "GBK");
} catch (IOException e) {
e.printStackTrace();
}
其他参考
Commons IO方便读写文件的工具类:http://laoyu.info/archives/282.html
Commons IO是apache的一个开源的工具包,封装了IO操作的相关类,使用Commons IO可以很方便的读写文件,url源代码等.
普通地读取一个网页的源代码的代码可能如下
[*]
InputStream in = new URL( "http://laoyu.info" ).openStream();
[*]
try {
[*]
InputStreamReader inR = new InputStreamReader( in );
[*]
BufferedReader buf = new BufferedReader( inR );
[*]
String line;
[*]
while ( ( line = buf.readLine() ) != null ) {
[*]
System.out.println( line );
[*]
}
[*]
} finally {
[*]
in.close();
[*]
}
使用了Commons IO,则可以大大简化代码.如下:
[*]
InputStream in = new URL( "http://laoyu.info" ).openStream();
[*]
try {
[*]
System.out.println( IOUtils.toString( in ) );
[*]
} finally {
[*]
IOUtils.closeQuietly(in);
[*]
}
Commons IO里的常用类
FileUtils包含了文件操作的相关方法.
下面的代码用于读取磁盘上的某个文件:
[*]
File file = new File("c:/test.txt");
[*]
List lines = FileUtils.readLines(file, "UTF-8");
FileSystemUtils 可以获得指定磁盘路径的可用空间
[*]
long freeSpace = FileSystemUtils.freeSpace("d:/");
文件复制代码:
[*]
File src = new File("src.txt");
[*]
File dest = new File("dest.txt");
[*]
FileUtils.copyFile(src, dest);
补充:
方便地下载文件到本地
[*]
InputStream in = new
[*]
URL("http://www.baidu.com/img/baidu_logo.gif").openStream();
[*]
byte [] gif = IOUtils.toByteArray(in);
[*]
//IOUtils.write(gif,new FileOutputStream(new File("c:/test.gif")));
[*]
FileUtils.writeByteArrayToFile(new File("c:/test.gif"),gif);
[*]
IOUtils.closeQuietly(in);
分享 commons io 工具类 代码:http://www.javaeye.com/topic/575996
输入流复制到输出流
Java代码 /javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=public%20class%20IoTest%20%7B%0A%0A%09%2F**%0A%09%20*%20%40param%20args%0A%09%20*%2F%0A%09public%20static%20void%20main(String%5B%5D%20args)%20throws%20Exception%20%7B%0A%09%09%2F%2F%20TODO%20Auto-generated%20method%20stub%0A%09%09Writer%20write%20%3D%20new%20FileWriter(%22c%3A%5C%5Ckk.dat%22)%3B%0A%0A%09%09InputStream%20ins%20%3D%20new%20FileInputStream(new%20File(%22c%3A%5C%5Ctext.txt%22))%3B%0A%0A%09%09IOUtils.copy(ins%2C%20write)%3B%0A%09%09write.close()%3B%0A%0A%09%09ins.close()%3B%0A%09%7D%0A%0A%7D
[*]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();
[*] }
[*]
[*]}
文本写入指定文件
Java代码 /javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=public%20class%20FileWirterTest%20%7B%0A%0A%09%2F**%0A%09%20*%20%40param%20args%0A%09%20*%2F%0A%09public%20static%20void%20main(String%5B%5D%20args)%20throws%20Exception%7B%0A%09%09%2F%2F%20TODO%20Auto-generated%20method%20stub%0A%09%09%0A%09%09String%20name%20%3D%20%22my%20name%20is%20panxiuyan%22%3B%0A%09%09%0A%09%09File%20file%20%3D%20%20new%20File(%22c%3A%5C%5Cname.txt%22)%3B%0A%09%09%0A%09%09FileUtils.writeStringToFile(file%2C%20name)%3B%0A%0A%09%7D%0A%0A%7D
[*]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);
[*]
[*] }
[*]
[*]}
将输入流转换成文本
Java代码 /javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=public%20class%20URLIoTest%20%7B%0A%0A%09%2F**%0A%09%20*%20%40param%20args%0A%09%20*%2F%0A%09public%20static%20void%20main(String%5B%5D%20args)%20throws%20Exception%20%7B%0A%09%09%2F%2F%20TODO%20Auto-generated%20method%20stub%0A%09%09URL%20url%20%3D%20new%20URL(%22http%3A%2F%2Fwww.dimurmill.com%22)%3B%0A%09%09%0A%09%09InputStream%20ins%20%3D%20url.openStream()%3B%0A%09%09%0A%09%09String%20contents%20%3D%20IOUtils.toString(ins%2C%22UTF-8%22)%3B%0A%09%20%20%20%20System.out.println(%20%22Slashdot%3A%20%22%20%2B%20contents%20)%3B%0A%0A%0A%09%7D%0A%0A%7D
[*]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 );
[*]
[*]
[*] }
[*]
[*]}
关闭相关流
Java代码 /javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=public%20class%20IoCloseTest%20%7B%0A%0A%09%2F**%0A%09%20*%20%40param%20args%0A%09%20*%2F%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%09%09%2F%2F%20TODO%20Auto-generated%20method%20stub%0A%09%09%0A%09%09File%20file%20%3D%20null%3B%0A%09%09%0A%09%09InputStream%20ins%20%3D%20null%3B%0A%09%09try%7B%0A%09%09%09file%20%3D%20new%20File(%22C%3A%5C%5CTest.java%22)%3B%0A%09%09%09%0A%09%09%09ins%20%3D%20new%20FileInputStream(file)%3B%0A%09%09%7Dcatch(Exception%20e)%7B%0A%09%09%09e.printStackTrace()%3B%0A%09%09%7Dfinally%7B%0A%09%09%09IOUtils.closeQuietly(ins)%3B%0A%09%09%7D%0A%0A%09%7D%0A%0A%7D
[*]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);
[*] }
[*]
[*] }
[*]
[*]}
文件复制
Java代码 /javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=public%20class%20FileCopyTest%20%7B%0A%0A%09%2F**%0A%09%20*%20%40param%20args%0A%09%20*%2F%0A%09public%20static%20void%20main(String%5B%5D%20args)%20throws%20Exception%7B%0A%09%09%2F%2F%20TODO%20Auto-generated%20method%20stub%0A%09%09%0A%09%09File%20srcfile%20%3D%20new%20File(%22c%3A%5C%5CTest.java%22)%3B%0A%09%09%0A%09%09File%20destfile%20%3D%20new%20File(%22c%3A%5C%5CTest.java.bak%22)%3B%0A%09%09%0A%09%09%0A%09%09FileUtils.copyFile(srcfile%2C%20destfile)%3B%0A%0A%09%7D%0A%0A%7D
[*]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);
[*]
[*] }
[*]
[*]}
文件复制指定的目录
Java代码 /javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=public%20class%20FileCopyTest%20%7B%0A%0A%09%2F**%0A%09%20*%20%40param%20args%0A%09%20*%2F%0A%09public%20static%20void%20main(String%5B%5D%20args)%20throws%20Exception%7B%0A%09%09%2F%2F%20TODO%20Auto-generated%20method%20stub%0A%09%09%0A%09%09File%20srcfile%20%3D%20new%20File(%22c%3A%5C%5CTest.java%22)%3B%0A%09%09%0A%09%09File%20destDir%20%3D%20new%20File(%22D%3A%5C%5C%22)%3B%0A%09%09%0A%09%09%0A%09%09FileUtils.copyFileToDirectory(srcfile%2C%20destDir)%3B%0A%0A%09%7D%0A%0A%7D
[*]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 destDir = new File("D:\\");
[*]
[*]
[*] FileUtils.copyFileToDirectory(srcfile, destDir);
[*]
[*] }
[*]
[*]}
网络流保存为文件
Java代码 /javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=public%20class%20URLToFileTest%20%7B%0A%0A%09%2F**%0A%09%20*%20%40param%20args%0A%09%20*%2F%0A%09public%20static%20void%20main(String%5B%5D%20args)%20throws%20Exception%7B%0A%09%09%2F%2F%20TODO%20Auto-generated%20method%20stub%0A%09%09%0A%09%09URL%20url%20%3D%20new%20URL(%22http%3A%2F%2Fwww.163.com%22)%3B%0A%09%09%0A%09%09File%20file%20%3D%20new%20File(%22c%3A%5C%5C163.html%22)%3B%0A%09%09%0A%09%09FileUtils.copyURLToFile(url%2C%20file)%3B%0A%0A%09%7D%0A%0A%7D
[*]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);
[*]
[*] }
[*]
[*]}
文件目录操作
Java代码 /javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=public%20class%20DirOper%20%7B%0A%0A%09%2F**%0A%09%20*%20%40param%20args%0A%09%20*%2F%0A%09public%20static%20void%20main(String%5B%5D%20args)%20throws%20Exception%20%7B%0A%09%09%2F%2F%20TODO%20Auto-generated%20method%20stub%0A%0A%09%09File%20dir%20%3D%20new%20File(%22c%3A%5C%5Ctest%22)%3B%0A%0A%09%09FileUtils.cleanDirectory(dir)%3B%2F%2F%E6%B8%85%E7%A9%BA%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84%E6%96%87%E4%BB%B6%0A%0A%09%09FileUtils.deleteDirectory(dir)%3B%2F%2F%E5%88%A0%E9%99%A4%E7%9B%AE%E5%BD%95%E5%92%8C%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84%E6%96%87%E4%BB%B6%0A%0A%09%7D%0A%0A%7D
[*]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);//删除目录和目录下的文件
[*]
[*] }
[*]
[*]}
目录大小
Java代码 /javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=long%20size%20%3D%20FileUtils.sizeOfDirectory(dir)%3B
[*]long size = FileUtils.sizeOfDirectory(dir);
目录操作
Java代码 /javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=File%20testFile%20%3D%20new%20File(%20%22testFile.txt%22%20)%3B%0A%0A%20%20%20%20%20%2F%2F%E5%A6%82%E6%9E%9C%E4%B8%8D%E5%AD%98%E5%9C%A8%2C%E6%96%B0%E5%BB%BA%0A%0A%20%20%20%20%2F%2F%20%E5%A6%82%E6%9E%9C%E5%AD%98%E5%9C%A8%2C%E4%BF%AE%E6%94%B9%E6%96%87%E4%BB%B6%E4%BF%AE%E6%94%B9%E6%97%B6%E9%97%B4%0A%0A%20%20%20%20FileUtils.touch(%20testFile%20)%3B%0A%20
[*]File testFile = new File( "testFile.txt" );
[*]
[*] //如果不存在,新建
[*]
[*] // 如果存在,修改文件修改时间
[*]
[*] FileUtils.touch( testFile );
[*]
记录流的读取写入字节数
Java代码 /javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=File%20test%20%3D%20new%20File(%20%22test.dat%22%20)%3B%0A%0A%2F%2F%E8%BE%93%E5%87%BA%E6%B5%81%E7%9A%84%E7%BB%9F%E8%AE%A1%0ACountingOutputStream%20countStream%20%3D%20null%3B%0A%0A%2F%2F%E8%BE%93%E5%85%A5%E6%B5%81%E7%9A%84%E7%BB%9F%E8%AE%A1%0A%2F%2FCountingInputStream%20countStream%20%3D%20null%3B%0A%0A%0A%0A%0Atry%20%7B%0A%0A%20%20%20%20FileOutputStream%20fos%20%3D%20new%20FileOutputStream(%20test%20)%3B%0A%0A%20%20%20%20countStream%20%3D%20new%20CountingOutputStream(%20fos%20)%3B%0A%0A%20%20%20%20countStream.write(%20%22Hello%22.getBytes(%20)%20)%3B%0A%0A%7D%20catch(%20IOException%20ioe%20)%20%7B%0A%0A%20%20%20%20System.out.println(%20%22Error%20writing%20bytes%20to%20file.%22%20)%3B%0A%0A%7D%20finally%20%7B%0A%0A%20%20%20%20IOUtils.closeQuietly(%20countStream%20)%3B%0A%0A%7D%0A%0A%0A%0Aif(%20countStream%20!%3D%20null%20)%20%7B%0A%0A%20%20%20%20int%20bytesWritten%20%3D%20countStream.getCount(%20)%3B%0A%0A%20%20%20%20System.out.println(%20%22Wrote%20%22%20%2B%20bytesWritten%20%2B%20%22%20bytes%20to%20test.dat%22%20)%3B%0A%0A%7D
[*]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" );
[*]
[*]}
相同的内容写入不同的文本
Java代码 /javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=File%20test1%20%3D%20new%20File(%22split1.txt%22)%3B%0A%0AFile%20test2%20%3D%20new%20File(%22split2.txt%22)%3B%0A%0AOutputStream%20outStream%20%3D%20null%3B%0A%0A%20%20%20%20%20%20%20%20%0A%0Atry%20%7B%0A%0A%20%20%20%20FileOutputStream%20fos1%20%3D%20new%20FileOutputStream(%20test1%20)%3B%0A%0A%20%20%20%20FileOutputStream%20fos2%20%3D%20new%20FileOutputStream(%20test2%20)%3B%0A%0A%20%20%20%20%2F%2F%E5%8C%85%E5%90%AB%E4%B8%8D%E5%90%8C%E7%9A%84%E6%96%87%E6%9C%AC%0A%20%20%20%20outStream%20%3D%20new%20TeeOutputStream(%20fos1%2C%20fos2%20)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%0A%20%20%20%20outStream.write(%20%22One%20Two%20Three%2C%20Test%22.getBytes(%20)%20)%3B%0A%0A%20%20%20%20outStream.flush(%20)%3B%0A%0A%7D%20catch(%20IOException%20ioe%20)%20%7B%0A%0A%20%20%20%20System.out.println(%20%22Error%20writing%20to%20split%20output%20stream%22%20)%3B%0A%0A%7D%20finally%20%7B%0A%0A%20%20%20%20IOUtils.closeQuietly(%20outStream%20)%3B%0A%0A%7D
[*]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 );
[*]
[*]}
页:
[1]