File类
·用来将文件或者文件夹封装成对象
·方便对文件与文件夹进行操作。
·File对象可以作为参数传递给流的构造函数。
·了解File类中的常用方法。
File类常见方法:
1,创建。
boolean createNewFile ():在指定位置创建文件,如果该文件已经存在,则不创建,返回Eals.
和输出流不一样,输出流对象一建立创建文件。而且文件已经存在,会覆盖。
2,删除。
boolean delete():删除失败返回false
void deleteOnExit ();在程序退出时删除指定文件。
3,判断。
boolean exists():文件是否存在.
isFi1s():
isDirectory ();
isBidden();
isAbsolute ();
4,获取信息。
getPath():
getParent():
getAbsolutePath ()
lastModified ()
length()
import java.io.*;
class FileDemo
{
public static void main(String[]args)
{
consMethod ();
}
//创建File对象
public static void consMethod ()
{
//将a. txt封装成file对象。可以将已有的和为出现的文件或者文件夹封装成对象。Filef1≡new File("a txt");
//
Filef2 = new File("c:\\aɔc", ") txt txt");
File d = new File("cː\\abc");
Filef3 = new File(d, "c. txt");
sop("f1:" + f1);
sop("f2ː" + f2);
sop("f3:" + f3);
Thile fā—new Hile("c:" + filc.separator + "abo" + File, separator + "zz" + File.separator + "a.txt");
}
public static void sop(Object obj)
{
system.out.peintln(obj)
}
}
Properties 是hashtable的子类。
也就是说它具备map集合的特点。而且它里面存储的键值对都是字符串。
是集合中和ro技术相结合的集合容器。
该对象的特点:可以用于键值对形式的配置文件。
那么在加载数据时,需要数据有固定格式:键=值。
import java.io.*;
import java.util.*;
class PropertiesDemo
{
public static void main(String[] args) throws IOException
{
//method _1();
loadDemo();
}
public static void loadDemo() throws IOException
{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("info txt");
//将流中的数据加载进集合。
prop.load(fis);
prop.setProperty ("wangwu", "39");
FileOutputStreamfos = new FileOutputStream ("info. txt");
prop.store(fos, "haha");
//System. out. printin(prop);
prop.list(System.out);
fos.close();
fis.close();
}
//演示,如何将流中的数据存储到集合中。
//想要将info. txt中键值数据存到集合中进行操作。
/*1,用一个流和info.txt文件关联。
2,读取一行数据,将该行数据用“ = ”进行切割。
3,等号左边作为键,右边作为值。存入到 properties 集合中即可。*/
public static void method 1() throws IOException
{
Buffered Readqr burr = new BufferedReader (new File Reader("info-txt"));
String line = null;
Properties prop = new Properties ();
while ((line = buff.readLine()) != null)
}
{
String[]arr = line.split("=");
///System. out. println(arr[0]+”...."+arr[1]):
prop.setProperty (arr[0], arr[1]);
buff.close();
system.out.println(prop);
}
}
合并流
import java, io, *;
import java, util.*;
class SequenceDemo
{
public static void main(string[] args) throws Iorxception
{
Vector< FileInputStream >v = neWVector< FileInputStream >();
v.add(new FileInputStream ("a:\\1. txt"));
v.add(new FileInputStream ("c:\\2, txt"));
v.add(new FileInputStream ("c:\\3, txt"));
Enumeration < FileInputStream >en = v.elements()
SequenceInputStream sis = new SequenceInputStream (en);
File Outputstream fos - new Fileoutput Stream("C:\\4. txt");
byte[]but = new byte[1024];
int 1en = 0;
while ({ len = sis,read(buf)) != -1)
{
fos.write(buff.0,len);
}
fos.close();
sis.close();
}
}