摘自:
http://blog.csdn.net/justdb/article/details/8653166
http://www.cnblogs.com/jifeng/archive/2011/08/16/2140003.html
StringUtils 字符串处理工具类
StringUtils.isEmpty(null) && StringUtils.isEmpty(""); // true
StringUtils.isBlank(" \n\t"); // true
StringUtils.substringAfterLast("foo.bar.baz", "."); // "baz"
StringUtils.substringBeforeLast("foo.bar.baz", "."); // "foo.bar"
StringUtils.split("foo.bar.baz", '.'); // { "foo", "bar", "baz" }
StringUtils.split("foo, bar,baz", ", "); // { "foo", "bar", "baz" }
StringUtils.leftPad("1", 3, '0'); // "001"
IOUtils and FileUtils
在一种当你需要手动操作多个文件罕见情况下必须具备的工具,这两个工具很相似(FileUtils操作文件,IOUtils操作InputStream和Reader classes),和捆绑常用IO.
File file1;
File file2;
InputStream inputStream;
OutputStream outputStream;
// copy one file into another
FileUtils.copyFile(file1, file2);
IOUtils.copy(inputStream, outputStream);
// read a file into a String
String s1 = FileUtils.readFileToString(file1);
String s2 = IOUtils.toString(inputStream);
// read a file into a list of Strings, one item per line
List<String> l1 = FileUtils.readLines(file1);
List<String> l2 = IOUtils.readLines(inputStream);
// put this in your finally() clause after manipulating streams
IOUtils.closeQuietly(inputStream);
// return the list of xml and text files in the specified folder and any subfolders
Collection<File> c1 = FileUtils.listFiles(file1, { "xml", "txt" }, true);
// copy one folder and its contents into another
FileUtils.copyDirectoryToDirectory(file1, file2);
// delete one folder and its contents
FileUtils.deleteDirectory(file1);
1.文件内容拷贝:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
publicclass FileCopyExample {
publicstaticvoid main(String[] args) {
try {
File src = new File( "test.dat" );
File dest = new File( "test.dat.bak" );
FileUtils.copyFile( src, dest );
} catch( IOException ioe ) {
System.out.println( "Problem copying file." );
}
try {
File src = new File( "test.dat" );
File dir = new File( "./temp" );
FileUtils.copyFileToDirectory( src, dir );
} catch( IOException ioe ) {
System.out.println( "Problem copying file to dir.");
}
try {
String string = "Blah blah blah";
File dest = new File( "test.tmp" );
FileUtils.writeStringToFile( dest, string, "ISO-8859-1" );
} catch( IOException ioe ) {
System.out.println( "Error writing out a String." );
}
try {
Writer writer = new FileWriter( "test.dat" );
InputStream inputStream =FileCopyExample.class.
getClass().getResourceAsStream("/io/web.xml");
IOUtils.copy( inputStream, writer );
writer.close();
inputStream.close();
} catch (IOException e) {
System.out.println( "Error copying data" );
}
try {
File src = new File( "test.txt" );
OutputStream output = new FileOutputStream(src);
InputStream inputStream =FileCopyExample.class.
getClass().getResourceAsStream("/io/web.xml");
IOUtils.copy( inputStream, output );
// writer.close();
inputStream.close();
output.close();
} catch (IOException e) {
System.out.println( "Error copying data" );
}
}
}
2.文件删除:
File file = new File( ("io/project.properties") );
String display = FileUtils.byteCountToDisplaySize( file.length() );
System.out.println( "project.xml is " + display );
FileUtils.forceDelete(file)
3.读取取文本中的每一行:
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileSystemUtils;
import org.apache.commons.io.FileUtils;
public class Test {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File((Test.class.getClass().getResource("/io/web.xml")).getFile());
List lines = FileUtils.readLines(file, "UTF-8");
for(int i=0;i<lines.size();i++)
System.out.println(lines.get(i));
}
}
http://blog.csdn.net/justdb/article/details/8653166
http://www.cnblogs.com/jifeng/archive/2011/08/16/2140003.html
StringUtils 字符串处理工具类
StringUtils.isEmpty(null) && StringUtils.isEmpty(""); // true
StringUtils.isBlank(" \n\t"); // true
StringUtils.substringAfterLast("foo.bar.baz", "."); // "baz"
StringUtils.substringBeforeLast("foo.bar.baz", "."); // "foo.bar"
StringUtils.split("foo.bar.baz", '.'); // { "foo", "bar", "baz" }
StringUtils.split("foo, bar,baz", ", "); // { "foo", "bar", "baz" }
StringUtils.leftPad("1", 3, '0'); // "001"
IOUtils and FileUtils
在一种当你需要手动操作多个文件罕见情况下必须具备的工具,这两个工具很相似(FileUtils操作文件,IOUtils操作InputStream和Reader classes),和捆绑常用IO.
File file1;
File file2;
InputStream inputStream;
OutputStream outputStream;
// copy one file into another
FileUtils.copyFile(file1, file2);
IOUtils.copy(inputStream, outputStream);
// read a file into a String
String s1 = FileUtils.readFileToString(file1);
String s2 = IOUtils.toString(inputStream);
// read a file into a list of Strings, one item per line
List<String> l1 = FileUtils.readLines(file1);
List<String> l2 = IOUtils.readLines(inputStream);
// put this in your finally() clause after manipulating streams
IOUtils.closeQuietly(inputStream);
// return the list of xml and text files in the specified folder and any subfolders
Collection<File> c1 = FileUtils.listFiles(file1, { "xml", "txt" }, true);
// copy one folder and its contents into another
FileUtils.copyDirectoryToDirectory(file1, file2);
// delete one folder and its contents
FileUtils.deleteDirectory(file1);
1.文件内容拷贝:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
publicclass FileCopyExample {
publicstaticvoid main(String[] args) {
try {
File src = new File( "test.dat" );
File dest = new File( "test.dat.bak" );
FileUtils.copyFile( src, dest );
} catch( IOException ioe ) {
System.out.println( "Problem copying file." );
}
try {
File src = new File( "test.dat" );
File dir = new File( "./temp" );
FileUtils.copyFileToDirectory( src, dir );
} catch( IOException ioe ) {
System.out.println( "Problem copying file to dir.");
}
try {
String string = "Blah blah blah";
File dest = new File( "test.tmp" );
FileUtils.writeStringToFile( dest, string, "ISO-8859-1" );
} catch( IOException ioe ) {
System.out.println( "Error writing out a String." );
}
try {
Writer writer = new FileWriter( "test.dat" );
InputStream inputStream =FileCopyExample.class.
getClass().getResourceAsStream("/io/web.xml");
IOUtils.copy( inputStream, writer );
writer.close();
inputStream.close();
} catch (IOException e) {
System.out.println( "Error copying data" );
}
try {
File src = new File( "test.txt" );
OutputStream output = new FileOutputStream(src);
InputStream inputStream =FileCopyExample.class.
getClass().getResourceAsStream("/io/web.xml");
IOUtils.copy( inputStream, output );
// writer.close();
inputStream.close();
output.close();
} catch (IOException e) {
System.out.println( "Error copying data" );
}
}
}
2.文件删除:
File file = new File( ("io/project.properties") );
String display = FileUtils.byteCountToDisplaySize( file.length() );
System.out.println( "project.xml is " + display );
FileUtils.forceDelete(file)
3.读取取文本中的每一行:
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileSystemUtils;
import org.apache.commons.io.FileUtils;
public class Test {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File((Test.class.getClass().getResource("/io/web.xml")).getFile());
List lines = FileUtils.readLines(file, "UTF-8");
for(int i=0;i<lines.size();i++)
System.out.println(lines.get(i));
}
}