Java – Convert File to Path

This article shows how to convert a File to a Path in Java. java.io.File => java.nio.file.Path // Convert File to Path File file = new File("/home/mkyong/test/file.txt"); Path path = file.toPath(); java.nio.file.Path => java.io.File // Convert Path to File Path path = Paths.get("/home/mkyong/test/file.txt"); File file = path.toFile(); 1. Convert File to Path In Java, we can …

Read more

Java – Add new line in String

Different operating system has a different new line or line separator string: UNIX, Linux or Mac OSX = \n Windows = \r\n NewLineExample.java package com.mkyong; public class NewLineExample { public static void main(String[] args) { String original = "Hello World Java"; System.out.println(original); // add new line String originalNewLine = "Hello\nWorld\nJava"; System.out.println(originalNewLine); } } Output Hello …

Read more

Java – Convert File to String

In Java, we have many ways to convert a File to a String. A text file for testing later. c:\\projects\\app.log A B C D E 1. Java 11 – Files.readString A new method Files.readString is added in java.nio.file.Files, it makes reading a string from File much easier. FileToString1.java package com.mkyong; import java.io.IOException; import java.nio.file.Files; import …

Read more

Java – How to save a String to a File

In Java, there are many ways to write a String to a File. 1. Java 11 – Files.writeString Finally, a new method added in java.nio to save a String into a File easily. StringToFileJava11.java package com.mkyong; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class StringToFileJava11 { public static void main(String[] args) …

Read more

Java Fork/Join Framework examples

What is Fork/Join? Read this Java Fork/Join paper by Doug Lea The fork/join framework is available since Java 7, to make it easier to write parallel programs. We can implement the fork/join framework by extending either RecursiveTask or RecursiveAction 1. Fork/Join – RecursiveTask A fork join example to sum all the numbers from a range. …

Read more

Java try-with-resources example

This article shows you how to use try-with-resources in Java. Table of contents: 1 Java try-with-resources before and after 2. try-with-resources with single resource 3. try-with-resources with multiple resources 3.1 JDBC example. 4. Custom resource with AutoCloseable interface 5. Resource open and closing order 6. Java 9 – final or effectively final variables Download Source …

Read more

How to convert InputStream to File in Java

Below are some Java examples of converting InputStream to a File. Plain Java – FileOutputStream Apache Commons IO – FileUtils.copyInputStreamToFile Java 7 – Files.copy Java 9 – InputStream.transferTo 1. Plain Java – FileOutputStream This example downloads the google.com HTML page and returns it as an InputStream. And we use FileOutputStream to copy the InputStream into …

Read more