Home > Contents > Index >
Utilities.readFile
Reads a specified file and either returns its contents or writes the bytes to the I/O stream.
This method has three variants:
- readFile (Variant 1) reads the specified file and returns its contents. Assumes default encoding.
- readFile (Variant 2) reads the specified file and returns its contents. You can specify a nondefault encoding.
- readFile (Variant 3) reads the specified file and writes its contents to the specified output stream.
Utilities.readFile
Reads the specified file and returns its contents.
Syntax
public static final String readFile(String pathname)Parameters
pathname
- The pathname of the file to read.
Description
The
readFile
method reads the file indicated by the passed file path, and returns its contents. The path is internally converted byosSafeSpec()
. The method assumes that the character of the file are in the default encoding. (The default encoding is set by the Java VM.)Returns
If successful, the method returns the contents of the file. If unsuccessful, the method returns
null
.Throws
IOException
- If the method could not read from
pathname
or write toos
. Common reasons for failure include the following:
- The file specified in
pathname
does not exist.- The file specified in
pathname
exists but the caller does not have permission to read from it.Example
The following example reads and displays the contents of file
D:/java/sonnet.txt
. The code assumes that the contents of this file are in the default encoding:
String text = Utilities.readFile("D:/java/sonnet.txt"); System.out.println(text);See Also
Utilities.readFile
Reads the specified file and returns its contents. Specify the encoding of the input file.
Syntax
public static final String readFile(String pathname, String encoding) throws IOExceptionParameters
pathname
- The pathname of the file to read.
encoding
- Specify the encoding of the contents of the file at
pathname
. Typical encoding values include"UTF-8"
or"Latin-1"
.
Description
The previous variant of
readfile
assumes that the contents ofpathname
are in the default encoding (which is set by the Java VM). By contrast, Variant 2 orreadfile
allows you to specify the encoding.Returns
If successful, the method returns the contents of the file. If unsuccessful, the method returns
null
.Throws
IOException
- If the method could not read from
pathname
or write toos
. Common reasons for failure include the following:
- The file specified in pathname does not exist.
- The file specified in
pathname
exists but the caller does not have permission to read from it.See Also
Utilities.readFile
Reads the file indicated by the passed file path and writes the bytes to the output stream.
Syntax
public static final boolean readFile(String pathname, OutputStream os) throws IOExceptionParameters
pathname
- The pathname of the file to read.
os
- The output stream to which this method should write the contents of the file.
Description
This
readFile
method reads the file indicated by the passedpathname
, and writes its contents to the specified output stream. The path is internally converted byosSafeSpec()
.Returns
Returns
true
if no error.Throws
IOException
- If the method could not read from
pathname
or write toos
. Common reasons for failure include the following:
- The output stream specified in
os
is not writable.- The file specified in
pathname
does not exist.- The file specified in
pathname
exists but the caller does not have permission to read from it.See Also
Home > Contents > Index > ![]()
Oracle JAVA Reference
Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.