1、IO流的工具类
(1)需要先导入jar包:
(2)FilenameUtils:
import org.apache.commons.io.FilenameUtils; public class FilenameUtilesDemo { public static void main(String[] args) { String name = FilenameUtils.getExtension("001.txt"); System.out.println(name);// 文件的扩展名 String Filename = FilenameUtils.getName("001.txt"); System.out.println(Filename);//文件名 boolean b=FilenameUtils.isExtension("001.txt", "java"); System.out.println(b); } }
(3)FileUtils:
文件夹的复制:
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public class FileUtilsDemo { public static void main(String[] args) throws IOException { FileUtils.copyDirectory(new File("M:\网页设计"), new File("M:\网页")); } }
读取文件内容:
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public class FileUtilsDemo { public static void main(String[] args) throws IOException { String s=FileUtils.readFileToString( new File("E:\IO流\java\121.txt")); System.out.println(s); } }
向文件写内容:
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public class FileUtilsDemo { public static void main(String[] args) throws IOException { FileUtils.writeStringToFile(new File("E:\IO流\java\121.txt"), "我爱java"); } }
2、Properties类
Properties类:继承了HashTable实现了Map接口,用于对配置文件的操作。
(1)setProperty与getProperty的使用:
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) {
Properties p = new Properties();
p.setProperty("河南", "郑州");
p.setProperty("江苏", "南京");
p.setProperty("河北", "石家庄");
p.setProperty("辽宁", "沈阳");
p.setProperty("安徽", "合肥");
String value1 = p.getProperty("河南");
String value2 = p.getProperty("江苏");
String value3 = p.getProperty("河北");
String value4 = p.getProperty("辽宁");
String value5 = p.getProperty("安徽");
System.out.println(value1);
System.out.println(value2);
System.out.println(value3);
System.out.println(value4);
System.out.println(value5);
}
}
(2)读取文件数据与向文本写入数据(字节和文本)
package pers.zzz.Read;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ReadProperties {
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
FileInputStream i = new FileInputStream("E:\IO流\java\catalina.properties");
prop.load(i);
System.out.println(prop);
i.close();
}
}
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
Properties p = new Properties();
p.setProperty("河南", "郑州");
p.setProperty("江苏", "南京");
p.setProperty("河北", "石家庄");
p.setProperty("辽宁", "沈阳");
p.setProperty("安徽", "合肥");
FileWriter fw=new FileWriter("E:\IO流\java\catalina.properties");
p.store(fw,"");
fw.close();
}
}