FileOutputStream
- 关系图

- FileOutputStream类中的方法
用法 | 解释 |
---|
void close() | 关闭此文件输出流并释放与此流相关联的任何系统资源。 |
protected void finalize() | 清理与文件的连接,并确保当没有更多的引用此流时,将调用此文件输出流的 close方法。 |
FileChannel getChannel() | 返回与此文件输出流相关联的唯一的FileChannel对象。 |
FileDescriptor getFD() | 返回与此流相关联的文件描述符。 |
void write(byte[] b) | 将 b.length个字节从指定的字节数组写入此文件输出流。 |
void write(byte[] b, int off, int len) | 将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。 |
void write(int b) | 将指定的字节写入此文件输出流。 |
- FileOutputStream用法详解
向文件中写入数据,没有文件就创建文件
@Test
public void Test(){
String filePath="D:/IOFile/news5.txt";
FileOutputStream fileOutputStream=null;
try {
fileOutputStream= new FileOutputStream(filePath,true);
String w="hello,world";
fileOutputStream.write(w.getBytes());
fileOutputStream.write(w.getBytes(),1,5);
fileOutputStream.write('!');
System.out.println("输出数据成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
FileOutputStream与FileInputStream的使用
public class Demo6 {
public static void main(String[] args) {
}
@Test
public void Test(){
String fileInputPath="D:/IOFile/news5.txt";
String fileOutputPath="D:/IOFile/news/news5.txt";
FileOutputStream fileOutputStream=null;
FileInputStream fileInputStream=null;
try {
fileOutputStream = new FileOutputStream(fileOutputPath);
fileInputStream = new FileInputStream(fileInputPath);
byte[] bytes=new byte[128];
int readlen=0;
while((readlen=fileInputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,readlen);
}
System.out.println("文件拷贝正常");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInputStream!=null){
fileInputStream.close();
}
if (fileOutputStream!=null){
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}