您的位置:首页 > 编程语言 > Java开发

day20打印流,删除一个带内容的目录,用于记录应用程序运行次数,多个读取流变成一个读取流,切割文件

2016-08-30 19:44 381 查看
/*

打印流:

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

字节打印流

PrintStream

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

1.file对象 file

2.字符串路径。String

3.字节输出流.OutputStream

字符打印流

PrintWriter

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

1.file对象 file

2.字符串路径。String

3.字节输出流.OutputStream

4.字符输出流 writer

*/

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

-------------------------------------

/*

删除一个带内容的目录

删除原理

在windows是从里往外删除

既然从里往外删除,那就需要用到递归。

*/

class  RemoveDir

{
public static void main(String[] args) 
{
File dir=new File("d:\\testdir");
removeDir(dir);
}
public static void removeDir(File dir)
{
File[] files=dir.listFiles();

for ( int x=0;x<files.length ;x++ )
{
if(files[x].ishidden()&&files[x].isDirectory())
removeDir(files[x]);
else
System.out.println(files[x].toString()+"::"+files[x].delete());
}
System.out.println(dir.delete());
}

}

-----------------------------------------------------

/*

用于记录应用程序运行次数。

如果使用次数已到,那么给出注册提示。

很容易想到的是:计数器

可是该计数器定义在程序中,随着运行而在内存中,并进行自增

可是随着该应用程序退出,该计数器也在内存中消失

下一次再启动程序,又重新开始0

程序结束,该计数器的值也存在

下次程序启动先加载计数器的值加一重新存储。

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

该配置文件使用键值对的形式

这样便于阅读数据,并操作数据。

键值对数据是map集合。

数据是以文本形式存储,使用IO

-----properties

*/

import java.util.*;

import java.io.*;

class  RunCount

{
public static void main(String[] args) 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 ;
}
}
count++;

prop.setProperty("time",count+"");

FileOutputStream fos=new FileOutputStream(file);

prop.store(fos,"注册信息已更新");

fos.close();
fis.close();
}

}

----------------------------------------

/*

多个读取流变成一个读取流

*/

import java.util.*;

import java.io.*;

class SequenceDemo 

{
public static void main(String[] args) throws IOException
{
Vector<FileInputStream> v=new Vector<FileInputStream>();

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

Enumeration<FileInputStream> en=v.elements();

SequenceInputStream sis=new SequenceInputStream();

FileOutputStream fos=new FileOutputStream("c:\\4.txt");

byte[] buf=new byte[1024];

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

}

--------------------------------------

/*

切割文件

*/

import java.util.*;

import java.io.*;

class  SpiltFile

{
public static void main(String[] args) throws IOException
{
merge();
}
public static void merge()
{
ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();

for (int x=1;x<=3 ;x++ )
{
al.add(new FileInputStream("c:\\splitfiles\\"+x+".part"));
}

Iterator<FileInputStream> it=al.iterater();

Enumeration<FileInputStream> en=new Enumeration<FileInputStream>()
{
public boolean hasMoreElements();
{
return it.hasNext();
}
public FileInputStream nextElement()
{
return it.next();
}
};
SequenceInputStream sis=new SequenceInputStream();

FileOutputStream fos=new FileOutputStream("c:\\0.bmp");
byte[] buf=new byte[1024];

int len=0;
while (len=sis.readLine(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();



}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java
相关文章推荐