Um fluxo de saída de arquivo Java é uma classe de fluxo de bytes usada para processar dados binários brutos. Para gravar dados em um arquivo, os dados devem ser convertidos em bytes e salvos no arquivo.
A cópia do código é a seguinte:
pacote com.yiibai.io;
importar java.io.file;
importar java.io.fileOutputStream;
importar java.io.ioException;
classe pública writefileExample {
public static void main (string [] args) {
FileOutputStream fop = null;
Arquivo de arquivo;
String Content = "Este é o conteúdo do texto";
tentar {
arquivo = novo arquivo ("c: /newfile.txt");
fop = new FileOutputStream (arquivo);
// Se o arquivo não existir, crie -o
if (! file.exists ()) {
file.createNewFile ();
}
// Obtenha o conteúdo em bytes
byte [] contentInbytes = content.getBytes ();
fop.write (contentInbytes);
fop.flush ();
fop.close ();
System.out.println ("done");
} catch (ioexception e) {
E.PrintStackTrace ();
} finalmente {
tentar {
if (fop! = null) {
fop.close ();
}
} catch (ioexception e) {
E.PrintStackTrace ();
}
}
}
}
// O JDK7 atualizado, por exemplo, usa o novo método "Try Recurso Shutdown" para processar facilmente arquivos.
pacote com.yiibai.io;
importar java.io.file;
importar java.io.fileOutputStream;
importar java.io.ioException;
classe pública writefileExample {
public static void main (string [] args) {
Arquivo de arquivo = novo arquivo ("c: /newfile.txt");
String Content = "Este é o conteúdo do texto";
Try (FileOutputStream fop = new FileOutputStream (File)) {
// Se o arquivo não existir, crie -o
if (! file.exists ()) {
file.createNewFile ();
}
// Obtenha o conteúdo em bytes
byte [] contentInbytes = content.getBytes ();
fop.write (contentInbytes);
fop.flush ();
fop.close ();
System.out.println ("done");
} catch (ioexception e) {
E.PrintStackTrace ();
}
}
}