File save in java


















It's an old question but here is a concise, readable, JDK-only solution with properly closed resources:. This answer is almost exactly like selected answer but with two enhancements: it's a method and it closes out the FileOutputStream object:. Personally, I've found Apache's HttpClient to be more than capable of everything I've needed to do with regards to this. Here is a great tutorial on using HttpClient.

There are many elegant and efficient answers here. But the conciseness can make us lose some useful information. In particular, one often does not want to consider a connection error an Exception , and one might want to treat differently some kind of network-related errors - for example, to decide if we should retry the download.

Here's a method that does not throw Exceptions for network errors only for truly exceptional problems, as malformed url or problems writing to the file. What I suggest in such cases is Apache HttpClient along with org. For example:. For such simple cases, FileWriter is a much better option.

BufferedWriter and FileWriter both extend Writer so they have the same methods:. PrintWriter lets us format the text before writing it down. It contains methods we're used to, such as printf , println , etc.

Let's create a PrintWriter :. A better way to work with a PrintWriter is with the try-with-resources syntax:. Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet.

Stop Googling Git commands and actually learn it! PrintWriter , like the StringBuilder provides the append method which allows us to append contents to the end of an existing file. The append method returns the PrintWriter object it was called upon. This second Java file-writing method lets you write an array of bytes to a file in Java binary data , using the FileOutputStream and BufferedOutputStream classes:.

I hope these "Java file write" example methods will help in your own Java file programming needs. By Alvin Alexander. Last updated: November 24, For example, the Java look and feel places the dialog directly over the parent component. If the parent component is in a frame, then the dialog is dependent on that frame. This dialog disappears when the frame is minimized and reappears when the frame is maximized. By default, a file chooser that has not been shown before displays all files in the user's home directory.

You can specify the file chooser's initial directory by using one of JFileChooser 's other constructors, or you can set the directory with the setCurrentDirectory method. The call to showOpenDialog appears in the actionPerformed method of the Open a File button's action listener:.

The show Xxx Dialog methods return an integer that indicates whether the user selected a file. To get the chosen file or directory, if you set up the file chooser to allow directory selections , call the getSelectedFile method on the file chooser.

This method returns an instance of File. The example obtains the name of the file and uses it in the log message. You can call other methods on the File object, such as getPath , isDirectory , or exists to obtain information about the file. You can also call other methods such as delete and rename to change the file in some way. Of course, you might also want to open or save the file by using one of the reader or writer classes provided by the Java platform.

The example program uses the same instance of the JFileChooser class to display a standard save dialog. This time the program calls showSaveDialog :. By using the same file chooser instance to display its open and save dialogs, the program reaps the following benefits:. Finally, the example program has commented-out lines of code that let you change the file selection mode. For example, the following line of code makes the file chooser able to select only directories, and not files:.

If you want to create a file chooser for a task other than opening or saving, or if you want to customize the file chooser, keep reading. This example uses a file chooser that has been customized in several ways.

Like the original example, the user invokes a file chooser with the push of a button. Here is a picture of the file chooser:. As the figure shows, this file chooser has been customized for a special task Attach , provides a user-choosable file filter Just Images , uses a special file view for image files, and has an accessory component that displays a thumbnail sketch of the currently selected image file.

The remainder of this section shows you the code that creates and customizes this file chooser. See the example index for links to all the files required by this example.



0コメント

  • 1000 / 1000