How do I print a file using the default registered application?

The example code below shows you how to print a file using the default registered application’s print command for the corresponding file type. As an example, on Windows notepad.exe is the default application for printing a .txt file.

To print a file using the default registered application we call the java.awt.Desktop.print(File) method. The print() method takes a parameter of File, which is the reference to a file to be printed. The code snippet below, when run on Windows, will open notepad.exe and print the data.txt file.

package org.kodejava.awt;

import java.awt.*;
import java.io.File;
import java.io.IOException;

public class RunningDefaultAppPrint {
    public static void main(String[] args) {
        File file = new File("data.txt");
        try {
            Desktop desktop = Desktop.getDesktop();

            // Prints a file with the native desktop printing facility, 
            // using the associated application's print command.
            desktop.print(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I edit a file using the default registered application?

For editing a file using the default registered or associated application we can do a call to the java.awt.Desktop.edit(File) method. In the code snippet below we will edit a PNG file. Using the edit() method of the Desktop class will open the default registered application for PNG files. For example on Windows there could be the Windows Paint or GIMP on the Linux operating system.

package org.kodejava.awt;

import java.awt.*;
import java.io.File;
import java.io.IOException;

public class RunningDefaultAppEdit {
    public static void main(String[] args) {
        File file = new File("logo.png");
        try {
            // Edit a file using the default program for the file 
            // type. In the example we will launch a default 
            // registered program to edit a PNG image.
            Desktop desktop = Desktop.getDesktop();
            desktop.edit(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I open a file using the default registered application?

If you want to open a file using the default registered or associated application for those files you can use the Desktop.open(File file) method call. In the example below we’ll ask the Desktop class to open a text file.

package org.kodejava.awt;

import java.awt.*;
import java.io.File;
import java.io.IOException;

public class RunningDefaultAppOpen {
    public static void main(String[] args) {
        // A reference to a text file
        File file = new File("data.txt");

        try {
            Desktop desktop = Desktop.getDesktop();

            // Open a file using the default program for the file type.
            // In the example we will launch a default registered
            // program to open a text file. For example on Windows
            // operating system this call might launch a notepad.exe
            // to open the file.
            desktop.open(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I copy file using FileChannel class?

The example below show you how to copy a file using the java.nio.channels.FileChannel class.

package org.kodejava.io;

import java.io.*;
import java.nio.channels.FileChannel;

public class FileCopy {
    public static void main(String[] args) {
        //// Define the source and target file
        File source = new File("D:/Temp/source.txt");
        File target = new File("D:/Temp/target.txt");

        // Create the source and target channel
        try (FileChannel sourceChannel = new FileInputStream(source).getChannel();
             FileChannel targetChannel = new FileOutputStream(target).getChannel()) {

            // Copy data from the source channel into the target channel
            targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

How do I read a file into byte array?

package org.kodejava.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ReadFileIntoByteArray {
    public static void main(String[] args) throws IOException {
        File file = new File("README.md");
        try (InputStream is = new FileInputStream(file)) {
            if (file.length() > Integer.MAX_VALUE) {
                throw new IOException("File is too large.");
            }

            int offset = 0;
            int bytesRead;
            byte[] bytes = new byte[(int) file.length()];
            while (offset < bytes.length
                    && (bytesRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += bytesRead;
            }
        }
    }
}