Java中的输入、输出流
輸入輸出流
內容概括:
存在java.io包中
所有輸入流都是抽象類InputStream(字節輸入流)和抽象類Reader(字符輸入流)的子類。
所有輸出流都是抽象類OutputStream(字節輸出流)和抽象類Writer(字符輸出流)的子類。
File類
不涉及對文件的讀寫操作,只獲取文件信息,如文件所在目錄、文件長度、文件讀寫權限等。
創建一個File對象的構造方法有:
- File(String filename);
- File(String directoryPath, String filename);
- File(File dir , String filename); //dir是一個目錄
幾個常用方法:
文件屬性
- public String getName() //獲取文件名
- public boolean canRead() //是否可讀
- public boolean exists() //是否存在
- public String getAbsolutePath() //獲取文件絕對路徑
- 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"); //文件需事先創建System.out.println(f.getName() + "是可讀的嗎:" + f.canRead());System.out.println(f.getName() + "的長度:" + f.length());System.out.println(f.getName() + "的絕對路徑:" + f.getAbsolutePath());File file = new File("new.txt");System.out.println("在當前目錄下創建新文件" + file.getName());if (!file.exists()) {try {file.createNewFile();System.out.println("創建成功");} catch (IOException exp) {}}} } hello.txt是可讀的嗎:true hello.txt的長度:24 hello.txt的絕對路徑:E:\hello.txt 在當前目錄下創建新文件new.txt創建目錄
- public boolean mkdir();
列出目錄中的文件
-  public String[] list(); // 
-  public File [] listFiles(); // 
-  public String[]?list(FilenameFilter obj) //以字符串形式返回目錄下指定類型的所有文件 
-  public File []?listFiles(FilenameFilter obj) //用File對象形式返回 FilenameFilter是一個接口,該接口有一個方法: 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("."); //參數點號表示當前目錄FileAccept fileAccept = new FileAccept();fileAccept.setExtendName("txt");String fileName[] = dirFile.list(fileAccept);//以字符串形式返回,參數為接口回調for(String name:fileName){System.out.println(name);}} } new.txt文件的創建與刪除
先創建對象:File file = new File("E:\",mary.txt);
如果沒有名為mary.txt的文件,調用public bolean createNewFile();創建
file.delete();刪除文件
運行可執行文件
可以用Runtime類,此時將使用靜態方法創建對象:
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);}} }文件字節輸入流
四個基本步驟:
如果需求簡單,可使用InputStream的子類FileInputStream。
構造方法
FileInputStream(String name);
FileInputStream(File file);
參數name和file指定的文件就稱為輸入流的源。通常要配合try catch使用
關閉流
close();
InStream.java
package com.InOut; import java.io.*;//使用文件字節流讀文件的內容 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. 你好,這是我的世界。文件字節輸出流
同文件字節輸入流類似
文件字符輸入、輸出流
上面的文件字節輸入流、輸出流的read write方法不能很好操作Unicode字符,例如漢字可能會出現亂碼現象。
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"); //不必事先創建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流的目的地,則將具有更強的讀寫能力,例如按行讀取等。
核心語句:String strLine = inTwo.readLine();
//inTwo是一個BufferedReader對象,但是參數里是文件輸入流的源。
隨機流
RandomAccessFile類,既不是InputStream,也不是OutStream的子類。
由RandomAccessFile類創建的流稱為隨機流,既可作為流的源,也可作為流的目的地。
構造方法
RandomAccessFile(String name, String mod)
RandomAccessFile(File file, String mod) //參數mod為 r 或 rw
方法
seek(long a) 參數a確定讀寫位置距離文件開頭的字節個數
getFilePointer() 獲取流的當前讀寫位置
read Char( )
writeFloat( )
數組流
- 字節數組流
- 字符數組流
數據流
對象流
其他功能
進度條
ProgressMonitorInputStream(Component c, String s, InputStream); //import java.swing.**文件鎖
FileChannel channel = input.getChannel(); //獲得信道 FileLock lock = channel.tryLock(); //加鎖 lock.release(); //解鎖“做程序員,圈子和學習最重要”因為有有了圈子可以讓你少走彎路,擴寬人脈,擴展思路,學習他人的一些經驗及學習方法!同時在這分享一下是一直以來整理的Java后端進階筆記文檔和學習資料免費分享給大家!需要資料的朋友私信我扣【06】
 ?
總結
以上是生活随笔為你收集整理的Java中的输入、输出流的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 超宽带 DWM1000模块 电气规格
- 下一篇: LibreCAD的基本使用
