To load a properties file bundled with your application:
import java.awt.Image; import javax.imageio.ImageIO; ... try { Image img = ImageIO.read(new File("~/Desktop/cat.png")); } catch (IOException e) { e.printStackTrace(); }
Write to a file examplefile.txt:
import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; public class CustomFileWriting { public static void main(String[] args) { String customFilePath = "C:\\examplefile.txt"; FileOutputStream customFos = null; try { customFos = new FileOutputStream(customFilePath); byte[] customBuffer = "This is a custom file content.".getBytes(); customFos.write(customBuffer, 0, customBuffer.length); System.out.println("Content has been written to: " + customFilePath); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (customFos != null) customFos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Read from file examplefile.txt:
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class CustomFileReading { public static void main(String[] args) { String customFilePath = "C:\\examplefile.txt"; FileInputStream customFis = null; try { customFis = new FileInputStream(customFilePath); int customLength = (int) new File(customFilePath).length(); byte[] customBuffer = new byte[customLength]; customFis.read(customBuffer, 0, customLength); System.out.println("Content read from " + customFilePath + ":\n" + new String(customBuffer)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (customFis != null) customFis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Note, that since Java 1.7 the try-with-resources statement was introduced what made implementation of reading\writing operation much simpler:
Write to a file examplefile.txt:
import java.io.File; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; public class CustomFileWriting { public static void main(String[] args) { String customFilePath = "C:\\examplefile.txt"; try (FileOutputStream customFos = new FileOutputStream(customFilePath)) { byte[] customBuffer = "This will be written in examplefile.txt".getBytes(); customFos.write(customBuffer, 0, customBuffer.length); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Read from file examplefile.txt:
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class CustomFileReading { public static void main(String[] args) { String customFilePath = "C:\\examplefile.txt"; try (FileInputStream customFis = new FileInputStream(customFilePath)) { int customLength = (int) new File(customFilePath).length(); byte[] customBuffer = new byte[customLength]; customFis.read(customBuffer, 0, customLength); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Java 7 introduced the very useful Files class
Version ≥ Java SE 7 import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.Path; Path path = Paths.get("path/to/file"); try { byte[] data = Files.readAllBytes(path); } catch(IOException e) { e.printStackTrace(); }
We can use Channel to copy file content faster. To do so, we can use transferTo() method of FileChannel .
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class CustomFileCopier { public static void main(String[] args) { File customSourceFile = new File("sourceFile.txt"); File customDestFile = new File("destinationFile.txt"); performCopy(customSourceFile, customDestFile); } public static void performCopy(File sourceFile, File destFile) { if (!sourceFile.exists() || !destFile.exists()) { System.out.println("Source or destination file doesn't exist"); return; } try (FileChannel sourceChannel = new FileInputStream(sourceFile).getChannel(); FileChannel destChannel = new FileOutputStream(destFile).getChannel()) { sourceChannel.transferTo(0, sourceChannel.size(), destChannel); } catch (IOException e) { e.printStackTrace(); } } }
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions