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

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

字节打印流
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();


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值