One line "command" to read a file

R

Ramon

Hi

Does Java has a one line instruction to read to a text file, like what
C# has?

I mean, is there something equivalent to this in Java?:
String data = System.IO.File.ReadAllText("path to file");

Regards
 
S

Stefan Ram

Ramon said:
Does Java has a one line instruction to read to a text file

Java is not a line-oriented language like BASIC, Fortran
or Cobol, so you can write anything into a single line.

Java does not have »instructions« or »commands« as far as
I know.

Sometimes, something like the following approach is suggested:

final java.lang.String contents = new java.util.Scanner
( new java.io.File( "path" )).findWithinHorizon
( java.util.regex.Pattern.compile
( ".*", java.util.regex.Pattern.DOTALL ), 0 );
 
A

Arne Vajhøj

Ramon said:
Does Java has a one line instruction to read to a text file, like what
C# has?

I mean, is there something equivalent to this in Java?:
String data = System.IO.File.ReadAllText("path to file");

No. At least I have never seen one.

Arne

PS: I would tend to prefer StreamReader and ReadToEnd over
File ReadAllText in C# - the last one seems to FSO'ish to me.
 
A

Arved Sandstrom

Hi

Does Java has a one line instruction to read to a text file, like what
C# has?

I mean, is there something equivalent to this in Java?:
String data = System.IO.File.ReadAllText("path to file");

Regards

Depends on what you mean by "Java". The Java _language_ has no such
"instruction", but then neither has C#. There is, however, a C# library
that provides a class and method to do that, as you've pointed out.

So if you want such a method, simply write one. :) It would be a static
method, and you'd want it to throw the usual exceptions, like
FileNotFoundException.

AHS
 
R

Roedy Green

Does Java has a one line instruction to read to a text file, like what
C# has?

I mean, is there something equivalent to this in Java?:
String data = System.IO.File.ReadAllText("path to file");

You can write one. See http://mindprod.com/products1.html#HUNKIO
--
Roedy Green Canadian Mind Products
http://mindprod.com
PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
However, he refuses to consider the costs of not implementing Kyoto which the
famous economist Nicholas Stern estimated at 5 to 20% of GDP
 
R

Roland Pibinger

Does Java has a one line instruction to read to a text file, like what
C# has?

I mean, is there something equivalent to this in Java?:
String data = System.IO.File.ReadAllText("path to file");

Here you are:

String data = new Scanner(new FileInputStream("myFile"))
useDelimiter("\\Z").next();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top