In certain scenarios such as unit testing, or for some application logics you might need to create temporary files.Creating a temporary fileThe File class in Java provides a method with name createTempFile(). This method accepts two String variables representing the prefix (starting name) and suffix(extension) of the temp file and a File object representing the directory (abstract path) at which you need to create the file.ExampleFollowing Java example creates a temporary file named exampleTempFile5387153267019244721.txt in the path D:/SampleDirectoryimport java.io.File; import java.io.IOException; public class TempararyFiles { public static void main(String args[]) throws IOException { String prefix = ... Read More
Character Streams − These handle data in 16 bit Unicode. Using these you can read and write text data only.The Reader and Writer classes (abstract) are the super classes of all the character stream classes: classes that are used to read/write character streams. Following are the character array stream classes provided by Java −ReaderWriterBufferedReaderBufferedWriterCharacterArrayReaderCharacterArrayWriterStringReaderStringWriterFileReaderFileWriterInputStreamReaderInputStreamWriterFileReaderFileWriterExampleThe following Java program reads data from a particular file using FileReader and writes it to another, using FileWriter.import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class IOStreamsExample { public static void main(String args[]) throws IOException { //Creating FileReader object ... Read More
These handle data in bytes (8 bits) i.e., the byte stream classes read/write data of 8 bits. Using these you can store characters, videos, audios, images etc.The InputStream and OutputStream classes (abstract) are the super classes of all the input/output stream classes: classes that are used to read/write a stream of bytes. Following are the byte array stream classes provided by Java −InputStreamOutputStreamFIleInputStreamFileOutputStreamByteArrayInputStreamByteArrayOutputStreamObjectInputStreamObjectOutputStreamPipedInputStreamPipedOutputStreamFilteredInputStreamFilteredOutputStreamBufferedInputStreamBufferedOutputStreamDataInputStreamDataOutputStreamExampleFollowing Java program reads data from a particular file using FileInputStream and writes it to another, using FileOutputStream.import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class IOStreamsExample { public static void main(String args[]) throws IOException { ... Read More
While you are trying to write data to a Stream using the BufferedWriter object, after invoking the write() method the data will be buffered initially, nothing will be printed.The flush() method is used to push the contents of the buffer to the underlying Stream.ExampleIn the following Java program, we are trying to print a line on the console (Standard Output Stream). Here we are invoking the write() method by passing the required String.import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; public class BufferedWriterExample { public static void main(String args[]) throws IOException { //Instantiating the OutputStreamWriter class ... Read More
The append() method of the StringBuilder class accepts a String value and adds it to the current object.To convert a String value to StringBuilder object −Get the string value.Append the obtained string to the StringBuilder using the append() method.ExampleIn the following Java program, we are converting an array of Strings to a single StringBuilder object. Live Demopublic class StringToStringBuilder { public static void main(String args[]) { String strs[] = {"Arshad", "Althamas", "Johar", "Javed", "Raju", "Krishna" }; StringBuilder sb = new StringBuilder(); sb.append(strs[0]); sb.append(" "+strs[1]); sb.append(" ... Read More
The stripStart() method of the org.apache.commons.lang.StringUtils class accepts two strings and removes the set of characters represented by the second string from the string of the first string.To remove leading zeros from a string using apache communal library −Add the following dependency to your pom.xml file org.apache.commons commons-lang3 3.9 Get the string.Pass the obtained string as first parameter and a string holding 0 as second parameter to the stripStart() method of the StringUtils class.ExampleThe following Java program reads an integer value from the user into a String and removes the leading zeroes from it using the stripStart() ... Read More
Coupling refers to the dependency of one object type on another, if two objects are completely independent of each other and the changes done in one doesn’t affect the other both are said to be loosely coupled.You can achieve loose coupling in Java using interfaces -Example Live Demointerface Animal { void child(); } class Cat implements Animal { public void child() { System.out.println("kitten"); } } class Dog implements Animal { public void child() { System.out.println("puppy"); } } public class LooseCoupling { public static void main(String args[]) { Animal obj = new Cat(); obj.child(); } }Outputkitten
If the super-class method throws certain exception, you can override it without throwing any exception.ExampleIn the following example the sampleMethod() method of the super-class throws FileNotFoundException exception and, the sampleMethod() method does not throw any exception at all. Still this program gets compiled and executed without any errors. Live Demoimport java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super { public void sampleMethod()throws FileNotFoundException { System.out.println("Method of superclass"); } } public class ExceptionsExample extends Super { public void sampleMethod() { System.out.println("Method of Subclass"); } public static void main(String args[]) ... Read More
If the super-class method throws certain exception, the method in the sub-class should not throw its super type.ExampleIn the following example the readFile() method of the super-class throws FileNotFoundException exception and, the readFile() method of the sub-class throws an IOException, which is the super type of the FileNotFoundException. Live Demoimport java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super { public String readFile(String path)throws FileNotFoundException { throw new FileNotFoundException(); } } public class ExceptionsExample extends Super { @Override public String readFile(String path)throws IOException { //method body ...... } }Compile ... Read More
The Collections class of java.util package methods that exclusively work on collections these methods provide various additional operations which involves polymorphic algorithms.This class provides different variants of the synchronizedCollection() method as shown below −Sr.NoMethods & Description1static Collection synchronizedCollection(Collection c)This method accepts any collection object and, returns a synchronized (thread-safe) collection backed by the specified collection.2static List synchronizedList(List list)This method accepts an object of the List interfacereturns a synchronized (thread-safe) list backed by the specified list.3static Map synchronizedMap(Map m)This method accepts an object of the Map interface and, returns a synchronized (thread-safe) map backed by the specified map.4static ... Read More