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();
}
}
打印流:
该流提供了打印方法,可以将各种数据类型的数据都原样打印
字节打印流
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();
}
}
相关文章推荐
- day20File类,各方法。递归。删除一带内容的目录。Properties 。记录应用程序运行次数。打印流。切割合并文件。
- 在电脑上找到这个路径:D:\jakarta-tomcat-6\conf\Catalina,将localhost目录直接删除掉,再一运行,就没事了! 因为配置文件中配置了启动程序,而webapps文件夹下却没有此应用程序,所以出现了上述错误。
- 黑马程序员—java基础学习—删除一个带文件内容的目录
- Python_删除/创建指定目录及其下所有子目录的文件,该文件记录当前文件夹的内容
- 用于记录应用程序运行次数,如果使用次数已到那么就要给出注册提示;
- 采用多线程和生产者消费者模式来实现对于一个目录以及所有子目录下的文件内容的搜索,打印出包含指定关键字的行.
- 使用 Properties 对象,用于记录应用程序运行次数
- Python_删除/创建指定目录及其下所有子目录的文件,该文件记录当前文件夹的内容
- 实现一个命令行linux终端应用程序,把输入的指定文件,映射到内存中,并打印出文件内容
- python 学习记录(11)-文件处理/读取文件/文件写入内容/文件删除/文件复制/文件重命名/后缀名/内容查找与替换/文件比较/ 配置文件访问/目录创建与删除/遍历目录/定向输出
- C语言打开一个文件(有内容),倒序打印出文件中的内容
- 将一个文本文件内容反序打印出来(包含中文时)
- 批量删除一个目录下的文件
- java删除一个目录下面的所有文件
- 删除一个目录下的重复文件(与文件名无关)
- c语言实现删除一个txt文件中含有xxx内容的一行,并把删除之后的文件写成另一份txt文件
- 目录操作/打印一个文件或目录的详细信息
- C#文件操作源码 递归复制一个目录 递归删除文件 和 文件夹
- 在linux终端中要删除一个目录及其内文件
- 一个简单的文件目录打印程序