您的位置:首页 > 职场人生

黑马程序员_IO流(二)_File类、其他流

2014-05-23 20:36 316 查看
---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流!
----------------------

流操作的基本规律:

1、明确源和目的;

2、操作的数据是否为纯为本;

3、当体系明确后,在明确要使用哪个具体的对象。

File类:

用来将文件或者文件夹封装成对象。

方便对文件与文件夹的属性信息进行操作。

File对象可以作为参数传递给构造函数。

目录分隔符:

File f = new File("c:"+File.separator+"abc");可跨平台;

File常见方法:

1、创建:

boolean createNewFile():在指定位置创建文件,如果该文件已经存在,则不创建,返回false。

boolean mkdir();创建文件夹,只能创建一级目录;

boolean mkdirs();可以创建多级文件夹;

和输出流不一样,输出流对象一建立创建文件。而且文件已经存在,会覆盖。

2、删除:

boolean delete();删除失败返回false;

void deleteOnExit();在程序退出时删除。

3、判断:

boolean exists();判断文件是否存在;

在判断文件对象是否是文件或者目的时,必须要先判断该文件对象封装的内容是否存在。

4、获取信息:

getName();

getPath();

getParent();

getAbsolutePath();

getAbsoluteFile();返回的是一个File类型,把绝对路径指向的文件直接封装为对象。

length();

lastModified();

renameTo(File f);重命名,还可以剪切文件。

调用list()方法的file对象必须是封装了一个目录,该目录还必须存在。

list(FilenameFilter filter)方法示例代码:

Java代码


public static void main(String[] args) throws Exception {

File dir = new File("d://java223//day2");

String[] arr = dir.list(new FilenameFilter(){

@Override
public boolean accept(File dir, String name) {
if(name.endsWith(".bmp"))
return true;
else
return false;
}

});
}

递归显示文件列表:

1、递归要注意限定条件;

2、要注意递归的次数,避免内存溢出。

Java代码


public static void showDir(File dir) {

System.out.println(dir);
File[] files = dir.listFiles();
for(int x=0;x<files.length;x++) {

if(files[x].isDirectory()) {
showDir(files[x]);
} else {
System.out.println(files[x]);
}
}
}

Properties类:

Properties是hashtable的子类,也就是说它具备map集合的特点。而且它里面存储的键值对都是字符串。

是集合中和IO技术相结合的集合容易。

该对象的特点:可以用于键值对形式的配置文件。

Java代码


public static void method_1() throws IOException {

BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));

String line = null;

Properties prop = new Properties();

while((line=bufr.readLine()) != null) {
String[] arr = line.split("=");
prop.setProperty(arr[0], arr[1]);
}

bufr.close();
}

public static void setAndGet() {

Properties prop = new Properties();

prop.setProperty("zhangsan", "30");
prop.setProperty("lisi", "39");

String value = prop.getProperty("lisi");

System.out.println(value);

Set<String> names = prop.stringPropertyNames();

for(String s : names) {
System.out.println(s + ":" + prop.getProperty(s));
}
}

打印流(PrintWriter,PrintStream):

打印流提供了打印方法,可以将各种数据类型的数据都原样打印。

构造方法:

1、file对象,File;

2、字符串路径,String;

3、字节输出流,OutputStream;

4、字符输出流,Writer;

自动刷新起作用的方法有,printf()、println()、format();

序列流(SequenceInputStream):

Java代码


<strong>public static void main(String[] args) throws Exception {

Vector<FileInputStream> v = new Vector<FileInputStream>();

v.add(new FileInputStream("1.txt"));
v.add(new FileInputStream("2.txt"));
v.add(new FileInputStream("3.txt"));

Enumeration<FileInputStream> en = v.elements();//获取Enumeration
SequenceInputStream sis = new SequenceInputStream(en);

FileOutputStream fos = new FileOutputStream("c\\4.txt");
byte[] buf = new byte[1024];

int len = 0;
while ((len=sis.read(buf))!=-1) {
fos.write(buf, 0, len);
}

fos.close();
sis.close();
}
</strong>

切割文件:

Java代码


public static void splitFile() throws IOException {

FileInputStream fis = new FileInputStream("c:\\1.bmp");

FileOutputStream fos = null;

byte[] buf = new byte[1024*1024];

int len = 0;

int count = 1;

while ((len=fis.read(buf))!=-1) {
fos = new FileOutputStream("c:\\"+ (count++) +".part");
fos.write(buf,0,len);
fos.close();
}

fis.close();
}

静态成员不能被序列化,因为静态在方法区,序列化只能对堆内的对象进行序列化。

Transient关键字也可以使成员不序列化。

管道流(PipedInputStream、PipedOutputStream):

输入输出可以直接进行连接,通过结合线程。

Java代码


public static void main(String[] args) throws Exception {

PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();

in.connect(out);
Read r = new Read(in);
Write w = new Write(out);

new Thread(r).start();
new Thread(w).start();

}

}

class Read implements Runnable {

private PipedInputStream in;

Read(PipedInputStream in) {
this.in = in;
}
@Override
public void run() {

int len;
try {
byte[] buf = new byte[1024];

len = in.read(buf);

String s = new String(buf,0,len);

System.out.println(s);

in.close();

} catch (IOException e) {
e.printStackTrace();
}

}

}

class Write implements Runnable {

private PipedOutputStream out;

Write(PipedOutputStream out) {
this.out = out;
}
@Override
public void run() {

try {
out.write("guandao lai le".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}

}

RandomAccessFile:

RandomAccessFile不是IO体系中的子类,直接继承Object,但是它是IO包中的成员。因为它具备读和写功

能。内部封装了一个数组,而且通过指针对数组的元素进行操作。可以通过getFilePointer()获取指针位

置,同时可以通过seek改变指针的位置。该类只能操作文件。

如果模式为rw读写,如果要操作的文件不存在,会自动创建,如果存在则不会覆盖。如果模式为只读,不会

创建文件。会读取一个已存在的文件,如果该文件不存在,则会出现异常。

操作基本数据类型的流:

DataStream:

操作字节数组:

ByteArrayStream

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流!
----------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: