string
is an alias in C# for System.String
.
So technically, there is no difference. It's like int
vs. System.Int32
.
As far as guidelines, it's generally recommended to use string
any time you're referring to an object.
e.g.
string place = "world";
Likewise, I think it's generally recommended to use String
if you need to refer specifically to the class.
e.g.
string greet = String.Format("Hello {0}!", place);
This is the style that Microsoft tends to use in their examples.
It appears that the guidance in this area may have changed, as StyleCop now enforces the use of the C# specific aliases.
If the reason you're checking is so you can do something like if file_exists: open_it()
, it's safer to use a try
around the attempt to open it. Checking and then opening risks the file being deleted or moved or something between when you check and when you try to open it.
If you're not planning to open the file immediately, you can use os.path.isfile
Return True
if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.
import os.path
os.path.isfile(fname)
if you need to be sure it's a file.
Starting with Python 3.4, the pathlib
module offers an object-oriented approach (backported to pathlib2
in Python 2.7):
from pathlib import Path
my_file = Path("/path/to/file")
if my_file.is_file():
# file exists
To check a directory, do:
if my_file.is_dir():
# directory exists
To check whether a Path
object exists independently of whether is it a file or directory, use exists()
:
if my_file.exists():
# path exists
You can also use resolve(strict=True)
in a try
block:
try:
my_abs_path = my_file.resolve(strict=True)
except FileNotFoundError:
# doesn't exist
else:
# exists
Best Answer
Read all text from a file
Java 11 added the readString() method to read small files as a
String
, preserving line terminators:For versions between Java 7 and 11, here's a compact, robust idiom, wrapped up in a utility method:
Read lines of text from a file
Java 7 added a convenience method to read a file as lines of text, represented as a
List<String>
. This approach is "lossy" because the line separators are stripped from the end of each line.Java 8 added the
Files.lines()
method to produce aStream<String>
. Again, this method is lossy because line separators are stripped. If anIOException
is encountered while reading the file, it is wrapped in anUncheckedIOException
, sinceStream
doesn't accept lambdas that throw checked exceptions.This
Stream
does need aclose()
call; this is poorly documented on the API, and I suspect many people don't even noticeStream
has aclose()
method. Be sure to use an ARM-block as shown.If you are working with a source other than a file, you can use the
lines()
method inBufferedReader
instead.Memory utilization
The first method, that preserves line breaks, can temporarily require memory several times the size of the file, because for a short time the raw file contents (a byte array), and the decoded characters (each of which is 16 bits even if encoded as 8 bits in the file) reside in memory at once. It is safest to apply to files that you know to be small relative to the available memory.
The second method, reading lines, is usually more memory efficient, because the input byte buffer for decoding doesn't need to contain the entire file. However, it's still not suitable for files that are very large relative to available memory.
For reading large files, you need a different design for your program, one that reads a chunk of text from a stream, processes it, and then moves on to the next, reusing the same fixed-sized memory block. Here, "large" depends on the computer specs. Nowadays, this threshold might be many gigabytes of RAM. The third method, using a
Stream<String>
is one way to do this, if your input "records" happen to be individual lines. (Using thereadLine()
method ofBufferedReader
is the procedural equivalent to this approach.)Character encoding
One thing that is missing from the sample in the original post is the character encoding. There are some special cases where the platform default is what you want, but they are rare, and you should be able justify your choice.
The
StandardCharsets
class defines some constants for the encodings required of all Java runtimes:The platform default is available from the
Charset
class itself:Note: This answer largely replaces my Java 6 version. The utility of Java 7 safely simplifies the code, and the old answer, which used a mapped byte buffer, prevented the file that was read from being deleted until the mapped buffer was garbage collected. You can view the old version via the "edited" link on this answer.