if(files[i].isHidden()){
System.out.println(files[i].getName()+"?
?
:
"+files[i].getAbsolutePath());
}
if(files[i].exists()){
files[i].delete();
}
}
}
3.打印文件夹下的隐藏文件
4.打印某文件夹下所有文件名称,包括文件夹名称,包含子文件夹情况。
【采用递归算法】
publicstaticvoidmain(String[]args){
Filefile=newFile("c:
"+File.separator+"temp");
printFileName(file);
}
privatestaticvoidprintFileName(Filefile){
if(file!
=null){//?
?
?
?
?
?
?
?
if(file.isDirectory()){//?
?
?
?
?
?
?
System.out.println("?
?
:
"+file.getName());
File[]files=file.listFiles();//?
?
?
?
?
?
?
?
?
?
for(Filef:
files){
printFileName(f);
}
}else{
System.out.println("?
?
?
?
:
"+file.getName());
}
}
}
作业:
利用JTree实现文件展开、折叠。
【递归、JTree】
3、System处理输入输出
静态属性:
Out:
?
?
?
?
?
?
?
PrintStream?
In:
?
?
?
?
?
?
?
InputStream?
Err:
?
?
?
?
?
?
?
?
?
?
PrintStream?
?
?
?
?
:
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
publicclassSystemDemo1{
publicvoidaddData(){
//?
?
?
?
System.out.println(
"?
?
?
?
?
"+SystemDemo1.class.getName()+"?
?
?
?
addData()"+
"?
?
?
?
"+parseDate());
}
publicstaticvoidmain(String[]args){
Filefile=
newFile("c:
"+File.separator+"log"+File.separator+"log.txt");
if(file.exists()&&file.isFile()){//?
?
?
?
?
?
?
?
?
?
?
?
?
try{
System.setOut(newPrintStream(file));
newSystemDemo1().addData();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
}else{
try{
file.createNewFile();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
privatestaticStringparseDate(){
SimpleDateFormatsdf=newSimpleDateFormat("yyyy/MM/ddHH:
mm:
ss");
Stringdate=sdf.format(newDate());
returndate;
}
}
?
?
:
?
?
?
?
?
?
?
publicstaticvoidmain(String[]args){
Filefile=
newFile("c:
"+File.separator+"log"+File.separator+"log.txt");
if(file.exists()&&file.isFile()){//?
?
?
?
?
?
?
?
?
?
?
?
?
try{
System.setIn(newFileInputStream(file));
Scanners=newScanner(System.in);
while(s.hasNext()){
System.out.println(s.nextLine());
}
s.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
}
}
?
?
?
?
:
1.?
?
简单的键盘记录器。
对于木马、病毒等,通常可以监视用户在键盘上的操作,如输入用户名和密码,从而盗取账号。
通过在控制台输入用户名和密码,将数据写入到文件中,并已明文显示。
4、输入输出流
在JAVA中所有的数据都是以流的形式进行传输或保存的,程序需要数据时要使用输入流读取数据,当程序需要将一些数据保存起来,则要使用输出流。
·输入流
将外部数据源的数据转换为流,程序就通过读取该类流中的数据,完成对外部数据源数据的读入。
·输出流
将类中流数据转换为外部数据源,完成将程序中的数据写入到外部数据源中。
IO?
?
?
?
?
?
:
?
?
?
(ByteStream)?
?
?
?
(CharStream)。
字节流是按照字节为单位,一个字节一个字节形成流。
字符流是按照字符为单位,一个字符一个字符形成流。
字节流:
输入流:
InputStream输出流:
OutputStream
字符流:
输入流:
Reader输出流:
Writer
一般来说,对文件进行操作有下面几个步骤:
v使用File类打开一个文件
v使用字节流或字符流来指定输出的位置。
v进行读写操作。
v关闭输入输出流【切记】
5、输出流
目的:
将内存中数据保存到文件中。
v字节流
OutputStream类:
抽象类,其常用的方法:
1.close():
关闭输出流一般来说放置于finally处进行流的关闭
2.flush():
刷新缓冲区强制性地从内存数据同步到外部数据源中
3.write():
往外部数据源写数据
publicstaticvoidmain(String[]args){
OutputStreamout=System.out;
Stringstr="hello,java,?
?
?
";
byte[]bytes=str.getBytes();//?
?
?
?
?
?
?
?
?
?
?
try{
out.write(bytes);
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(out!
=null){
out.close();//?
?
?
?
?
?
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
v文件输出流
FileOutputStream:
?
?
?
:
1.传入File类型
FileOutputStream(Filefile):
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
2.?
?
?
?
File?
?
?
?
?
?
?
?
FileOutputStream(Filefile,booleanappend):
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
[\r\n]。
?
?
:
?
?
?
?
?
?
?
?
?
?
?
?
?
publicclassFileOutputStreamDemo1{
publicstaticvoidmain(String[]args){
OutputStreamout=null;
Stringstr="\r\nhello,java,?
?
?
1";
byte[]bytes=str.getBytes();//?
?
?
?
?
?
?
?
?
?
?
try{
out=newFileOutputStream(newFile("c:
//abc.txt"),true);
out.write(bytes);
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(out!
=null){
out.close();//?
?
?
?
?
?
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
v缓冲输出流
?
?
?
?
?
?
?
IO?
?
?
?
?
?
?
?
?
?
?
?
IO?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
BufferedOutputStream?
?
?
?
?
?
?
?
?
flush?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
:
1.BufferedOutputStream(OutputStreamout):
创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
2.BufferedOutputStream(OutputStreamout,intsize):
创建一个新的缓冲输出流,并制定缓冲输出流大小,以将数据写入指定的底层输出流。
注意:
一定要记得关闭输出流,另外记得使?
flush?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
BufferedOutputStreambos=null;
OutputStreamos=System.out;
bos=newBufferedOutputStream(os);//?
?
?
?
?
?
?
try{
bos.write("?
?
?
".getBytes());
bos.flush();
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(bos!
=null){
bos.close();
}
if(os!
=null){
os.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
6、输入流
InputStream?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
:
1.available():
返回当前输入流读取时返回的有效字节数
2.close():
关闭输入流
3.read():
将外部数据源中输入流读入到程序中
4.mark(intreadlimit):
打标记
5.reset():
重新回到最后一个mark的位置。
6.skip():
publicstaticvoidmain(String[]args){
InputStreamis=System.in;//?
?
?
InputStream?
?
System.out.println("?
?
?
?
?
:
");
byte[]bytes=newbyte[1024];
try{
is.read(bytes);//?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
System.out.println("?
?
?
?
?
?
?
:
");
Stringstr=newString(bytes);
System.out.println(str);
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(is!
=null){
is.close();
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
字节读入过程中,组装字符串采用String的构造函数。
如果自己写的话,针对汉字需要两个字节合并为一个字符,因此可以利用范围进行判断。
例如是字母、数字的可以直接进行转换,对于汉字的则采取两个字节合并。
如果不做这样的处理,则会出现中文乱码情况出现。
v作业:
1.利用FileInputStream实现对文件的读取,并利用OutputStream将数据写出到控制台。
2.实现文件复制功能
对文件进行复制,建议采用字节流形式进行处理,原因因为文档并不一定是有文字的文档,有可能为图片、音乐等。
注意:
应该采用边读边写的方式,因为有可能文件过大产生内存溢出的情况。
处理方式见【一个字节一个字节读取】
3.扩展:
实现文件夹复制功能
publicclassFileDirectoryCopy{
/**
*
*@paramdescFile:
?
?
?
?
?
*@paramfiles:
?
?
?
?
?
?
?
?
*/
privatestaticvoidcopy(FiledescFile,File[]files){
for(inti=0;i//?
?
?
?
?
?
?
?
?
?
?
if(files[i].isFile()){//?
?
?
InputStreamis=null;
OutputStreamos=null;
try{
is=newFileInputStream(files[i]);
Filefile=
newFile(descFile.getAbsolutePath()+File.separator+
files[i].getName());//?
?
?
?
File?
?
os=newFileOutputStream(file);//?
?
?
?
?
?
?
?
byte[]bytes=newbyte[(int)files[i].length()];
is.read(bytes);//?
?
?
?
?
?
?
?
?
?
?
?
?
?
os.write(bytes);//?
?
?
?
?
?
?
?
?
?
?
?
?
?
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
os.close();
is.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
if(files[i].isDirectory()){//?
?
?
?
Filefile=
newFile(descFile.getAbsolutePath()+File.separator
+files[i].getName());
if(!
file.exists()){//?
?
?
?
?
?
?
?
?
?
file.mkdir();
}
copy(file,files[i].listFiles());//?
?
?
?
}
}
}
publicstaticvoidmain(String[]args){
FilesourceFile=newFile("c:
//log");//?
?
?
FiledescFile=newFile("c:
//copy");//?
?
?
?
?
?
?
if(!
sourceFile.isDirectory()||!
sourceFile.exists()){
System.out.println("?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
");
}else{
if(!
descFile.exists()){//?
?
?
?
?
?
?
?
?
?
?
descFile.mkdir();
}
copy(descFile,sourceFile.listFiles());
}
}
}
vFileInputStream
1.指定数组长度
is=newFileInputStream(file);//?
File?
?
?
?
?
?
?
?
byteb[]=newbyte[1024];
intlength=is.read(b);//?
?
?
?
?
?
?
System.out.println("length:
"+length);
System.out.println(newString(b,0,length));
2.通过文件指定数组长度
byteb[]=newbyte[(int)file.length()];
is.read(b);
System.out.println(newString(b));
3.一个字节一个字节读取
publicstaticvoidmain(String[]args){
Filefile=newFile("c:
//bcd.txt");
InputStreamio;
BufferedInputStreambis;
try{
io=newFileInputStream(file);
bis=newBufferedInputStream(io);
byte[]bytes=newbyte[(int)file.length()];
inttemp=0;
inti=0;
while((temp=bis.read())!
=-1){//?
?
?
?
?
?
?
?
?
?
?
bytes[i]=(byte)temp;
i++;
}
Stringstr=newString(bytes);
System.out.println(str);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
【文件导出功能】:
导出Excel、Word
JXL-----JAR包支持
POI-----支持03、07版本
【图标功能】:
曲线图、柱状图、饼图
JFreeChart
v缓冲输入流
BufferedInpuputStream。
7、字符输入输出流
字符流是以字符为单位,因此只能传输文本文件。
vWriter
Writer类是一个输出流,也是一个抽象类。
常用方法:
flush():
Close():
Write(char[]chars):
写入文件
Write(Stringstr):
publicstaticvoidmain(String[]args){
Writerw=newPrintWriter(System.out);
Stringstr="?
?
?
?
?
?
?
?
";
char[]chars=str.toCharArray();
FileWriterfw;
try{
w.write(chars);
//w.flush();
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
w.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
?
?
:
?
?
?
?
?
?
?
?
?
:
?
?
:
?
?
?
?
?
?
?
?
?
?
:
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
flush?
?
?
?
?
?
?
?
?
?
?
?
:
?
?
BufferWriter?
?
?
?
?
?
?
?
?
?
?
?
?
publicstaticvoidmain(String[]args){
Writerw=null;
Stringstr="?
?
?
?
?
?
?
?
";