Java set output to file

Java set output to file

A file output stream is an output stream for writing data to a File or to a FileDescriptor . Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open. FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter .

Constructor Summary

Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.

Method Summary

Cleans up the connection to the file, and ensures that the close method of this file output stream is called when there are no more references to this stream.

Methods inherited from class java.io.OutputStream

Methods inherited from class java.lang.Object

Constructor Detail

FileOutputStream

public FileOutputStream(String name) throws FileNotFoundException

Creates a file output stream to write to the file with the specified name. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with name as its argument. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

FileOutputStream

public FileOutputStream(String name, boolean append) throws FileNotFoundException

Creates a file output stream to write to the file with the specified name. If the second argument is true , then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with name as its argument. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

FileOutputStream

public FileOutputStream(File file) throws FileNotFoundException

Creates a file output stream to write to the file represented by the specified File object. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with the path represented by the file argument as its argument. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

Читайте также:  Значение while в питоне

FileOutputStream

public FileOutputStream(File file, boolean append) throws FileNotFoundException

Creates a file output stream to write to the file represented by the specified File object. If the second argument is true , then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with the path represented by the file argument as its argument. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

FileOutputStream

Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system. First, if there is a security manager, its checkWrite method is called with the file descriptor fdObj argument as its argument. If fdObj is null then a NullPointerException is thrown. This constructor does not throw an exception if fdObj is invalid . However, if the methods are invoked on the resulting stream to attempt I/O on the stream, an IOException is thrown.

Источник

Java set output to file

A file output stream is an output stream for writing data to a File or to a FileDescriptor . Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open. FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter .

Constructor Summary

Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.

Method Summary

Cleans up the connection to the file, and ensures that the close method of this file output stream is called when there are no more references to this stream.

Methods inherited from class java.io.OutputStream

Methods inherited from class java.lang.Object

Constructor Detail

FileOutputStream

public FileOutputStream(String name) throws FileNotFoundException

Creates a file output stream to write to the file with the specified name. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with name as its argument. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

FileOutputStream

public FileOutputStream(String name, boolean append) throws FileNotFoundException

Creates a file output stream to write to the file with the specified name. If the second argument is true , then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with name as its argument. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

Читайте также:  Python working with time

FileOutputStream

public FileOutputStream(File file) throws FileNotFoundException

Creates a file output stream to write to the file represented by the specified File object. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with the path represented by the file argument as its argument. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

FileOutputStream

public FileOutputStream(File file, boolean append) throws FileNotFoundException

Creates a file output stream to write to the file represented by the specified File object. If the second argument is true , then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with the path represented by the file argument as its argument. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

FileOutputStream

Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system. First, if there is a security manager, its checkWrite method is called with the file descriptor fdObj argument as its argument. If fdObj is null then a NullPointerException is thrown. This constructor does not throw an exception if fdObj is invalid . However, if the methods are invoked on the resulting stream to attempt I/O on the stream, an IOException is thrown.

Источник

Java FileOutputStream

Java FileOutputStream tutorial shows how to use FileOutputStream class to write to files in Java.

Java FileOutputStream

FileOutputStream is an output stream for writing data to a File or to a FileDescriptor . FileOutputStream is a subclass of OutputStream , which accepts output bytes and sends them to some sink. In case of FileOutputStream , the sink is a file object.

Java FileOutputStream constructors

  • FileOutputStream(File file) — creates a file output stream to write to a File object.
  • FileOutputStream(File file, boolean append) — creates a file output stream to write to a File object; allows appending mode.
  • FileOutputStream(FileDescriptor fdObj) — creates a file output stream to write to the specified file descriptor.
  • FileOutputStream(String name) — creates a file output stream to write to the file with the specified name.
  • FileOutputStream(String name, boolean append) — creates a file output stream to write to the file with the specified name; allows appending mode.

Java FileOutputStream close

The FileOutputStream’s close method closes file output stream and releases any system resources associated with this stream. In our examples we use try-with-resources statement, which ensures that each resource is closed at the end of the statement.

Читайте также:  Java string regex not match

Java FileOutputStream write

  • write(byte[] b) — writes array of bytes to the file output stream.
  • write(byte[] b, int off, int len) — writes len bytes from the specified byte array starting at offset off to the file output stream.
  • write(int b) — writes one byte to the file output stream.

Java FileOutputStream example

The following example uses FileOutputStream to write text to a file.

package com.zetcode; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileOutputStreamEx < public static void main(String[] args) throws FileNotFoundException, IOException < String fileName = "/home/janbodnar/tmp/newfile.txt"; try (FileOutputStream fos = new FileOutputStream(fileName)) < String text = "Today is a beautiful day"; byte[] mybytes = text.getBytes(); fos.write(mybytes); >> >

The code example writes one line to a file.

try (FileOutputStream fos = new FileOutputStream(fileName)) 

The FileOutputStream constructor takes a string as a parameter; it is the file name to which we write. We use try-with-resources construct to clean resources after we have finished writing.

String text = "Today is a beautiful day"; byte[] mybytes = text.getBytes();

FileOutputStream write bytes to the file; we get bytes from a string with the getBytes method.

The bytes are written to the file.

$ cat newfile.txt Today is a beautiful day

We show the contents of the file with the cat command.

Java FileOutputStream append to file

With FileOutputStream it is possible to append data to a file. The typical usage for appending is logging.

package com.zetcode; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileOutputStreamAppend < public static void main(String[] args) throws FileNotFoundException, IOException < String fileName = "/home/janbodnar/tmp/newfile.txt"; try (FileOutputStream fos = new FileOutputStream(fileName, true)) < String text = "Today is a beautiful day"; byte[] mybytes = text.getBytes(); fos.write(mybytes); >> >

The code example appends text to file.

try (FileOutputStream fos = new FileOutputStream(fileName, true)) 

The second parameter of FileOutputStream indicates that we will append to the file.

Java FileOutputStream specifying encoding

FileWriter class, which is a Java convenience class for writing character files, has a serious limitation: it uses the default encoding and does not allow us to explicitly specify the encoding. If we have to set the encoding, we can use OutputStreamWriter and FileOutputStream .

package com.zetcode; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; public class FileOutputStreamEncoding < public static void main(String[] args) throws FileNotFoundException, IOException < String fileName = "/home/janbodnar/tmp/newfile.txt"; FileOutputStream fos = new FileOutputStream(fileName); try (OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) < String text = "Сегодня был прекрасный день."; osw.write(text); >> >

The example writes text to a file with OutputStreamWriter . The second parameter is the charset to be used.

$ cat newwfile.txt Сегодня был прекрасный день.

We show the contents of the file.

In this article we have presented the Java FileOutputStream class.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

Источник

Оцените статью