Tuesday, June 24, 2008

Java One Liner - Reading a Text File at Once

Java is not meant or known for any one-liners, but who said we can't try? There you go:
String contents = new Scanner(new File("filepath")).findWithinHorizon(Pattern.compile(".*", Pattern.DOTALL), 0);

Cool, eh?

Hmm, may be not. Of course, you still have to deal with the checked exceptions + 3 import statements, etc. The so called one-liner by me has 113 characters (could be more or could be less if you consider the file path's length). I'm not sure if I would consider anything more than 80 chars as "one-liner" but hey, this is Java and this is as close as you can get.

Just for fun. Do you have anything better than this in Java? How about in Java 1.4? Now, don't write a separate method, call it, and say it's a one-liner for Perl sakes!

8 comments:

Anonymous said...

String file = new Scanner( new File( "test.txt" ) ).useDelimiter( "\\Z" ).next() ;

Is a (probably) neater alternative...

Srikanth said...

Thanks for that!

I didn't know of \Z before. It is surely a neater alternative.

Chii said...

lol nice

but cant beat perl's

<>

one step ahead said...

very light weight, and efficient

Anonymous said...

If reading a files content in is truly something you are going to need more than once, just write a utility class with a static method. I did this a long, long, time ago and use it all the time. This beats perl by long shot:

Java:
FileUtil.getContents("filename") // short but tells you what it does

Perl:
<> # yea I know what it does, but really, do you need a non-intuitive built-in operator? And besides this only works for reading from the command line

Srikanth said...

I thought somebody would say that and you did!

Yes, I will never write this in an actual program. Something like FileUtils.getContents("filepath") is always the best way to do this. Just wanted have fun. :)

And for clean scripting, I always prefer Python over Perl. For dirty throw-away code, Perl is probably a better choice. And of course, this is my preference which you may not agree.

Anonymous said...

umm, nevermind the 80 character limit - for me a one-liner can be as long as it likes, as long as it does what it's supposed to and i can be bothered to type it.

the problem with yours is that it doesn't do anything. not without ancillary code, and, worse yet, not without compiling the freaking thing first.

why are you worried about one-liners in java? it's a programming language, not a commandline tool to get something done. you want your java to be clear and concise and easy to follow for you and your brethren, and horribly longwinded and explicit and redundant and mind your getters and your setters for, as it were, perl's sake.

look mommy i used a 5 tonne front loader to plant this flower instead of this hand-held garden trowel.

not really brag-worthy? especially since the flower didn't actually end up getting planted after all.

but other than that, yeah, it's some java code, and it's all on one line. so well done. :)

Michal Stefanow said...

String file = new Scanner( new File( "test.txt" ) ).useDelimiter( "\\Z" ).next() ;

THX! Great one :)

Post a Comment