package com.hspedu.io_; import org.junit.Test; import java.io.FileOutputStream; import java.io.IOException; public class TestFileOutputStream { @Test public void writeFile() { String filePath = "e:\\JavaIO\\FileOutputStream\\test.txt"; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(filePath); // fileOutputStream.write();方法写入文件 String string = "hello, world"; // fileOutputStream.write(string.getBytes()); fileOutputStream.write(string.getBytes(), 0, string.length()); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }