欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

首頁綜合 正文
目錄

柚子快報激活碼778899分享:Java之文件操作和IO

柚子快報激活碼778899分享:Java之文件操作和IO

http://yzkb.51969.com/

目錄

File類

屬性

構造方法

方法??

文件內容的讀寫

InputStream

OutputStream

File類

屬性

修飾符及類型屬性說明static StringpathSeparator依賴于系統(tǒng)的路徑分隔符,String類型的表示static charpathSeparator依賴于系統(tǒng)的路徑分隔符,char類型的表示

??

構造方法

簽名說明File(File parent,String child)根據(jù)父目錄+孩子文件路徑,創(chuàng)建一個新的File實例File(String pathname)根據(jù)文件路徑創(chuàng)建一個新的File實例,路徑可以是絕對路徑或者相對路徑File(String parent,String child)根據(jù)父目錄+孩子文件路徑,創(chuàng)建一個新的File實例,父目錄用路徑表示

方法??

修飾符及方法返回值類型方法簽名說明StringgetParent()返回File對象的父目錄文件路徑StringgetName()返回File對象的純文件名稱StringgetPath()返回File對象的文件路徑StringgetAbsolutePath()返回File對象的的絕對路徑StringgetCanonicalPath()返回File對象的修飾過的絕對路徑booleanexists()判斷File對象描述的文件是否真實存在booleanisDirectory()判斷File對象代表的文件是否是一個目錄booleanisFile()判斷File對象代表的文件是否是一個普通文件booleancreateNewFile()根據(jù)File對象,自動創(chuàng)建一個空文件,成功創(chuàng)建后返回truebooleandelete()根據(jù)File對象,刪除該文件,成功刪除后返回truevoiddeleteOnExit()根據(jù)File對象,標注文件將被刪除,刪除動作會到JVM運行結束后才會進行String[]list()返回File對象代表的目錄下的所有文件名File[]listFiles()返回File對象代表的目錄下的所有文件,以File對象表示booleanmkdir()創(chuàng)建File對象代表的目錄booleanmkdirs()創(chuàng)建File對象代表的目錄,如果必要,會創(chuàng)建中間目錄booleanrenameTo(Filedest)進行文件改名,也可以視為剪切,粘貼操作booleancanRead()判斷用戶是否對文件有可讀權限booleancanWrite() 判斷用戶是否對文件有可寫權限

代碼示例1

public class demo1 {

public static void main(String[] args) throws IOException {

File f=new File("./test.txt");

System.out.println(f.getParent());

System.out.println(f.getName());

System.out.println(f.getPath());

System.out.println(f.getAbsolutePath());

System.out.println(f.getCanonicalPath());

}

}

運行結果

代碼示例2

public class demo2 {

public static void main(String[] args) throws IOException {

File file=new File("d:/test.txt");

System.out.println(file.exists());

System.out.println(file.isDirectory());

System.out.println(file.isFile());

//創(chuàng)建文件

boolean ret=file.createNewFile();

System.out.println("ret = "+ret);

System.out.println(file.exists());

System.out.println(file.isDirectory());

System.out.println(file.isFile());

}

}

運行結果

代碼示例3

public class demo3 {

public static void main(String[] args) throws InterruptedException {

File f=new File("d:/test.txt");

// boolean ret=f.delete();

// System.out.println("ret = "+ret);

f.deleteOnExit();

Thread.sleep(5000);

System.out.println("進程結束");

}

}

第二種刪除方式是進程運行結束后才刪除。

代碼示例4

public class demo4 {

public static void main(String[] args) {

File f=new File("d:/");

String[] files=f.list();

System.out.println(Arrays.toString(files));

}

}

運行結果

代碼示例5

public class demo5 {

public static void main(String[] args) {

File f=new File("d:/aaa/bbb/ccc");

// boolean ret=f.mkdir();

boolean ret=f.mkdirs();

System.out.println("ret = "+ret);

}

}

運行結果

文件內容的讀寫

InputStream

方法

修飾符及返回值類型方法簽名說明intread()讀取一個字節(jié)的數(shù)據(jù),返回-1代表已經(jīng)完全讀完了intread(byte[] b)最多讀取b.length字節(jié)的數(shù)據(jù)到b中,返回實際獨到的數(shù)量,-1代表讀完了intread(byte[] b,int off,int len)最多讀取len-off字節(jié)的數(shù)據(jù)到b中,放在從off開始,返回實際獨到的數(shù)量,-1代表讀完了readclose()關閉字節(jié)流

InputStream 只是一個抽象類,要使用還需要具體的實現(xiàn)類。關于 InputStream 的實現(xiàn)類有很多,基本可以認為不同的輸入設備都可以對應一個 InputStream 類,我們現(xiàn)在只關心從文件中讀取,所以使用 FileInputStream?。

