• IO流05--毕向东JAVA基础教程视频学习笔记


    Day20

    10 创建java文件列表
    11 Properties简述
    12 Properties存取
    13 Properties存取配置文件
    14 Properties练习
    15 PrintWriter
    16 合并流
    17 切割文件

    10 创建java文件列表

    练习:
    将一个指定目录下的java文件的绝对路径,存储到一个文本文件中,
    建立一个java文件列表的文件。

    思路:
    1.对指定的目录进行递归
    2.获取递归过程所有的java文件的路径
    3.将这些路径存储到集合中
    4.将集合中的数据存储到一个文件中。

     1 import java.io.*;
     2 import java.util.*;
     3 public class  JavaFileList
     4 {
     5     public static void main(String[] args) throws IOException
     6     {
     7         File dir=new File("d:\abc");
     8         List<File> list=new ArrayList<File>();
     9         fileToList(dir,list);
    10         File file=new File(dir,"javafile.txt");
    11         writeToFile(list,file.toString());
    12         
    13         
    14     }
    15     public static void fileToList(File dir,List<File> list) 
    16     {
    17         File[] files=dir.listFiles();
    18 
    19         for(File file:files)
    20         {
    21             if(file.isDirectory())
    22                 fileToList(file,list);
    23             else
    24             {
    25                 if(file.getName().endsWith(".java"))
    26                     list.add(file);
    27             }
    28         }
    29     }
    30     public static void writeToFile(List<File> list,String javaListFile)throws IOException 
    31     {
    32         BufferedWriter bufw=null;
    33         try
    34         {
    35             bufw=new BufferedWriter(new FileWriter(javaListFile));
    36             for(File f:list)
    37             {
    38                 String path=f.getAbsolutePath();
    39                 bufw.write(path);
    40                 bufw.newLine();
    41                 bufw.flush();
    42             }
    43 
    44         }
    45         catch (IOException e)
    46         {
    47             throw e;
    48         }
    49         finally
    50         {
    51             try
    52             {
    53                 if(bufw!=null)
    54                     bufw.close();
    55             }
    56             catch (IOException ie)
    57             {
    58                 throw ie;
    59             }
    60         }
    61     }
    62 }
    View Code

    11 Properties简述

    Properties是hashtable的子类。
    也就是说它具备map集合的特点,而且它里面存储的键值对都是字符串,
    是集合和IO技术相结合的集合容器。
    该对象的特点,可以用于键值对形式的配置文件

    配置文件:就是在用户登录电脑时,或是用户在使用软件时,软件系统为用户所要加载所需环境的设置和文件的集合。

    它包括所有用户专用的配置设置,如程序项目、屏幕颜色、网络连接、打印机连接、鼠标设置及窗口的大小和位置等。


    12 Properties存取

    String getProperty(String key):用指定的键在此属性类表中搜索属性。

    Object setProperty(String key,String value):调用Hashtable的方法put.

    Set<String> stringPropertyNames():返回此属性类表中的键集。

     1 import java.util.*;
     2 public class  PropertiesDemo
     3 {
     4     public static void main(String[] args) 
     5     {
     6         setAndGet();
     7         
     8     }
     9     public static void setAndGet()
    10     {
    11         Properties prop=new Properties();
    12 
    13         prop.setProperty("Tina","22");
    14         prop.setProperty("Jack","21");
    15         prop.setProperty("Marry","20");
    16 
    17         String value=prop.getProperty("Jack");
    18         System.out.println(value);
    19 
    20         Set<String> names=prop.stringPropertyNames();
    21         for(String s:names)
    22         {
    23             System.out.println(s+":"+prop.getProperty(s));
    24         }
    25     }
    26 }
    View Code

    13 Properties存取配置文件

    如何将流中的数据存储到集合中
    想要将info.txt中键值数据存到集合中进行操作
    1.用一个流和info.txt文件相关联。
    2.读取一行数据,将该行数据用“=”进行分割,并且存入Properties数组中。

    Properties类在加载数据时,需要数据有固定的格式,通常是键=值。

     1 import java.util.*;
     2 import java.io.*;
     3 public class PropertiesDemo_2
     4 {
     5     public static void main(String[] args)throws IOException
     6     {
     7         method_1();
     8         //loadDemo();
     9     }
    10     public static void method_1()throws IOException
    11     {
    12         BufferedReader bufr=new BufferedReader(new FileReader("info.txt"));
    13         String line=null;
    14         Properties prop=new Properties();
    15         while((line=bufr.readLine())!=null)
    16         {
    17             String[] arr=line.split("=");
    18             
    19             prop.setProperty(arr[0],arr[1]);
    20         }
    21         System.out.println(prop);
    22         bufr.close();
    23     }
    24     //load和store方法的使用
    25     public static void loadDemo()throws IOException
    26     {
    27         Properties prop=new Properties();
    28         FileInputStream fis=new FileInputStream("info.txt");
    29         
    30         //将流中的数据加载进集合
    31         prop.load(fis);
    32         //改变集合内容
    33         prop.setProperty("Tina","16");
    34         FileOutputStream fos=new FileOutputStream("info.txt");
    35         //改变文件内容,使用store方法
    36         prop.store(fos,"hahaha~~~");
    37 
    38         System.out.println(prop);
    39         fis.close();
    40         fos.close();
    41 
    42     }
    43     
    44 }
    View Code

    14 Properties练习

    用于记录应用程序运行次数,
    如果使用次数已到,那么给出注册提示。

    很容易想到的是:计数器
    可是该计数器定义在程序中,随着程序的运行而在内存中存在,并自行自增,
    可是随着该应用程序的退出,该计数器也在内存中消失了。

    下一次启动该程序,又会重新从0开始计数,
    这样不是我们想要的。
    程序即使结束,该计数器的值也应该存在。
    下次程序启动会在加载该计数器的并加1后重新存储起来。

    所以要建立一个配置文件,用于记录该软件的使用次数。

    该配置文件使用键值对的形式,这样便于阅读和操作数据。
    键值对数据是map集合,
    数据是以文件形式存储,使用io技术。
    那么map+io-->properties

    配置文件可以实现应用程序数据的共享。

    具体实现代码如下:

     1 import java.io.*;
     2 import java.util.*;
     3 public class  RunCount
     4 {
     5     public static void main(String[] args) throws IOException
     6     {
     7         Properties prop=new Properties();
     8         //把文件封装成对象
     9         File file=new File("count.ini");
    10         //如果文件不存在,新建文件
    11         if(!file.exists())
    12             file.createNewFile();
    13         FileInputStream fis=new FileInputStream(file);
    14         //从输入流中读取属性列表
    15         prop.load(fis);
    16         
    17         //定义变量,记录运行次数
    18         int count=0;
    19         String value=prop.getProperty("time");
    20 
    21         if(value!=null)
    22         {
    23             count=Integer.parseInt(value);
    24             if(count>=5)
    25             {
    26                 System.out.println("您好,使用次数已到,请注册后再使用!");
    27                 return;
    28             }
    29         }
    30         count++;
    31         //改变Properties表中的键值
    32         prop.setProperty("time",count+"");
    33 
    34         FileOutputStream fos=new FileOutputStream(file);
    35         //将Properties表中的键和元素对写入输出流
    36         prop.store(fos,"");
    37 
    38         //关闭输入输出流
    39         fos.close();
    40         fis.close();
    41 
    42     }
    43 }
    View Code

    运行情况:

    运行第六次时,控制台的显示:

    您好,使用次数已到,请注册后再使用!

    count.ini文件的内容:

    #
    #Wed Jan 13 16:33:13 CST 2016
    time=5

    15 PrintWriter

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

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

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

     1 import java.io.*;
     2 public class  PrintDemo
     3 {
     4     public static void main(String[] args) throws IOException
     5     {
     6         BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
     7         //自动刷新
     8         //PrintWriter out=new PrintWriter(System.out,true);
     9         PrintWriter out=new PrintWriter(new FileWriter("g.txt"),true);
    10 
    11         String line=null;
    12         while((line=bufr.readLine())!=null)
    13         {
    14             if("over".equals(line))
    15                 break;
    16             //带换行的打印
    17             out.println(line.toUpperCase());
    18             //out.flush();
    19 
    20         }
    21         out.close();
    22         bufr.close();
    23         
    24     }
    25 }
    View Code

    16 合并流

    SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,

    直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。

    可以用来合并文件。

     1 import java.io.*;
     2 import java.util.*;
     3 public class  SequenceInputStreamDemo
     4 {
     5     public static void main(String[] args) throws IOException
     6     {
     7         Vector<FileInputStream> v=new Vector<FileInputStream>();
     8 
     9         v.add(new FileInputStream("d:\abc\1.txt"));
    10         v.add(new FileInputStream("d:\abc\2.txt"));
    11         v.add(new FileInputStream("d:\abc\3.txt"));
    12 
    13         Enumeration<FileInputStream> en=v.elements();
    14 
    15         SequenceInputStream sis=new SequenceInputStream(en);
    16         FileOutputStream fos=new FileOutputStream("d:\abc\4.txt");
    17 
    18         byte[] buf=new byte[1024];
    19 
    20         int len=0;
    21         while((len=sis.read(buf))!=-1)
    22         {
    23             fos.write(buf,0,len);
    24         }
    25         fos.close();
    26         sis.close();
    27 
    28 
    29     }
    30 }
    View Code

    17 切割文件

    文件可以合并,同样也可以切割。当网站的一次上传大小有一定限制时,需要上传较大文件时,就需要用到文件的切割。

     1 import java.io.*;
     2 //切割文件
     3 public class SplitFile
     4 {
     5     public static void main(String[] args)throws IOException 
     6     {
     7         splitFile();
     8         
     9     }
    10     public static void splitFile()throws IOException 
    11     {
    12         FileInputStream fis=new FileInputStream("d:\abc\bird.jpg");
    13         FileOutputStream fos=null;
    14 
    15 
    16         byte[] buf=new byte[1024];
    17         int len=0;
    18         int count =1;
    19         while((len=fis.read(buf))!=-1)
    20         {
    21             fos=new FileOutputStream("d:\abc\"+(count++)+".part");
    22             fos.write(buf,0,len);
    23             fos.close();
    24 
    25         }
    26         fis.close();
    27 
    28     }
    29 }
    View Code
  • 相关阅读:
    操作系统
    网络通信协议(互联网协议)
    返回Json数据 结构封装R
    开放平台,开发者的时代到来?
    seo的html代码优化
    推广网站要做的事情
    Java对象转xml字符串
    也来一段Mootools的图片切换代码
    new关键字用法
    关于CTS、CLS、CLR
  • 原文地址:https://www.cnblogs.com/wsw-tcsygrwfqd/p/5128115.html
Copyright © 2020-2023  润新知