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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

Java中的输入、输出流

發(fā)布時(shí)間:2023/12/31 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java中的输入、输出流 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

輸入輸出流

內(nèi)容概括:

存在java.io包中

所有輸入流都是抽象類InputStream(字節(jié)輸入流)和抽象類Reader(字符輸入流)的子類。

所有輸出流都是抽象類OutputStream(字節(jié)輸出流)和抽象類Writer(字符輸出流)的子類。

File類

不涉及對(duì)文件的讀寫操作,只獲取文件信息,如文件所在目錄、文件長(zhǎng)度、文件讀寫權(quán)限等。

創(chuàng)建一個(gè)File對(duì)象的構(gòu)造方法有:

  • File(String filename);
  • File(String directoryPath, String filename);
  • File(File dir , String filename); //dir是一個(gè)目錄

幾個(gè)常用方法:

文件屬性

  • public String getName() //獲取文件名
  • public boolean canRead() //是否可讀
  • public boolean exists() //是否存在
  • public String getAbsolutePath() //獲取文件絕對(duì)路徑
  • public String getParent() //獲得父目錄
  • public boolean isFile() //判斷是否是文件

Txt.java

package com.InOut;import java.io.*;public class Txt {public static void main(String[] args) {File f = new File("E:\\", "hello.txt"); //文件需事先創(chuàng)建System.out.println(f.getName() + "是可讀的嗎:" + f.canRead());System.out.println(f.getName() + "的長(zhǎng)度:" + f.length());System.out.println(f.getName() + "的絕對(duì)路徑:" + f.getAbsolutePath());File file = new File("new.txt");System.out.println("在當(dāng)前目錄下創(chuàng)建新文件" + file.getName());if (!file.exists()) {try {file.createNewFile();System.out.println("創(chuàng)建成功");} catch (IOException exp) {}}} } hello.txt是可讀的嗎:true hello.txt的長(zhǎng)度:24 hello.txt的絕對(duì)路徑:E:\hello.txt 在當(dāng)前目錄下創(chuàng)建新文件new.txt

創(chuàng)建目錄

  • public boolean mkdir();

列出目錄中的文件

  • public String[] list(); //

  • public File [] listFiles(); //

  • public String[]?list(FilenameFilter obj) //以字符串形式返回目錄下指定類型的所有文件

  • public File []?listFiles(FilenameFilter obj) //用File對(duì)象形式返回

    FilenameFilter是一個(gè)接口,該接口有一個(gè)方法:

    public boolean accept(File dir,String name);

Directory.java

package com.InOut; import java.io.*;class FileAccept implements FilenameFilter{private String extendName;public void setExtendName(String s){extendName = "."+s;}public boolean accept(File dir,String name){ //重寫接口中的方法return name.endsWith(extendName);}}public class Directory {public static void main(String[] args) {File dirFile = new File("."); //參數(shù)點(diǎn)號(hào)表示當(dāng)前目錄FileAccept fileAccept = new FileAccept();fileAccept.setExtendName("txt");String fileName[] = dirFile.list(fileAccept);//以字符串形式返回,參數(shù)為接口回調(diào)for(String name:fileName){System.out.println(name);}} } new.txt

文件的創(chuàng)建與刪除

先創(chuàng)建對(duì)象:File file = new File("E:\",mary.txt);

如果沒有名為mary.txt的文件,調(diào)用public bolean createNewFile();創(chuàng)建

file.delete();刪除文件

運(yùn)行可執(zhí)行文件

可以用Runtime類,此時(shí)將使用靜態(tài)方法創(chuàng)建對(duì)象:

Runtime ec = Runtime.getRuntime();

Exe.java

package com.InOut; import java.io.*;//打開記事本和Typora public class Exe {public static void main(String[] args) {try {Runtime ce = Runtime.getRuntime();File file = new File("c:/windows", "Notepad.exe");ce.exec(file.getAbsolutePath());file = new File("E:\\User\\Soft\\Typora\\Typora.exe");ce.exec(file.getAbsolutePath());} catch (Exception e) {System.out.println(e);}} }

文件字節(jié)輸入流

四個(gè)基本步驟:

  • 設(shè)定輸入流的源
  • 創(chuàng)建指向源的輸入流
  • 讓輸入流讀取源中的數(shù)據(jù)
  • 關(guān)閉輸入流
  • 如果需求簡(jiǎn)單,可使用InputStream的子類FileInputStream。

    構(gòu)造方法

    FileInputStream(String name);

    FileInputStream(File file);

    參數(shù)name和file指定的文件就稱為輸入流的源。通常要配合try catch使用

    關(guān)閉流

    close();

    InStream.java

    package com.InOut; import java.io.*;//使用文件字節(jié)流讀文件的內(nèi)容 public class InStream {public static void main(String[] args) {int n=-1;byte [] a =new byte[100];try{File f = new File("hello.txt");System.out.println("is file exists:"+f.exists());InputStream in = new FileInputStream(f);while((n=in.read(a,0,100))!=-1){String s = new String(a,0,n);System.out.println(s);}in.close();}catch(IOException e){System.out.println("File read error"+e);}} } is file exists:true Hello, this is my world. 你好,這是我的世界。

    文件字節(jié)輸出流

    同文件字節(jié)輸入流類似

    文件字符輸入、輸出流

    上面的文件字節(jié)輸入流、輸出流的read write方法不能很好操作Unicode字符,例如漢字可能會(huì)出現(xiàn)亂碼現(xiàn)象。

    FileReader 和 FileWriter分別是Reader 和 Writer的子類。

    CharInOut.java

    package com.InOut; import java.io.*;public class CharInOut {public static void main(String[] args) {File sourceFile = new File("hello.txt");File targetFile = new File("hello_1.txt"); //不必事先創(chuàng)建char c[] = new char[19];try{Writer out = new FileWriter(targetFile,true);Reader in = new FileReader(sourceFile);int n = -1;while((n=in.read(c))!=-1){out.write(c,0,n);}out.flush();out.close();}catch(IOException e){System.out.println("Error"+e);}} }

    緩沖流

    如果把文件字符輸入流作為BufferedReader流的源,把文件字符輸出流作為BufferedWriter流的目的地,則將具有更強(qiáng)的讀寫能力,例如按行讀取等。

    核心語(yǔ)句:String strLine = inTwo.readLine();

    //inTwo是一個(gè)BufferedReader對(duì)象,但是參數(shù)里是文件輸入流的源。

    隨機(jī)流

    RandomAccessFile類,既不是InputStream,也不是OutStream的子類。

    由RandomAccessFile類創(chuàng)建的流稱為隨機(jī)流,既可作為流的源,也可作為流的目的地。

    構(gòu)造方法

    RandomAccessFile(String name, String mod)

    RandomAccessFile(File file, String mod) //參數(shù)mod為 r 或 rw

    方法

    seek(long a) 參數(shù)a確定讀寫位置距離文件開頭的字節(jié)個(gè)數(shù)

    getFilePointer() 獲取流的當(dāng)前讀寫位置

    read Char( )

    writeFloat( )

    數(shù)組流

    • 字節(jié)數(shù)組流
    • 字符數(shù)組流

    數(shù)據(jù)流

    對(duì)象流

    其他功能

    進(jìn)度條

    ProgressMonitorInputStream(Component c, String s, InputStream); //import java.swing.**

    文件鎖

    FileChannel channel = input.getChannel(); //獲得信道 FileLock lock = channel.tryLock(); //加鎖 lock.release(); //解鎖

    “做程序員,圈子和學(xué)習(xí)最重要”因?yàn)橛杏辛巳ψ涌梢宰屇闵僮邚澛?#xff0c;擴(kuò)寬人脈,擴(kuò)展思路,學(xué)習(xí)他人的一些經(jīng)驗(yàn)及學(xué)習(xí)方法!同時(shí)在這分享一下是一直以來(lái)整理的Java后端進(jìn)階筆記文檔和學(xué)習(xí)資料免費(fèi)分享給大家!需要資料的朋友私信我扣【06】
    ?

    總結(jié)

    以上是生活随笔為你收集整理的Java中的输入、输出流的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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