FileInputStream

簽名說明FileInputStream(File file)利用File構造文件輸入流FileInputStream(String name)利用文件路徑構造文件輸入流

代碼示例1

public class demo9 {

public static void main(String[] args) {

try(InputStream inputStream=new FileInputStream("d:/test.txt")){

byte[] buffer=new byte[1024];

int n=inputStream.read(buffer);

System.out.println("n = "+n);

for (int i = 0; i < n; i++) {

System.out.printf("%x\n",buffer[i]);

}

} catch (FileNotFoundException e) {

throw new RuntimeException(e);

} catch (IOException e) {

throw new RuntimeException(e);

}

}

}

?運行結果

代碼示例2

public class demo11 {

public static void main(String[] args) {

try(InputStream inputStream=new FileInputStream("d:/test.txt")){

Scanner scanner=new Scanner(inputStream);

String s=scanner.next();

System.out.println(s);

} catch (FileNotFoundException e) {

throw new RuntimeException(e);

} catch (IOException e) {

throw new RuntimeException(e);

}

}

}

??運行結果

OutputStream

方法

修飾符及返回值類型方法簽名說明voidwrite(int b)寫入一個字節(jié)的數(shù)據(jù)voidwrite(byte[] b)將b這個字節(jié)數(shù)組中的數(shù)組全部寫入OSintwrite(byte[] b,int off,int len)將b這個字節(jié)數(shù)組中從off開始的數(shù)據(jù)寫入OS,一共寫len個voidclose()關閉字節(jié)流voidflush()

重要:我們知道

I/O

的速度是很慢的,所以,大多的

OutputStream

為了減少設備操作的次數(shù),在寫數(shù)據(jù)的時候都會將數(shù)據(jù)先暫時寫入內存的

一個指定區(qū)域里,直到該區(qū)域滿了或者其他指定條件時才真正將數(shù)據(jù)寫

入設備中,這個區(qū)域一般稱為緩沖區(qū)。但造成一個結果,就是我們寫的

數(shù)據(jù),很可能會遺留一部分在緩沖區(qū)中。需要在最后或者合適的位置,

調用

flush

(刷新)操作,將數(shù)據(jù)刷到設備中。

?OutputStream 同樣只是一個抽象類,要使用還需要具體的實現(xiàn)類。我們現(xiàn)在還是只關心寫入文件中,所以使用 FileOutputStream。

代碼示例1

public class demo10 {

public static void main(String[] args) {

try(OutputStream outputStream=new FileOutputStream("d:/test.txt",true)) {

String s="你好世界";

outputStream.write(s.getBytes());

} catch (FileNotFoundException e) {

throw new RuntimeException(e);

} catch (IOException e) {

throw new RuntimeException(e);

} ;

}

}

運行結果

代碼示例2

public class demo12 {

public static void main(String[] args) {

try(OutputStream outputStream=new FileOutputStream("d:/test.txt")){

PrintWriter writer=new PrintWriter(outputStream);

writer.println("hello");

writer.flush();

} catch (FileNotFoundException e) {

throw new RuntimeException(e);

} catch (IOException e) {

throw new RuntimeException(e);

}

}

}

運行結果

代碼示例3

刪除指定目錄下的指定關鍵詞的文件

public class demo13 {

public static void main(String[] args) {

Scanner scanner=new Scanner(System.in);

System.out.println("請輸入要掃描的路徑");

String path=scanner.next();

File rootPath=new File(path);

if(!rootPath.isDirectory()){

System.out.println("輸入的路徑有誤");

return;

}

System.out.println("請輸入要刪除文件的關鍵詞");

String word=scanner.next();

scanDir(rootPath,word);

}

private static void scanDir(File rootPath,String word){

File[] files=rootPath.listFiles();

if(files==null)

return;

for (File f:files) {

System.out.println("當前掃描的文件:"+f.getAbsolutePath());

if(f.isFile())

checkDelete(f,word);

else

scanDir(f,word);

}

}

private static void checkDelete(File f,String word){

if(!f.getName().contains(word))

return;

System.out.println("當前文件為:"+f.getAbsolutePath()+",請確認是否刪除");

Scanner scanner=new Scanner(System.in);

String choice=scanner.next();

if(choice.equals("Y") || choice.equals("y")){

f.delete();

System.out.println("刪除完畢");

}else{

System.out.println("取消刪除");

}

}

}

柚子快報激活碼778899分享:Java之文件操作和IO

http://yzkb.51969.com/

精彩內容

評論可見,查看隱藏內容

本文內容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。

轉載請注明,如有侵權,聯(lián)系刪除。

本文鏈接:http://m.gantiao.com.cn/post/19461849.html

發(fā)布評論

您暫未設置收款碼

請在主題配置——文章設置里上傳

掃描二維碼手機訪問

文章目錄