黑马程序员__IO总结3

---------------------- android培训java培训、期待与您交流! ----------------------

File

1、 用来将文件或文件夹封装成对象

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

3、 File对象可以作为参数传递给流的构造函数

File类常见方法

1,创建。

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

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

boolean mkdir():创建文件夹。

boolean mkdirs():创建多级文件夹。

2,删除。

boolean delete():删除失败返回false。如果文件正在被使用,则删除不了返回falsel

void deleteOnExit();在程序退出时删除指定文件。

3,判断。

boolean exists() :文件是否存在.

isFile():是否是文件

isDirectory();是否是目录

isHidden();是否隐藏

isAbsolute();是否绝对路径

4,获取信息。

getName():获取名称

getPath():获取路径

getParent()://该方法返回的是绝对路径中的父目录。如果获取的是相对路径,返回null

如果相对路径中有上一层目录那么该目录就是返回结果。

getAbsolutePath() 获取绝对路径

long lastModified() 最后修改时间

long length():获取文件长度

list()和listFiles()的区别;listFiles返回的是对象

递归:

因为目录中还有目录,只要使用同一个列出目录功能的函数完成即可。

在列出过程中出现的还是目录的话,还可以再次调用本功能。

也就是函数自身调用自身。

这种表现形式,或者编程手法,称为递归。

递归要注意:

1,限定条件。

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

/*

Propertieshashtable的子类。

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

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

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

那么在加载数据时,需要数据有固定格式:键=值。

练习:限制程序运行次数。当运行次数到达5次时,给出,请您注册的提示。并不再让该程序执行。

*/

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");

FileOutputStream fos = new FileOutputStream("info.txt");

prop.store(fos,"haha");

// System.out.println(prop);

prop.list(System.out);

fos.close();

fis.close();

}

//演示,如何将流中的数据存储到集合中。

//想要将info.txt中键值数据存到集合中进行操作。

/*

1,用一个流和info.txt文件关联。

2,读取一行数据,将该行数据用"="进行切割。

3,等号左边作为键,右边作为值。存入到Properties集合中即可。

*/

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("=");

///System.out.println(arr[0]+"...."+arr[1]);

prop.setProperty(arr[0],arr[1]);

}

bufr.close();

System.out.println(prop);

}

// 设置和获取元素。

public static void setAndGet()

{

Properties prop = new Properties();

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

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

// System.out.println(prop);

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

//System.out.println(value);

prop.setProperty("lisi",89+"");

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

for(String s : names)

{

System.out.println(s+":"+prop.getProperty(s));

}

}

}

打印流:

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

字节打印流:

PrintStream

构造函数可以接收的参数类型:

1file对象。File

2,字符串路径。String

3,字节输出流。OutputStream

字符打印流

PrintWriter

构造函数可以接收的参数类型:

1file对象。File

2,字符串路径。String

3,字节输出流。OutputStream

4,字符输出流,Writer

*/

import java.io.*;

class PrintStreamDemo

{

public static void main(String[] args) throws IOException

{

BufferedReader bufr =

new BufferedReader(new InputStreamReader(System.in));

PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true);

String line = null;

while((line=bufr.readLine())!=null)

{

if("over".equals(line))

break;

out.println(line.toUpperCase());

//out.flush();

}

out.close();

bufr.close();

}

}

合并流!

3个文件合并成一个文件

import java.io.*;

import java.util.*;

class SequenceDemo

{

public static void main(String[] args) throws IOException

{

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

v.add(new FileInputStream("c:\\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);

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();

}

}

切割文件和合并文件:

import java.io.*;

import java.util.*;

class SplitFile

{

public static void main(String[] args) throws IOException

{

//splitFile();

merge();

}

public static void merge()throws IOException

{

ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();

for(int x=1; x<=3; x++)

{

al.add(new FileInputStream("c:\\splitfiles\\"+x+".part"));

}

final Iterator<FileInputStream> it = al.iterator();

Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()

{

public boolean hasMoreElements()

{

return it.hasNext();

}

public FileInputStream nextElement()

{

return it.next();

}

};

SequenceInputStream sis = new SequenceInputStream(en);

FileOutputStream fos = new FileOutputStream("c:\\splitfiles\\0.bmp");

byte[] buf = new byte[1024];

int len = 0;

while((len=sis.read(buf))!=-1)

{

fos.write(buf,0,len);

}

fos.close();

sis.close();

}

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:\\splitfiles\\"+(count++)+".part");

fos.write(buf,0,len);

fos.close();

}

fis.close();

}

}

---------------------- android培训java培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net/heima

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值