How to print from a file in java




















Here's the bare minimum:. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Java: print contents of text file to screen Ask Question. Asked 8 years, 9 months ago. Active 2 years, 1 month ago. Viewed k times. I have a text file named foo. Improve this question. Community Bot 1 1 1 silver badge. It sounds like you need to read text from a file.

I can assure you that there is plenty of information on the web regarding reading from files in Java I'm telling you! I can't find this anywhere! And yes, I use Google. This past question has just the details you're looking for: stackoverflow. I find this hard to believe. Words Product Family PDF Product Family Cells Product Family Email Product Family Slides Product Family Imaging Product Family BarCode Product Family Diagram Product Family Tasks Product Family OCR Product Family Note Product Family CAD Product Family GIS Product Family ZIP Product Family Page Product Family PSD Product Family OMR Product Family 8.

This way, you can do more with the result:. Since Java 7, there is such thing as a try-with-resources statement, which automatically closes the reader when an exception occurs, which you are not doing here. Your current code has memory leaks!

Not sure about the best way, but I'd personally favor one of the options using java. And if for some reason you cannot use either of those, you might want to have a look at FileUtils readLines java. File from Apache Commons IO although it's probably not worth adding that dependency only for this task. This way your IDE will tell you where you need to be careful.

A cleaner way of ensuring that your resources get closed is to use try-with-resources. Use a StringBuilder instead. But in this program, you don't need to, since you never even use fulltext. By the way, your fulltext is probably not what you want: it has all of the line terminators stripped out, since that's what BufferedReader. There's nothing really line-oriented about your program, so there is little reason to scan for line breaks.

The simplest way to achieve the same results is using java. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?



0コメント

  • 1000 / 1000