📅  最后修改于: 2020-09-27 01:57:25             🧑  作者: Mango
Java FileOutputStream是用于将数据写入文件的输出流。
如果必须将原始值写入文件,请使用FileOutputStream类。您可以通过FileOutputStream类编写面向字节和面向字符的数据。但是,对于面向字符的数据,首选使用FileWriter而不是FileOutputStream。
让我们看一下Java.io.FileOutputStream类的声明:
public class FileOutputStream extends OutputStream
Method | Description |
---|---|
protected void finalize() | It is used to clean up the connection with the file output stream. |
void write(byte[] ary) | It is used to write ary.length bytes from the byte array to the file output stream. |
void write(byte[] ary, int off, int len) | It is used to write len bytes from the byte array starting at offset off to the file output stream. |
void write(int b) | It is used to write the specified byte to the file output stream. |
FileChannel getChannel() | It is used to return the file channel object associated with the file output stream. |
FileDescriptor getFD() | It is used to return the file descriptor associated with the stream. |
void close() | It is used to closes the file output stream. |
import java.io.FileOutputStream;
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
fout.write(65);
fout.close();
System.out.println("success...");
}catch(Exception e){System.out.println(e);}
}
}
输出:
Success...
文本文件testout.txt的内容由数据A设置。
testout.txt
A
import java.io.FileOutputStream;
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
String s="Welcome to javaTpoint.";
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("success...");
}catch(Exception e){System.out.println(e);}
}
}
输出:
Success...
文本文件testout.txt的内容与数据Welcome to javaTpoint一起设置。
testout.txt
Welcome to javaTpoint.