java-IO-基本输出输入流
生活随笔
收集整理的這篇文章主要介紹了
java-IO-基本输出输入流
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/ 標(biāo)準(zhǔn)輸入輸出流:直接類名調(diào)用 一經(jīng)創(chuàng)建無法改變public static final PrintStream err “標(biāo)準(zhǔn)”錯(cuò)誤輸出流。 public static final InputStream in “標(biāo)準(zhǔn)”輸入流。 用來讀取鍵盤輸入的數(shù)據(jù)調(diào)用:System.inInputStream is =System.in;Scanner sc=new Scanner(System.in);讀取鍵盤錄入public static final PrintStream out “標(biāo)準(zhǔn)”輸出流。 System.out.println()將數(shù)據(jù)輸在再命令行*/ package IO;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Writer;public class IO {
public static void main(String[] args) throws IOException{/* 例子:輸出指定文件在命令行 項(xiàng)目文件讀取=====字符輸入流命令行====基本的輸出流由于標(biāo)準(zhǔn)輸出流是一個(gè)字節(jié)的輸出流,所以只能輸出字節(jié)或者字節(jié)組,但是我們讀取道得數(shù)據(jù)則是字符串,我們通過GetByte來獲取字節(jié)數(shù)值我們想通過標(biāo)準(zhǔn)輸出流輸出字符串,把標(biāo)準(zhǔn)輸出流轉(zhuǎn)化成一種字符輸出流,OutputStreamWriterOutputStreamWriter(OutputStream out) 創(chuàng)建使用默認(rèn)字符編碼的 OutputStreamWriter。把標(biāo)準(zhǔn)輸入流轉(zhuǎn)化成一種字符輸入流,InputStreamReaderInputStreamReader(InputStream in) 創(chuàng)建一個(gè)使用默認(rèn)字符集的 InputStreamReader。method();method01();method02();*//*例子:讀取鍵盤錄入的數(shù)據(jù),并輸出到a.txt文件當(dāng)中創(chuàng)建字節(jié)輸入流創(chuàng)建字符輸入流*/InputStream is=System.in;FileWriter fw=new FileWriter("a.txt");byte[] bys= new byte[2048];int len;while((len=is.read(bys))!=-1) {fw.write(new String(bys));fw.flush();}is.close();fw.close();}public static void method02() throws FileNotFoundException, IOException {BufferedReader br=new BufferedReader(new FileReader("Systemout.txt"));Writer w=new OutputStreamWriter(System.out);BufferedWriter bw=new BufferedWriter(w);String line;while((line = br.readLine())!=null) {bw.write(line);bw.newLine();}w.close();br.close();
}
public static void method01() throws FileNotFoundException, IOException {// 創(chuàng)建輸入流對(duì)象BufferedReader br=new BufferedReader(new FileReader("Systemout.txt"));Writer w=new OutputStreamWriter(System.out);String line;while((line = br.readLine())!=null) {w.write(line);w.write("\r\n");}w.close();br.close();
}public static void method() throws FileNotFoundException, IOException {// 創(chuàng)建輸入流對(duì)象BufferedReader br=new BufferedReader(new FileReader("Systemout.txt"));// 創(chuàng)建輸出基本流OutputStream os=System.out;String line;while((line=br.readLine())!=null) {os.write(line.getBytes());}os.close();br.close();
}
}
總結(jié):IO中的File是文件操作 ?字符緩沖類BufferWriter ?BufferReader ? 基本輸入輸出流 System.in ?System.out ?轉(zhuǎn)化流OutputStreamWriter ?InputStreamReader
基本輸入輸出流只能帶入數(shù)組參數(shù) ?這時(shí)候需要把字節(jié)的輸入輸出流轉(zhuǎn)化為字符類的輸入輸出流 ?這時(shí)候就有可轉(zhuǎn)化流 ?將基本的輸入輸出流轉(zhuǎn)換為字符高效緩沖流BufferWriter ?BufferReader
轉(zhuǎn)載于:https://www.cnblogs.com/bai-boy/p/10394315.html
總結(jié)
以上是生活随笔為你收集整理的java-IO-基本输出输入流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【思维题 状压dp】APC001F -
- 下一篇: python之time模块和hashli