Archive for the ‘Java 6’ tag
Java File IO & NIO2
Java 6, or the java.io.*
package which has been around a long time has known limits. There are some noted issues with the Java 6 java.io.*
package, and why I wrote the FileIO.java
file back in 2002. The FileIO.java
class creates a series of static methods that let you read, write, copy, and move files both locally and across the network.
- Many methods fail to throw exceptions
- A number of simple methods are missing, like the copy and move methods
- There isn’t any support for symbolic links
- Many methods fail to scale with large files
The Java 7 introduces the java.nio.file.*
package, and it does the following:
- Works more consistently across platforms
- Provides improved access to more file attributes
- Provides improved exception handling
- Supports non-native file systems
A student asked me to post the FileIO.java
file. So, I posted it on the github.com
site. I hope it helps simplifies the use of File IO operations. Admittedly, it’s easier to read and write a file with Java 7. The following provides a complete example class:
import java.awt.Dimension; import java.awt.Font; import java.io.*; import java.nio.file.*; import java.nio.charset.Charset; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class ReadWriteFile { // Constructor for objects of class ReadFile public ReadWriteFile() {} // Write a file. public static void writeFile(String fileName, String fileText) { // Declare a file path. Path p = Paths.get(fileName); // Write a file. try { BufferedWriter bw = Files.newBufferedWriter( p, Charset.forName("ISO-8859-1") , StandardOpenOption.CREATE, StandardOpenOption.APPEND ); bw.write(fileText, 0, fileText.length()); bw.close(); } catch(IOException ioe) { System.out.println("Error writing a file."); System.exit(0); }} // Read a file. public static String readFile(String fileName) { // Declare a String to hold the file. String text = "", line = ""; // Declare a file path. Path p = Paths.get(fileName); // Write a file. try { // Read the file. BufferedReader fileInput = Files.newBufferedReader(p,Charset.forName("ISO-8859-1")); // Read and process first line. line = fileInput.readLine(); text = line + "\n"; // Read and process second thru last lines. while (line != null) { line = fileInput.readLine(); if (line != null) text += line + "\n"; } // Close the file input. fileInput.close(); } catch(EOFException eofe) { System.out.println("No more lines to read."); System.exit(0); } catch(IOException ioe) { System.out.println("Error reading file."); System.exit(0); } // Return a String value. return text; } // Declare a static main to read a text file. public static void main(String[] args) { // Declare local object types. JOptionPane op = new JOptionPane(); JScrollPane sp; JTextArea ta; Object[] options = {}; // Instantiate a test class. ReadWriteFile rwf = new ReadWriteFile(); // Text the readFile method. if (args.length > 0) { // Set the value and formatting of the text area. ta = new JTextArea(rwf.readFile(args[0])); ta.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14)); ta.setLineWrap(true); ta.setWrapStyleWord(true); // Assign the text area to a scroll pane. sp = new JScrollPane(ta); sp.setPreferredSize(new Dimension(400,200)); // Invoke a generic option pane. op.showOptionDialog(null, sp, "File Content", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, null); } // Test the writeFile method. if (args.length > 1) { rwf.writeFile(args[1],rwf.readFile(args[0])); } } } |
You can call it like this to read and write an output.txt file from any directory:
java ReadWriteFile Fellowship.txt output.txt |
It will display a JOptionDialog
like this:
Any suggestions or improvements are welcome.