0

Possible Duplicate:
Java: how to create and write to a file

I am new to java and wanted to know of theres an easy way to save/load a file to/from a desired path. Is there an easy way to do this?

2
  • yes - google, there be lot's of examples there... I mean, here is one which I found in two seconds: exampledepot.com/egs/java.io/WriteToFile.html Commented Jun 6, 2011 at 10:31
  • Quote: "I am new to Java" - c'mon, don't decrease your rep with downvoting. And for the usual "google" advice: you first have to learn the basics before you can use search engines effectively. You have to learn the words that are needed to form the correct questions and find the right answers. Commented Jun 6, 2011 at 11:18

2 Answers 2

1

The simplest library to use it likely to be Apache FileUtils

You can load/save a file as a single line.

for(String line: FileUtils.readLines(filename)) {
   // process the line.
}

It will take care of exception handling and make sure the file is closed.

Note: this tool works best with file less than 100 MB. If you have really large files, you need to read the contents progressively yourself.

Sign up to request clarification or add additional context in comments.

Comments

0

The plain Java runtime does not offer some file copy methods. So if we don't want to use an additional library (like FileUtils, see Peter's answer) or call external programs (like copy.cmd or cp), then we have to read all bytes from a file into memory and write them to a new file.

Here's a nice tutorial that shows exactly how to copy from one file to another (although: that code runs much too slow for practical use because it does not buffer the streams)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.