日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

I/O流之文件流

發(fā)布時間:2025/7/25 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 I/O流之文件流 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.文件操作類 File

1.public File(String pathname)//給定一個要操作文件的完整路徑 2.public File(File parent,String child)//給定要操作文件的父路徑和子路徑名稱

?取得了文件之后需要對文件進行操作,會用到以下方法

1.public boolean creatNewFile()throws IOException//創(chuàng)建文件 2.public boolean delete()//刪除文件 3.public boolean exists()//判斷給定的路徑是否存在

?文件的基本操作

public class Test {public static void main(String[] args) throws IOException {File f=new File("D:"+File.separator+"demo.txt");//文件的路徑if(f.exists()) {f.delete();}else {f.createNewFile();//需要完全控制數(shù)據(jù)盤} } }

如果想要創(chuàng)建帶目錄的文件,需要使用以下方法

1.public File getParentFile()//找到一個指定路徑的父路徑 2.public boolean mkdirs()//創(chuàng)建指定目錄

?舉例

public class Test {public static void main(String[] args) throws IOException {//文件路徑//D:/hello/my/test/demo.txtFile f=new File("D:"+File.separator+"hellodemo"+File.separator+"my"+File.separator+"test"+File.separator+"demo.txt");//文件的路徑if(!f.getParentFile().exists()) {//如果父路徑不存在f.getParentFile().mkdirs();//創(chuàng)建目錄}if(f.exists()){ //如果文件存在f.delete();//刪除文件}else {f.createNewFile();//創(chuàng)建新文件} } }

?File類的其它方法

1.public String getName()//取得文件名稱 2.public boolean isDirectory()//判斷給定的路徑是否是文件夾 3.public boolean isFile()//判斷給定的路徑是否是文件 4.public booleaan isHidden()//判斷是否隱藏 5.public long lastModified()//文件最后一次修改日期 6.public long length()//取得文件大小,以字節(jié)為單位返回 7.public boolean renameTo(File dest)//為文件重命名 8.public File[] listFile()//將目錄中所有文件以File對象數(shù)組的方式輸出

?取得文件信息

package 數(shù)據(jù)流;import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Date;public class Test {public static void main(String[] args) throws IOException {//文件路徑//D:/hello/my/test/demo.txtFile f=new File("D:"+File.separator+"hellodemo"+File.separator+"my"+File.separator+"test"+File.separator+"demo.txt");//文件的路徑if(!f.getParentFile().exists()) {//如果父路徑不存在f.getParentFile().mkdirs();//創(chuàng)建目錄}if(f.exists()) {System.out.println("文件名稱:"+f.getName());//獲得文件名稱System.out.println(f.getName()+(f.isDirectory()?"是一個目錄":"不是一個目錄"));System.out.println(f.getName()+(f.isFile()?"是一個文件":"不是一個文件"));System.out.println(f.getName()+(f.isHidden()?"是一個隱藏文件":"不是一個隱藏文件"));System.out.println("最后一次更改的時間"+new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒").format(new Date(f.lastModified())));}else {f.createNewFile();} } }

?為文件重命名

File newf=new File("D:"+File.separator+"hellodemo"+File.separator+"my"+File.separator+"test"+File.separator+"caizhen.txt");//指定一個文件路徑
f.renameTo(newf);//使用renameTo()方法將原本的文件名稱更改為新的文件名稱

?列出目錄內(nèi)容

package 數(shù)據(jù)流;import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Date;public class Test {public static void main(String[] args) throws IOException {//文件路徑//D:/hello/my/test/demo.txtFile f=new File("D:"+File.separator+"hellodemo"+File.separator+"my"+File.separator+"test"+File.separator);//目錄的路徑if(!f.isDirectory()) {//如果父路徑不存在System.out.println("不是目錄");}if(f.exists()) {File result[]=f.listFiles();for(int x=0;x<result.length;x++) {System.out.println(result[x]);}} } }

?這個方法只適用列出給定目錄下的文件(目錄內(nèi)容)

列出指定目錄下的全部內(nèi)容

?

轉(zhuǎn)載于:https://www.cnblogs.com/cainame/p/10417640.html

總結(jié)

以上是生活随笔為你收集整理的I/O流之文件流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。