黑马程序员-----java基础IO流<二>

来源:互联网 发布:光环助手同类软件 编辑:程序博客网 时间:2024/05/16 07:23

                               ------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


IO流中的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():


getAbsolutePath() 
long lastModified() 
long length()




递归算法
列出指定目录下文件或者文件夹,包含子目录中的内容。
也就是列出指定目录下所有内容。


因为目录中还有目录,只要使用同一个列出目录功能的函数完成即可。
在列出过程中出现的还是目录的话,还可以再次调用本功能。
也就是函数自身调用自身。
这种表现形式,或者编程手法,称为递归。


递归要注意:
1,限定条件。


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

/*练习将一个指定目录下的java文件的绝对路径,存储到一个文本文件中。建立一个java文件列表文件。思路:1,对指定的目录进行递归。2,获取递归过程所以的java文件的路径。3,将这些路径存储到集合中。4,将集合中的数据写入到一个文件中。*/import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.List;class Test {public static void main(String[] args) throws IOException {//要变量的文件夹File srcFile = new File("E:\\wordspace\\JavaLesson\\Code");List<File> fileList = new ArrayList<File>();//储存到到的文件名字String destinationName = "filelist.txt";fileToList(srcFile,fileList);saveFile(fileList,destinationName);}//得到有装有文件的集合public static void fileToList(File dir,List<File> listFile) {File[] files = dir.listFiles();listFile.add(dir);//遍历目录下的文件for (int i=0; i<files.length; i++) {//目录的情况下if (files[i].isDirectory()) {fileToList(files[i],listFile);} else {listFile.add(files[i].getAbsoluteFile());}}}//把集合中的文件保存到文件当中,以备查阅public static void saveFile(List<File> listFile,String filename) throws IOException {BufferedWriter bufw = null;try {bufw = new BufferedWriter(new FileWriter(filename));//取出集合中的File再写入缓冲区中File f = null;for (int i=0; i<listFile.size(); i++) {f = listFile.get(i);bufw.write(f.getAbsolutePath());bufw.newLine();bufw.flush();}} finally {try {if (bufw != null)bufw.close();} catch (IOException e) {throw new RuntimeException("关闭流出错了");}}}}

Properties 类的运用:

/*Properties是Hashtable的子类。也就是说它具备map集合的特点。而且它里面存储的键值对都是字符串。是集合中和IO技术相结合的集合容器。该对象的特点:可以用于键值对形式的配置文件。那么在加载数据时,需要数据有固定格式:键=值。*/练习:限制程序运行次数。当运行次数到达5次时,给出,请您注册的提示。并不再让该程序执行。class Test {public static void main(String[] args) throws IOException {calNum();}public static void calNum() throws IOException {Properties prop = new Properties();File file = new File("count.ini");if (!file.exists())file.createNewFile();FileInputStream fis = new FileInputStream(file);prop.load(fis);int count = 0;String value = prop.getProperty("time");if (value != null) {count = Integer.parseInt(value);if (count >= 5) {System.out.println("您好,使用次数已到,请注册该软件!");return;}System.out.println("软件正常使用");System.out.println("剩余使用次数"+(5-count));}count++;prop.setProperty("time", count + "");FileOutputStream fos = new FileOutputStream(file);prop.store(fos, "soft use times");fos.close();fis.close();}}



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


字节打印流:
PrintStream
构造函数可以接收的参数类型:
1,file对象。File
2,字符串路径。String
3,字节输出流。OutputStream






字符打印流:
PrintWriter
构造函数可以接收的参数类型:
1,file对象。File
2,字符串路径。String
3,字节输出流。OutputStream
4,字符输出流,Writer。


练习:
import java.io.BufferedReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;class Test {public static void main(String[] args) throws IOException {//从控制台上录入数据BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));//true表示带刷新功能,保存到当前目录下的a.txt文件中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.close();bufr.close();}}

/*SequenceInputStream的运用把一个流拆分成多个流*/import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.SequenceInputStream;import java.util.Vector;class Test {public static void main(String[] args) throws IOException {// showSeq();spilFile();}/** * SequenceInputStream 的用法:把多个流有序的连接起来,生成一个大流 */// 要求: 把1.txt,2.txt,3.txt.三个文件组合成一个新的文件4.txtpublic static void showSeq() throws IOException {Vector<FileInputStream> vec = new Vector<FileInputStream>();vec.add(new FileInputStream("1.txt"));vec.add(new FileInputStream("2.txt"));vec.add(new FileInputStream("3.txt"));// 构造该函数时添加多个输入流,并且是枚举类型的SequenceInputStream sis = new SequenceInputStream(vec.elements());// 定义输出流FileOutputStream fos = new FileOutputStream("4.txt");byte[] bs = new byte[1024];int len = 0;while ((len = sis.read(bs)) != -1) {fos.write(bs, 0, len);}fos.close();sis.close();}/** * 将4.txt中的文件进行拆分成3个文件,风别为1.txt,2.txt,3.txt */public static void spilFile() throws IOException {// 把4.txt中的文件读入到流中FileInputStream fis = new FileInputStream("4.txt");FileOutputStream fos = null;byte[] bs = new byte[50];int len = 0;int count = 1;while ((len = fis.read(bs)) != -1) {// 每读取50个字节就写入一个文件fos = new FileOutputStream((count++) + ".txt");fos.write(bs, 0, len);fos.flush();fos.close();}fis.close();}}


0 0
原创粉丝点击