When trying to access an Array value at an undefined index, we will get the ArrayIndexOutOfBoundsException. To prevent this error we should access the values at a defined index only. In that if an Array has 3 elements the defined indexes will be 0,1,2. Any other index say 4 will be undefined. Example: public class […]
Category: Java Programming
Create the new file from large file, using File Channel and ByteBuffer: Example : import java.io.IOException; import java.net.URI; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Date; public class CopyLargeFileExample { public static void main(String args[]) throws IOException { long startTime = new Date().getTime(); // args[0] — Input source file // args[1] […]
Write the file Using ByteBuffer and FileChannel from String: import java.io.IOException; import java.net.URI; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class WriteFileUsingFileChannel { public static void main(String args[]) throws IOException { String content = “Text content”; Path file = Paths.get(URI.create(“file:///D:/TestFile.txt”)); FileChannel fileChannel = FileChannel.open(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE); ByteBuffer buffer = ByteBuffer.allocate(content.length()); buffer.put(content.getBytes()); […]
Read the file Using ByteBuffer and file Size: import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Path; import java.nio.file.Paths; public class ReadFileUsingFileChannel { public static void main(String args[]) throws IOException { String inputFile = “D:\\SampleFile.txt”; //Set the Path with input file Path file = Paths.get(inputFile); //Open the file FileChannel fileChannel = FileChannel.open(file); //Get the Size of […]
JDBC Architecture
Java Architecture is consist of Java Application, JDBC API, DriverManager, Driver and Database. Java Application is our client program which contains JDBC API client logic and our bussiness logic. JDBC API: JDBC ( Java Database Connectivity) API provides the connectivity between Java application and Database. Java application can communicate with any Database with the help […]
Java:OOPs concept – Data hiding
Data hiding: The data hiding provides data integrity and data security and the other class member or method cannot access them directly our class methods if we can to access we can provide the public methods. Providing class data members not access to other class is called Data hiding. In Java, we can achieve data […]