Java IO

1,创建文件

 1 import java.io.File;
 2 import java.io.IOException;
 3 
 4 public class CreateFileExample 
 5 {
 6     public static void main( String[] args )
 7     {    
 8         try {
 9              
10           File file = new File("c:\\newfile.txt");
11           
12           if (file.createNewFile()){
13             System.out.println("File is created!");
14           }else{
15             System.out.println("File already exists.");
16           }
17           
18         } catch (IOException e) {
19           e.printStackTrace();
20     }
21     }
22 }
23 Referen
create file

 

2,设置文件权限

 1 import java.io.File;
 2 import java.io.IOException;
 3 
 4 public class FilePermissionExample 
 5 {
 6     public static void main( String[] args )
 7     {    
 8         try {
 9              
10           File file = new File("/mkyong/shellscript.sh");
11           
12           if(file.exists()){
13               System.out.println("Is Execute allow : " + file.canExecute());
14           System.out.println("Is Write allow : " + file.canWrite());
15           System.out.println("Is Read allow : " + file.canRead());
16           }
17              
18           file.setExecutable(false);
19           file.setReadable(false);
20           file.setWritable(false);
21           
22           System.out.println("Is Execute allow : " + file.canExecute());
23           System.out.println("Is Write allow : " + file.canWrite());
24           System.out.println("Is Read allow : " + file.canRead());
25           
26           if (file.createNewFile()){
27             System.out.println("File is created!");
28           }else{
29             System.out.println("File already exists.");
30           }
31           
32         } catch (IOException e) {
33           e.printStackTrace();
34         }
35     }
36 }
set file permission

 

3,BufferedInputStream读文件

 1 import java.io.BufferedInputStream;
 2 import java.io.DataInputStream;
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.IOException;
 6 
 7 public class BufferedInputStreamExample {
 8 
 9     public static void main(String[] args) {
10 
11         File file = new File("C:\\testing.txt");
12         FileInputStream fis = null;
13         BufferedInputStream bis = null;
14         DataInputStream dis = null;
15 
16         try {
17             fis = new FileInputStream(file);
18 
19             bis = new BufferedInputStream(fis);
20             dis = new DataInputStream(bis);
21 
22             while (dis.available() != 0) {
23                 System.out.println(dis.readLine());
24             }
25 
26         } catch (IOException e) {
27             e.printStackTrace();
28         } finally {
29             try {
30                 fis.close();
31                 bis.close();
32                 dis.close();
33             } catch (IOException ex) {
34                 ex.printStackTrace();
35             }
36         }
37     }
38 }
读文件

 

4,java IO字节流(Byte Streams)

字节流

 

5,Java IO 字符流(Character Streams)

字符流

 

6,其他 http://www.mkyong.com/tutorials/java-io-tutorials/

 

转载于:https://www.cnblogs.com/pphy1884/p/5462729.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值