中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

演示如何使用Java BufferedOutputStream類寫文件

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
下面代碼演示如何使用BufferedOutputStream類寫文件。

使用BufferedOutputStream類寫文件,需要先將字符串轉(zhuǎn)換為字節(jié)數(shù)組,然后再寫入。

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
*
* @author outofmemory.cn
*/
public class Main {

/**
* Prints some data to a file
*/
public void writeToFile(String filename) {

BufferedOutputStream bufferedOutput = null;

try {

//Construct the BufferedOutputStream object
bufferedOutput = new BufferedOutputStream(new FileOutputStream(filename));

//Start writing to the output stream
bufferedOutput.write("Line one".getBytes());
bufferedOutput.write("\n".getBytes()); //new line, you might want to use \r\n if you're on Windows
bufferedOutput.write("Line two".getBytes());
bufferedOutput.write("\n".getBytes());

//prints the character that has the decimal value of 65
bufferedOutput.write(65);

} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the BufferedOutputStream
try {
if (bufferedOutput != null) {
bufferedOutput.flush();
bufferedOutput.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().writeToFile("myFile.txt");
}
}


標(biāo)簽: 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Android中實現(xiàn)圖片倒影的代碼

下一篇:獲取Android activity上所有的控件