1,创建文件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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
2,设置文件权限
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 }
3,BufferedInputStream读文件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
5,Java IO 字符流(Character Streams)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
6,其他 http://www.mkyong.com/tutorials/java-io-tutorials/