JAVA基础------IO操作(一)

 

【案例1】创建一个新文件

 
 
  1. import java.io.*;  
  2. class hello{  
  3.     public static void main(String[] args) {  
  4.         File f=new File("D:\\hello.txt");  
  5.         try{  
  6.             f.createNewFile();  
  7.         }catch (Exception e) {  
  8.             e.printStackTrace();  
  9.         }  
  10.     }  

【运行结果】:

程序运行之后,在d盘下会有一个名字为hello.txt的文件。

【案例2】File类的两个常量

 
 
  1. import java.io.*;  
  2. class hello{  
  3.     public static void main(String[] args) {  
  4.         System.out.println(File.separator);  
  5.         System.out.println(File.pathSeparator);  
  6.     }  

【运行结果】:
\
;
此处多说几句:有些同学可能认为,我直接在windows下使用\进行分割不行吗?当然是可以的。但是在linux下就不是\了。所以,要想使得我们的代码跨平台,更加健壮,所以,大家都采用这两个常量吧,其实也多写不了几行。呵呵、

现在我们使用File类中的常量改写上面的代码:

 
 
  1. import java.io.*;  
  2. class hello{  
  3.     public static void main(String[] args) {  
  4.         String fileName="D:"+File.separator+"hello.txt";  
  5.         File f=new File(fileName);  
  6.         try{  
  7.             f.createNewFile();  
  8.         }catch (Exception e) {  
  9.             e.printStackTrace();  
  10.         }  
  11.     }  

你看,没有多写多少吧,呵呵。所以建议使用File类中的常量。

删除一个文件

 
 
  1. /**  
  2.  * 删除一个文件  
  3.  * */ 
  4. import java.io.*;  
  5. class hello{  
  6.     public static void main(String[] args) {  
  7.         String fileName="D:"+File.separator+"hello.txt";  
  8.         File f=new File(fileName);  
  9.         if(f.exists()){  
  10.             f.delete();  
  11.         }else{  
  12.             System.out.println("文件不存在");  
  13.         }  
  14.           
  15.     }  

创建一个文件夹

 
 
  1. /**  
  2.  * 创建一个文件夹  
  3.  * */ 
  4. import java.io.*;  
  5. class hello{  
  6.     public static void main(String[] args) {  
  7.         String fileName="D:"+File.separator+"hello";  
  8.         File f=new File(fileName);  
  9.         f.mkdir();  
  10.     }  

【运行结果】:

D盘下多了一个hello文件夹

列出指定目录的全部文件(包括隐藏文件):

 
 
  1. /**  
  2.  * 使用list列出指定目录的全部文件  
  3.  * */ 
  4. import java.io.*;  
  5. class hello{  
  6.     public static void main(String[] args) {  
  7.         String fileName="D:"+File.separator;  
  8.         File f=new File(fileName);  
  9.         String[] str=f.list();  
  10.         for (int i = 0; i < str.length; i++) {  
  11.             System.out.println(str[i]);  
  12.         }  
  13.     }  

【运行结果】:

$RECYCLE.BIN

360

360Downloads

360Rec

360SoftMove

Config.Msi

da

Downloads

DriversBackup

eclipse

java web整合开发和项目实战

Lenovo

MSOCache

Program

Program Files

python

RECYGLER.{8F92DA15-A229-A4D5-B5CE-5280C8B89C19}

System Volume Information

Tomcat6

var

vod_cache_data

新建文件夹

(你的运行结果应该和这个不一样的,呵呵)

但是使用list返回的是String数组,。而且列出的不是完整路径,如果想列出完整路径的话,需要使用listFiles.他返回的是File的数组

列出指定目录的全部文件(包括隐藏文件):

 
 
  1. /**  
  2.  * 使用listFiles列出指定目录的全部文件  
  3.  * listFiles输出的是完整路径  
  4.  * */ 
  5. import java.io.*;  
  6. class hello{  
  7.     public static void main(String[] args) {  
  8.         String fileName="D:"+File.separator;  
  9.         File f=new File(fileName);  
  10.         File[] str=f.listFiles();  
  11.         for (int i = 0; i < str.length; i++) {  
  12.             System.out.println(str[i]);  
  13.         }  
  14.     }  

【运行结果】:

D:\$RECYCLE.BIN

D:\360

D:\360Downloads

D:\360Rec

D:\360SoftMove

D:\Config.Msi

D:\da

D:\Downloads

D:\DriversBackup

D:\eclipse

D:\java web整合开发和项目实战

D:\Lenovo

D:\MSOCache

D:\Program

D:\Program Files

D:\python

D:\RECYGLER.{8F92DA15-A229-A4D5-B5CE-5280C8B89C19}

D:\System Volume Information

D:\Tomcat6

D:\var

D:\vod_cache_data

D:\新建文件夹

通过比较可以指定,使用listFiles更加方便、

 

判断一个指定的路径是否为目录

 

 
 
  1. /**  
  2.  * 使用isDirectory判断一个指定的路径是否为目录  
  3.  * */ 
  4. import java.io.*;  
  5. class hello{  
  6.     public static void main(String[] args) {  
  7.         String fileName="D:"+File.separator;  
  8.         File f=new File(fileName);  
  9.         if(f.isDirectory()){  
  10.             System.out.println("YES");  
  11.         }else{  
  12.             System.out.println("NO");  
  13.         }  
  14.     }  

【运行结果】:YES

搜索指定目录的全部内容

 

 
 
  1. /**  
  2.  * 列出指定目录的全部内容  
  3.  * */ 
  4. import java.io.*;  
  5. class hello{  
  6.     public static void main(String[] args) {  
  7.         String fileName="D:"+File.separator;  
  8.         File f=new File(fileName);  
  9.         print(f);  
  10.     }  
  11.     public static void print(File f){  
  12.         if(f!=null){  
  13.             if(f.isDirectory()){  
  14.                 File[] fileArray=f.listFiles();  
  15.                 if(fileArray!=null){  
  16.                     for (int i = 0; i < fileArray.length; i++) {  
  17.                         //递归调用  
  18.                         print(fileArray[i]);  
  19.                     }  
  20.                 }  
  21.             }  
  22.             else{  
  23.                 System.out.println(f);  
  24.             }  
  25.         }  
  26.     }  

【运行结果】:

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\framepages\web4welcome_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\help_005fhome_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\help_005fhome_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\home_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\home_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\index_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\index_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\login_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\login_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\modify_005fuser_005finfo_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\modify_005fuser_005finfo_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\register_005fnotify_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\register_005fnotify_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\sign_005fup_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\sign_005fup_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\transit_jsp.class

……

【使用RandomAccessFile写入文件】

 

 
 
  1. /**  
  2.  * 使用RandomAccessFile写入文件  
  3.  * */ 
  4. import java.io.*;  
  5. class hello{  
  6.     public static void main(String[] args) throws IOException {  
  7.         String fileName="D:"+File.separator+"hello.txt";  
  8.         File f=new File(fileName);  
  9.         RandomAccessFile demo=new RandomAccessFile(f,"rw");  
  10.         demo.writeBytes("asdsad");  
  11.         demo.writeInt(12);  
  12.         demo.writeBoolean(true);  
  13.         demo.writeChar('A');  
  14.         demo.writeFloat(1.21f);  
  15.         demo.writeDouble(12.123);  
  16.         demo.close();     
  17.     }  

如果你此时打开hello。txt查看的话,会发现那是乱码。

字节流

【向文件中写入字符串】

 

 
 
  1. /**  
  2.  * 字节流  
  3.  * 向文件中写入字符串  
  4.  * */ 
  5. import java.io.*;  
  6. class hello{  
  7.     public static void main(String[] args) throws IOException {  
  8.         String fileName="D:"+File.separator+"hello.txt";  
  9.         File f=new File(fileName);  
  10.         OutputStream out =new FileOutputStream(f);  
  11.         String str="你好";  
  12.         byte[] b=str.getBytes();  
  13.         out.write(b);  
  14.         out.close();  
  15.     }  

查看hello.txt会看到“你好”

当然也可以一个字节一个字节的写。

 

 
 
  1. /**  
  2.  * 字节流  
  3.  * 向文件中一个字节一个字节的写入字符串  
  4.  * */ 
  5. import java.io.*;  
  6. class hello{  
  7.     public static void main(String[] args) throws IOException {  
  8.         String fileName="D:"+File.separator+"hello.txt";  
  9.         File f=new File(fileName);  
  10.         OutputStream out =new FileOutputStream(f);  
  11.         String str="你好";  
  12.         byte[] b=str.getBytes();  
  13.         for (int i = 0; i < b.length; i++) {  
  14.             out.write(b[i]);  
  15.         }  
  16.         out.close();  
  17.     }  

结果还是:“你好”

向文件中追加新内容:

 

 
 
  1. /**  
  2.  * 字节流  
  3.  * 向文件中追加新内容:  
  4.  * */ 
  5. import java.io.*;  
  6. class hello{  
  7.     public static void main(String[] args) throws IOException {  
  8.         String fileName="D:"+File.separator+"hello.txt";  
  9.         File f=new File(fileName);  
  10.         OutputStream out =new FileOutputStream(f,true);  
  11.         String str="Rollen";  
  12.         //String str="\r\nRollen";  可以换行  
  13.         byte[] b=str.getBytes();  
  14.         for (int i = 0; i < b.length; i++) {  
  15.             out.write(b[i]);  
  16.         }  
  17.         out.close();  
  18.     }  

【运行结果】:

你好Rollen

【读取文件内容】

 

 
 
  1. /**  
  2.  * 字节流  
  3.  * 读文件内容  
  4.  * */ 
  5. import java.io.*;  
  6. class hello{  
  7.     public static void main(String[] args) throws IOException {  
  8.         String fileName="D:"+File.separator+"hello.txt";  
  9.         File f=new File(fileName);  
  10.         InputStream in=new FileInputStream(f);  
  11.         byte[] b=new byte[1024];  
  12.         in.read(b);  
  13.         in.close();  
  14.         System.out.println(new String(b));  
  15.     }  

【运行结果】
你好Rollen

Rollen_

但是这个例子读取出来会有大量的空格,我们可以利用in.read(b);的返回值来设计程序。如下:

 

 
 
  1. /**  
  2.  * 字节流  
  3.  * 读文件内容  
  4.  * */ 
  5. import java.io.*;  
  6. class hello{  
  7.     public static void main(String[] args) throws IOException {  
  8.         String fileName="D:"+File.separator+"hello.txt";  
  9.         File f=new File(fileName);  
  10.         InputStream in=new FileInputStream(f);  
  11.         byte[] b=new byte[1024];  
  12.         int len=in.read(b);  
  13.         in.close();  
  14.         System.out.println("读入长度为:"+len);  
  15.         System.out.println(new String(b,0,len));  
  16.     }  

【运行结果】:

读入长度为:18

你好Rollen

Rollen

读者观察上面的例子可以看出,我们预先申请了一个指定大小的空间,但是有时候这个空间可能太小,有时候可能太大,我们需要准确的大小,这样节省空间,那么我们可以这样干:

 

 
 
  1. /**  
  2.  * 字节流  
  3.  * 读文件内容,节省空间  
  4.  * */ 
  5. import java.io.*;  
  6. class hello{  
  7.     public static void main(String[] args) throws IOException {  
  8.         String fileName="D:"+File.separator+"hello.txt";  
  9.         File f=new File(fileName);  
  10.         InputStream in=new FileInputStream(f);  
  11.         byte[] b=new byte[(int)f.length()];  
  12.         in.read(b);  
  13.         System.out.println("文件长度为:"+f.length());  
  14.         in.close();  
  15.         System.out.println(new String(b));  
  16.     }  

文件长度为:18

你好Rollen

Rollen

将上面的例子改为一个一个读:

 

 
 
  1. /**  
  2.  * 字节流  
  3.  * 读文件内容,节省空间  
  4.  * */ 
  5. import java.io.*;  
  6. class hello{  
  7.     public static void main(String[] args) throws IOException {  
  8.         String fileName="D:"+File.separator+"hello.txt";  
  9.         File f=new File(fileName);  
  10.         InputStream in=new FileInputStream(f);  
  11.         byte[] b=new byte[(int)f.length()];  
  12.         for (int i = 0; i < b.length; i++) {  
  13.             b[i]=(byte)in.read();  
  14.         }  
  15.         in.close();  
  16.         System.out.println(new String(b));  
  17.     }  

输出的结果和上面的一样。

细心的读者可能会发现,上面的几个例子都是在知道文件的内容多大,然后才展开的,有时候我们不知道文件有多大,这种情况下,我们需要判断是否独到文件的末尾。

 

 
 
  1. /**  
  2.  * 字节流  
  3.  *读文件  
  4.  * */ 
  5. import java.io.*;  
  6. class hello{  
  7.     public static void main(String[] args) throws IOException {  
  8.         String fileName="D:"+File.separator+"hello.txt";  
  9.         File f=new File(fileName);  
  10.         InputStream in=new FileInputStream(f);  
  11.         byte[] b=new byte[1024];  
  12.         int count =0;  
  13.         int temp=0;  
  14.         while((temp=in.read())!=(-1)){  
  15.             b[count++]=(byte)temp;  
  16.         }  
  17.         in.close();  
  18.         System.out.println(new String(b));  
  19.     }  

【运行结果】

你好Rollen

Rollen_

提醒一下,当独到文件末尾的时候会返回-1.正常情况下是不会返回-1的

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值