碧藍航線黃金的祕寶活動
阿新 • • 發佈:2021-04-02
此文獻介紹位元組輸出輸入流
位元組輸入流 InputStream 將內容以位元組(byte)的形式輸入,如果要輸入普通文字,假設文字內容為
通過以下程式碼:
public class CeShi {
public static void main(String[] args) {
InputStream in;
try {
in = new FileInputStream(new File(“F:\表.txt”));
byte[] bs = new byte[100];//緩衝區
int count = 0;
while((count = in.read(bs,0,bs.length))!=-1){
String str = new String(bs,0,count);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//文字路徑
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(in != null) {
in.close();
}
} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
}執行程式碼結果為:
位元組輸出流,將普通文字以位元組的形式輸出,程式碼部分相較輸入流較少。
public class CeShi {
public static void main(String[] args) {
OutputStream out = null;
try {
out = new FileOutputStream(new File(“F:\表.txt”));
out.write(new String (“天下大同”).getBytes());
out.flush();
} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if(out != null) { out.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
程式碼執行後: