python输入输出流详解_输入输出流的概念
Java中的文件復制相較Python而言,涉及到輸入輸出流的概念,實現(xiàn)中會調(diào)用很多對象,復雜很多,在此以文件復制進行簡單總結。
這里是一個簡單的處理代碼:
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException{
// 輸入文本定位
BufferedInputStream in = new BufferedInputStream(
new FileInputStream("in.txt"));
// 輸出文本定位
PrintStream out = new PrintStream(new BufferedOutputStream(
new FileOutputStream("out.txt")));
/* 重定向 */
System.setIn(in); // 由原來從鍵盤作為輸入重定向到文本 'in.txt'
System.setOut(out); // 由原來從鍵盤作為輸入重定向到文本 'out.txt'
System.setErr(out); // 同上
BufferedReader br = new BufferedReader( // Buffer流使進程效率更高
new InputStreamReader(System.in)); // 此處的節(jié)點流 InputStreamReader 處理數(shù)據(jù)
String s;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
in.close();
out.close();
}
}
這里有個需要理解的地方就是輸入輸出重定向的問題:
原來 System.in 所定向的是鍵盤,而 System.out 所定向的是顯示器,由
System.setIn(in);
System.setOut(out);
System.setErr(out);
而將輸入重定向到了文件 in.txt ,將輸出重定向到了 out.txt ,所以在 while 中,System.out.printf() 自然也就將 String 輸出到了文件 out.txt 中。
總結
以上是生活随笔為你收集整理的python输入输出流详解_输入输出流的概念的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 妄想山海钧木在哪里?
- 下一篇: java 线程 连接池_java程序实现