Useful File java.io Mini API

useful-file-java-io-mini-api

Java io is a very extensive library. There are numerous ways and combinations to write/read data on a file. This article summaries some very useful classes in java.io package.

Here is a table from OCP Java SE 8 Programmer II Exam Guide (Exam 1Z0-809) that summarizes the basic classes in file io.

java.io Class Extends From Key Constructor(s) Key Methods
File Object File, String
String
String, String
createNewFile()
delete()
exists()
isDirectory()
isFile()
list()
mkdir()
renameTo()
FileWriter Writer File
String
close()
flush()
write()
BufferedWriter Writer Writer close()
flush()
newLine()
write()
PrintWriter Writer File
String
OuputStream
Writer
close()
flush()
format()
printf()
print()
println()
write()
FileOutputStream OutputStream File
String
close()
write()
FileReader Reader File
String
read()
BufferedReader Reader Reader read()
readLine()
FileInputStream InputStream File
String
read()
close()

Examples

import java.io.*;

class Writer1 {
	public static void main(String... args) {
		try {
			boolean newFile = false;
			File file = new File("fileWrite1.txt");
			System.out.println(file.exists());
			newFile = file.createNewFile();
			System.out.println(newFile);
			System.out.println(file.exists());
		} catch(IOException e) {}

	}
}

class Writer2 {
	public static void main(String... args) {
		char[] in = new char[50];
		int size = 0;
		try {
			File file = new File("fileWrite2.txt");
			FileWriter fw = new FileWriter(file);
			fw.write("howdy\nfolks\n");
			fw.flush();
			fw.close();
			FileReader fr = new FileReader(file);
			size = fr.read(in);
			System.out.print(size + " ");
			for(char c: in) {
				System.out.print(c);
			}
			fr.close();
		} catch(IOException e) { }
	}
}

class Writer3 {
	public static void main(String... args) {
		byte[] in = new byte[50];
		int size = 0;
		File file = new File("fileWrite3.txt");
		try(FileOutputStream outStream = new FileOutputStream(file);
			FileInputStream inStream = new FileInputStream(file)) {
			String s = "howdy\nfolks\n";
			outStream.write(s.getBytes());
            outStream.flush();

			size = inStream.read(in);
			System.out.print(size + " ");
			for(byte b: in) {
				System.out.print((char) b);
			}
		} catch(IOException e) {}
	}
}

class Writer4 {
    public static void main(String... args) {
        File file = new File("fileWrite4.txt");
        try(PrintWriter pw = new PrintWriter(file);
            BufferedReader br = new BufferedReader(new FileReader(file))) {
            pw.println("howdy");
            pw.println("folks");
            pw.flush();
            
            String line = null;
            while(true) {
                line = br.readLine();
                if (line == null) break;
                System.out.println(line);
            }

        } catch(IOException e) {}    
    }
}

Comments

Popular posts from this blog

Distributed Transaction Management with REST and Try Confirm/Cancel Pattern

Creating a Library Project for Spring Boot

Exception Handling in WebMethods