Java Program to Compare Strings In this program, you’ll learn to compare two strings in Java. Example 1: Compare two strings public class CompareStrings { public static void main(String[] args) { String style = “Bold”; String style2 = “Bold”; if(style == style2) System.out.println(“Equal”); else System.out.println(“Not Equal”); } } When you run the program, the output…
Read more
Java Program to Convert InputStream to String In this program, you’ll learn to convert input stream to a string using InputStreamReader in Java. Example: Convert InputStream to String import java.io.*; public class InputStreamString { public static void main(String[] args) throws IOException { InputStream stream = new ByteArrayInputStream(“Hello there!”.getBytes()); StringBuilder sb = new StringBuilder(); String line;…
Read more
Java Program to Convert File to byte array and Vice-Versa In this program, you’ll learn to convert a File object to byte[] and vice-versa in Java. Before we convert a file to byte array and vice-versa, we assume we have a file named test.txt in our src folder. Here’s the content of test.txt This is…
Read more
Java Program to Convert a Stack Trace to a String In this program, you’ll learn to convert a stack trace to a string in Java. Example: Convert stack trace to a string import java.io.PrintWriter; import java.io.StringWriter; public class PrintStackTrace { public static void main(String[] args) { try { int division = 0 / 0; }…
Read more
Java Program to Append Text to an Existing File In this program, you’ll learn different techniques to append text to an existing file in Java. Before we append text to an existing file, we assume we have a file named test.txt in our src folder. Here’s the content of test.txt This is a Test file.…
Read more