Java編程思想--I/O系統__演算法

來源:互聯網
上載者:User

1,File類

既能代表一個特定檔案的名稱,又能代表一個目錄下的一組檔案的名稱。如果它指向的是一個檔案集,可以對詞集合調用list()方法,返回一個字元數組。

例1

package FileTest;import java.io.File;public class DirList {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFile filedir=new File("/home/hadoop/Data/luceneData");File[] textFiles=filedir.listFiles();for(File file :textFiles){System.out.println(file.getAbsolutePath());System.out.println(file.getName());}}}
結果

/home/hadoop/Data/luceneData/txt1.txttxt1.txt/home/hadoop/Data/luceneData/txt2.txttxt2.txt/home/hadoop/Data/luceneData/txt5.txttxt5.txt/home/hadoop/Data/luceneData/txt4.txttxt4.txt/home/hadoop/Data/luceneData/txt3.txttxt3.txt
輸出檔案的絕對路徑和檔案名稱

2,輸入和輸出

緩衝輸入檔案

package InOutPut;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOError;import java.io.IOException;public class BufferedInputFile {/** * @param args */public static String read(String filename) throws IOException{BufferedReader in=new BufferedReader(new FileReader(filename));String s;StringBuilder sb =new StringBuilder();while((s=in.readLine())!=null){sb.append(s+"\n");}in.close();return sb.toString();}public static void main(String[] args) throws IOException {// TODO Auto-generated method stubSystem.out.println(read("./src/FileTest/DirList.java"));}}

結果

package FileTest;import java.io.File;import java.io.IOException;public class DirList {/** * @param args * @throws IOException  */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubFile filedir=new File("/home/hadoop/Data/luceneData");File[] textFiles=filedir.listFiles();for(File file :textFiles){System.out.println(file.getAbsolutePath());System.out.println(file.getCanonicalPath());System.out.println(file.getName());}}}

從記憶體輸入

package InOutPut;import java.io.IOException;import java.io.StringReader;public class MemoryInput {/** * @param args * @throws IOException  */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubStringReader inReader=new StringReader(BufferedInputFile.read("./src/FileTest/DirList.java"));int c;while((c=inReader.read())!=-1){System.out.println((char)c);}System.out.println(BufferedInputFile.read("./src/FileTest/DirList.java"));}}

結果

package InOutPut;import java.io.IOException;import java.io.StringReader;public class MemoryInput {/** * @param args * @throws IOException  */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubStringReader inReader=new StringReader(BufferedInputFile.read("./src/FileTest/DirList.java"));int c;while((c=inReader.read())!=-1){System.out.println((char)c);}System.out.println(BufferedInputFile.read("./src/FileTest/DirList.java"));}}

基本的檔案輸出

package InOutPut;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.io.PrintWriter;import java.io.StringReader;public class BasicFileOutput {static String file ="Output.out";/** * @param args */public static void main(String[] args) throws IOException{// TODO Auto-generated method stubBufferedReader in= new BufferedReader(new StringReader(BufferedInputFile.read("./src/FileTest/DirList.java")));PrintWriter out=new PrintWriter(file);int lineCount=1;String s;while((s=in.readLine()) !=null){out.println(lineCount++ + ": "+s);}out.close();System.out.println(BufferedInputFile.read(file));//從記憶體中讀取System.out.println(BufferedInputFile.read("./Output.out"));}}

結果

1: package FileTest;2: 3: import java.io.File;4: import java.io.IOException;5: 6: public class DirList {7: 8: /**9:  * @param args10:  * @throws IOException 11:  */12: public static void main(String[] args) throws IOException {13: // TODO Auto-generated method stub14: File filedir=new File("/home/hadoop/Data/luceneData");15: File[] textFiles=filedir.listFiles();16: for(File file :textFiles){17: System.out.println(file.getAbsolutePath());18: System.out.println(file.getCanonicalPath());19: System.out.println(file.getName());20: }21: 22: }23: 24: }1: package FileTest;2: 3: import java.io.File;4: import java.io.IOException;5: 6: public class DirList {7: 8: /**9:  * @param args10:  * @throws IOException 11:  */12: public static void main(String[] args) throws IOException {13: // TODO Auto-generated method stub14: File filedir=new File("/home/hadoop/Data/luceneData");15: File[] textFiles=filedir.listFiles();16: for(File file :textFiles){17: System.out.println(file.getAbsolutePath());18: System.out.println(file.getCanonicalPath());19: System.out.println(file.getName());20: }21: 22: }23: 24: }

3,I/O

從標準輸入中讀取

package IO;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Echo {/** * @param args * @throws IOException  */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubBufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));String s;while((s=stdin.readLine()) != null && s.length() !=0){System.out.println(s);}}}

結果

哈哈哈哈

輸出

package IO;import java.io.PrintWriter;public class ChangeSystemOut {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubPrintWriter out =new PrintWriter(System.out,true);out.println("Hello,world!");}}

結果

Hello,world!



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.