Java read file backward




















That avoids the hassle of using forward or backward slashes. You can then get the absolute path by calling the toAbsolutePath method followed by toString.

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. Forward slash or backslash?

Ask Question. Asked 8 years, 2 months ago. Active 5 years, 9 months ago. Viewed 48k times. Improve this question. Patriot Patriot 1 1 gold badge 1 1 silver badge 7 7 bronze badges.

Don't hardcode paths into your program, so it will be installation- independent. Add a comment. Active Oldest Votes. Using forward slashes will make it system independent. I'd stick to that for simplicity. Improve this answer. Paul Draper Paul Draper Here's an alternative, more efficient solution, that also simplifies the reversing logic using a LinkedList , with a descendingIterator :.

In terms of code, the above is, obviously, much more readable and basically uses the NIO classes for efficient management of file resources and Collections API for managing the list itself. Also notice the getResource String can be a better way of loading resources as explained here.

There is also a nice discussion on features of using Collections API over arrays. I suppose Scanner is good if you are expecting different types of input in your file. Considering the fact that the file is called "numbers. Taking all the points in Janos' answer and converting to Java 8 you have:. You still need the intermediate collection, as you cannot reverse a Stream without consuming it all.

Assuming that the array isn't something you have to use, the natural solution to this kind of problem is to use a Stack. A stack is a data structure that is Last-In, First-Out. The idea is that you read the file, one item number, in your case at a time, and 'push' each item on to the stack. Once you've read all of the items, you 'pop' them off the stack to display them.

Integer does inherit from Object , so we use that instead. There's a bit of a rabbit hole there but I won't delve deeper. Other primitive types in Java boolean, double, float, etc have an analogous Object type Boolean, Double, Float, etc. Pop is kind of tricky. It's doing two things: it is returning the object on top of the stack, and it is removing that object from the stack. Since it removes an element from the stack, while!

The downside to this approach is that you no longer have your numbers after the fact, since the pop functionality throws your numbers away. There are still ways to get around this, but that's something you have to keep in mind. 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? Learn more. Reversing the lines of a file Ask Question.

Collectives on Stack Overflow. Learn more. Reverse file java program Ask Question. Asked 9 years, 1 month ago.

Active 5 years, 11 months ago. Viewed 5k times. Thanks, code: command line arguments: source2. File; import java. IOException; import java. PrintWriter; import java. Improve this question. WideshoT0 WideshoT0 1 1 1 silver badge 1 1 bronze badge. Can you please post what it is currently writing to the file?

Thanks Software Monkey! I switched to OutputStream and it worked fine. Add a comment. Active Oldest Votes. What output are you seeing??



0コメント

  • 1000 / 1000