OutputStream能夠將byte寫入到文件中,可是要注意write有重載方法。 java
write(int b)和write(byte[] b), 效率上有差別。 app
import java.io.FileOutputStream; import java.io.OutputStream; public class WriteBytes2File { public static void main(String args[]) throws Exception{ OutputStream out = new FileOutputStream( "helloworld.txt"); String str = "hello world!"; out.write(str.getBytes()); //write(byte[]) out.close(); writeByByte(); } public static void writeByByte() throws Exception{ OutputStream out = new FileOutputStream( "helloworld1.txt"); String str = "hello world!"; //out.write(str.getBytes()); for(Byte b : str.getBytes()){ out.write(b);//write(int) } out.close(); } public static void append() throws Exception{ OutputStream out = new FileOutputStream( "helloworld1.txt", true); String str = "hello java!"; //out.write(str.getBytes()); for(Byte b : str.getBytes()){ out.write(b); } out.close(); }}