------- android培训、java培训、期待与您交流! ----------
until20
IO流
1.File概述
2.File对象功能-创建和删除
3.File对象功能-判断
4.File对象功能-获取
5.File对象功能-文件列表
6.File对象功能-文件列表
7.列出目录下的所有内容-递归
8.列出目录下的所有内容-层次
9.删除带内容的目录
10.创建java文件列表
11.properties简述
12.properties存取
13.properties存取配置文件
14.properties练习
15.pintWriter
16.合并流
17.切割文件
File概述
io包中的File类!
1.用于将文件或文件夹封装成对象
2.便于对文件或者文件夹的操作(属性信息等等)
3.File对象可作为参数传给流的构造函数
import java.io.*;
public class demo {
public static void main(String[] args) {
File f = new File("c:\\FetionBox");
System.out.println(f);
File f1 = new File("c:\\","FetionBox");
System.out.println(f1);
File f2 = newFile("c:"+File.separator+"FetionBox");//File.separator相当于\\
System.out.println(f2);
}
}
File对象功能-创建和删除
需求 | 方法 | 注释 |
增加 | boolean createNewFile() | 如果创建的、文件存在返回flase,否则返回true |
删除 | void delete/deleteOnExit | 直接删除/退出时删除 |
判断 | canExecute()是否可以运行; canWrite()是否可写; canRead(); exists()是否存在! isDirectory()是否为目录; isHidden(); isAbsolute() |
|
获取 | getName(); getPath(); getParent() getAbsoluteFile(); lastModified()返回文件最后一次修改时间long; length() long; | renameTo()重新命名! |
【
File对象功能:文件列表
File[] listRoots():返回根目录列表
String[] list():返回指定目录下文件名列表(包含隐藏文件)
File[] listFiles()
String[] list(FilenameFilter filter):
File[] listFiles(FileFilter filter)//根据过滤器筛选符合条件的文件
File[] listFiles(FilenameFilter filter)
】
(mkdir 创建目不过只能创建一级目录!mkdirs 可以创建多级!)
public class Demo1 {
public static void main(String[] args) throws IOException, InterruptedException {
//增加
File f = new File("w.txt");
File f2 = new File("cc.txt");
boolean b = f.createNewFile();
System.out.println(b);
//删除
//f.deleteOnExit();
//Thread.sleep(4000);
//boolean d = f.delete();
//System.out.println(d);
//判断
boolean c1 = f.canExecute();//是否可运行
System.out.println(c1);
//是否存在
boolean c2 = f.exists();
System.out.println(c2);
//是否为目录
boolean c3 = f.isDirectory();
System.out.println(c3);
//获取
//获取路径
String s1 = f.getPath();
System.out.println(s1);
String s2 = f.getParent();
System.out.println(s2);
//获取名字
String s3 = f.getName();
System.out.println(s3);
//获取最后修改时间
long l1 = f.lastModified();
System.out.println(l1);
//获取长度
long l2 = f.length();
System.out.println(l2);
//修改名字
f.renameTo(f2);
}
}
File对象功能-文件列表
public class Demo2 {
public static void main(String[] args) {
File[] f = File.listRoots();//列出了盘符目录
for(File ff : f)
{
System.out.println(ff);
}
File files = new File("c:\\");//列出了指定目录中的文件
String[] s = files.list();
for(String s1 : s)
{
System.out.println(s1);
}
File fi = new File("F:\\class\\java_CodePath\\毕老师的25天java基础课程");//列出目录中指定类型文件
String[] s1 = fi.list(new FilenameFilter()
{
public boolean accept(File fi,String name)
{
return name.endsWith(".doc");
}
});
for(String s2 : s1)
{
System.out.println(s2);
}
File fe = new File("c:\\");//把文件目录中的文件封装了对象
File[] ss = fe.listFiles();
for(File ff : ss)
{
System.out.println(ff.getName());
}
}
}
列出目录下的所有内容-递归、层次
package until_20;
import java.io.*;
public class File_1 {
public static void main(String[] args) throws IOException {
System.setOut(new PrintStream("aa.txt"));
File f = new File("F:\\class\\java_CodePath\\毕老师的25天java基础课程");
System.out.println(f.getName());
showDir(f,0);
//toBin(6);
}
public static void showDir(File f,int level)
{
System.out.println(level(level)+f.getName());
level++;
File[] files = f.listFiles();
for(int x = 0;x<files.length;x++)
{
if(files[x].isDirectory())//用到了递归的方法
{
showDir(files[x],level);
}
else
System.out.println(level(level)+files[x].getName());
}
}
public static String level(int le)
{
StringBuilder sb = new StringBuilder();
for(int x = 0;x<le;x++)
{
sb.append("|-");
}
return sb.toString();
}
//用二进制的方式来实验递归的方法
public static void toBin(int x)
{
if(x>0)
{
toBin(x>>1);
System.out.print(x%2);
}
}
}
删除带内容的目录
public class DeleteDir {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f = new File("F:\\class\\java_CodePath\\毕老师的25天java基础课程\\all25until\\kke");
dd(f);
}
public static void dd(File f)
{
File[] files = f.listFiles();
for(int x = 0;x<files.length;x++)
{
if(files[x].isDirectory())
dd(files[x]);
else
files[x].delete();
}
f.delete();
}
}
创建java文件列表
package until_20;
import java.io.*;
import java.util.*;
public class ListDirectory {
public static void main(String[] args) {
File f = new File("F:\\class\\java_CodePath\\毕老师的25天java基础课程\\all25until\\bin");
List<File> list = new ArrayList<File>();
listDirectory(f,list);
File ff = new File("oo.txt");
writePrint(list,ff.toString());
}
public static void listDirectory(File f,List<File> list)//把目录中的符合文件对象存入集合中
{
File[] files = f.listFiles();
for(int x = 0;x<files.length;x++)
{
if(files[x].isDirectory())
listDirectory(files[x],list);
else
{
if(files[x].getName().endsWith(".class"))
list.add(files[x]);
}
}
}
public static void writePrint(List<File> list,String s)//处理文件对象输出到一个文件中!
{
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(s));
for(File f : list)
{
String path = f.getAbsolutePath();
bw.write(path);
bw.newLine();
}
} catch (Exception e) {
throw new RuntimeException("失败");
}
finally
{
try {
if(bw!=null)
bw.close();
} catch (Exception e2) {
throw new RuntimeException("失败");
}
}
}
}
properties简述
是Hashtable的子类!具备map特点!~所以里面存在的是字符串
是集合和IO流的
1.6出现!使用StringPropertiesNames 返回是的Set<String>!
properties存取
public class PropertiesDemo {
public static void main(String[] args) {
Properties pp = new Properties();
pp.setProperty("1","a");
pp.setProperty("2","b");
pp.setProperty("3","c");
pp.setProperty("3", "aa");
Set<String> s = pp.stringPropertyNames();
for(String key : s)
{
System.out.println(pp.getProperty(key));
}
}
}
properties存取配置文件
public class PropertiesDemo_1 {
public static void main(String[] args) throws IOException {
//将流中的数据存储到properties中
Demo2();
}
public static void Demo1() throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("oo.txt"));
Properties prop = new Properties();
String line = null;
while((line=br.readLine())!=null)
{
String[] s = line.split("==");
prop.setProperty(s[0], s[1]);
}
System.out.println(prop);
}
public static void Demo2() throws IOException
{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("oo.txt");
//把流中的数据加载进入prop集合中!1.6之后出现的方法
prop.load(fis);
//System.out.println(prop);
prop.list(System.out);//list方法是把prop中的信息指定打印到某个地方!
prop.setProperty("wangwu","wangwu");
FileOutputStream fos = new FileOutputStream("oo.txt");
prop.store(fos, "hha");//"hha"是注释!而且使用该方法会新增一次修改时间在文件头!
fis.close();
fos.close();
}
}
出现了load的方法!~也是1.6出现的!store方法用于把文件信息写入流中!
properties练习
public class Practice {
public static void main(String[] args) throws IOException{
// easy
File f = new File("ci.ini");
if(!f.exists())
f.createNewFile();
FileInputStream fis = new FileInputStream(f);
Properties prop = new Properties();
prop.load(fis);
int count = 0;
String s = prop.getProperty("time");
System.out.println(s);
System.out.println(prop);
if(s!=null)
{
count = Integer.parseInt(s);
if(count>5)
{
System.out.println("使用次数到了~!");
fis.close();
return;
}
}
count++;
prop.setProperty("time", count+"");
//打印流的位置很重!一开始我把下面的一行代码放在读取流上面!导致value一直为null!
//因为是把文件扔进流中,如果扔进流的时候文件没有改变!就不会得到我们想要的结果
FileOutputStream fos = new FileOutputStream(f);
prop.store(fos,"计数");
fis.close();
fos.close();
}
}
pintWriter
该流提供了打印方法!原样打印各种数据!
打印流很常用!它的构造函数可以1.接收File2.字符串路径3.字符输出流4.字符输入流
println()会自动刷新(相当与flush了)
或者在构造函数后加true,也会自动flush
合并流
SequenceInputStream
切割文件
匿名内部类要记得final修饰一样!Enumeration重写之后就可以直接和List集合通用了!