Posts

Showing posts from June, 2020

What is main Method in java ? how to use main method in Java ? can we overload main method ? why main method is static?

What is the main Method in java? how to use the main method in Java ? can we overload the main method? why the main method is static   What is the main Method in java? The main method is the entry point that means it is the starting of every java program. if JVM runs any program for java it first needs is find the main method in java. it is always written as public static void main(String[] args) . public : Public is an access modifier, which is used to define who can access this method that means who can use this method. Public means that this Method will be accessible by any Class. static : Static keyword used for memory management. It is a keyword in java that identifies it is class-based. main() is made static in Java so that it can be accessed without creating the instance of a Class. In case, main is not made static then the compiler will throw an error as main () is called by the JVM before any objects are made and only static met...

How do I create a Java string from the contents of a file? convert file to Java String.

How do I create a Java string from the contents of a file? convert the file to Java String. The following a possible way for getting data from the file as a String form. Read all text from a file First Solution:- import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; String content = new String(Files.readAllBytes(Paths.get("readMe.txt")), StandardCharsets.UTF_8); Second Solution:- Guava has a method similar to the one from Commons IOUtils that Williams Rohr mentioned: import com.google.common.base.Charsets; import com.google.common.io.Files; // ... String text = Files.toString(new File(path), Charsets.UTF_8); Third Solution:- Java 11 added the readString() method to read small files as a String, preserving line terminators: String content = Files.readString(path, StandardCharsets.US_ASCII); For versions between Java 7 and 11, here's a compact, robust the idiom, wrapped up in a u...

How do I read / convert an InputStream into a String in Java? convert String to InputStream.

How do I read/convert an Input Stream into a String in Java? convert String to Input Stream. First, we learn What is InputStream? The  Java   InputStream  class,  java.io.InputStream , represents an ordered stream of bytes. you can read data from a Java  InputStream as an order sequence of bytes. Java stream is a flow of data from a source into a destination. The source or destination can be a disk, memory, socket, or other programs. The data can be bytes, characters, or objects. The following way of Converting String to InputStream. First Solution:- A nice way to do this is using  Apache commons   IOUtils  to copy them InputStream into a StringWriter... something like. StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, encoding); String theString = writer.toString(); or // NB: does not close inputStream, you'll have to use try-with-resources for that String ...

How do I convert a String to an int in Java? convert String to java,Solution java.

First Solution:- String myString = "1234"; int foo = Integer.parseInt(myString); you'll notice the "catch" is that this function can throw a NumberFormatException, which of course you have to handle: int foo; try { foo = Integer.parseInt(myString); } catch (NumberFormatException e) { foo = 0; } (This treatment defaults a malformed number to 0, but you can do something else if you like.) Alternatively, you can use an Ints method from the Guava library, which in combination with Java 8's Optional, makes for a powerful and concise way to convert a string into an int: import com.google.common.primitives.Ints; int foo = Optional.ofNullable(myString) .map(Ints::tryParse) .orElse(0); Second Solution:- Simply you can try this: Use Integer.parseInt(your_string); to convert a String to int Use Double.parseDouble(your_string); to convert a String to double Example String str = "8955"; int q = Integer.parseInt(str); System.out.p...