load contents of file into string java
Java
package test;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* Java Program to demonstrate different ways to loop over collection in
* pre Java 8 and Java 8 world using Stream's forEach method.
* @author Javin Paul
*/
public class FileToStringJava8 {
public static void main(String args[]) throws IOException {
// How to read file into String before Java 7
InputStream is = new FileInputStream("manifest.mf");
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
String line = buf.readLine();
StringBuilder sb = new StringBuilder();
while(line != null){
sb.append(line).append("\n");
line = buf.readLine();
}
String fileAsString = sb.toString();
System.out.println("Contents (before Java 7) : " + fileAsString);
// Reading file into Stirng in one line in JDK 7
String contents = new String(Files.readAllBytes(Paths.get("manifest.mf")));
System.out.println("Contents (Java 7) : " + contents);
// Reading file into String using proper character encoding
String fileString = new String(Files.readAllBytes(Paths.get("manifest.mf")), StandardCharsets.UTF_8);
System.out.println("Contents (Java 7 with character encoding ) : " + fileString);
// It's even easier in Java 8
Files.lines(Paths.get("manifest.mf"), StandardCharsets.UTF_8).forEach(System.out::println);
}
}
//Using Java 8
public class youPick {
public static void main(String[] args)
{
String filePath = "C:/Users/.."; //insert file path
System.out.println(readFileLineByLine(filePath));
}
private static String channelEventGatewayRequest(String filePath)
{
StringBuilder contentBuilder = new StringBuilder();
try (Stream<String> stream = Files.lines( Paths.get(filePath), StandardCharsets.UTF_8))
{
stream.forEach(s -> contentBuilder.append(s).append("\n"));
}
catch (IOException e)
{
e.printStackTrace();
}
return contentBuilder.toString();
}
Also in Java:
- Title
- init admob
- Category
- Java
- Title
- java create window
- Category
- Java
- Title
- LocalRegistry java rebind() java8
- Category
- Java
- Title
- find first non repeating character in a string
- Category
- Java
- Title
- remove unused imports intellij
- Category
- Java
- Title
- java swing change label icon
- Category
- Java
- Title
- convert string into unicode java
- Category
- Java
- Title
- java 8 findany on null list
- Category
- Java
- Title
- how to get a user to add to an arraylist in java
- Category
- Java
- Title
- insertion sort doubly linked list java
- Category
- Java
- Title
- how to make an arraylist java
- Category
- Java
- Title
- logger in java
- Category
- Java
- Title
- remove last character from string java
- Category
- Java
- Title
- spigot kill entity
- Category
- Java
- Title
- math minimum java
- Category
- Java
- Title
- bubble sort in java
- Category
- Java
- Title
- final variables in java
- Category
- Java
- Title
- spigot respawn player location
- Category
- Java
- Title
- how to write a java for loop?
- Category
- Java
- Title
- java for in loop
- Category
- Java
- Title
- java get size of array
- Category
- Java
- Title
- editer un label java
- Category
- Java
- Title
- java create map
- Category
- Java
- Title
- find duplicate elements in array in java
- Category
- Java
- Title
- can we overload a static method in java
- Category
- Java
- Title
- getindexrequest elasticsearch
- Category
- Java
- Title
- selection sort in java
- Category
- Java
- Title
- java variable declaration
- Category
- Java
- Title
- java 8 find in list by property
- Category
- Java
- Title
- java type casting
- Category
- Java
- Title
- java how to make a gui
- Category
- Java
- Title
- string to char in java
- Category
- Java
- Title
- java clone method
- Category
- Java
- Title
- java linked list functions
- Category
- Java
- Title
- java string format with placeholder
- Category
- Java
- Title
- jtable font size
- Category
- Java
- Title
- pascal's triangle java 2d array
- Category
- Java
- Title
- java add a list to a list
- Category
- Java
- Title
- how to add all list elements at once in java
- Category
- Java
- Title
- java switch case
- Category
- Java
- Title
- how to separate no and text in java
- Category
- Java
- Title
- create jdbc connection in java
- Category
- Java
- Title
- java round double to 2 decimal places
- Category
- Java
- Title
- two pointer approach java
- Category
- Java
- Title
- java infinitew recursion
- Category
- Java
- Title
- scanner check if int
- Category
- Java
- Title
- simple javascript to detect browser using java utils
- Category
- Java
- Title
- convert int array to integer list java
- Category
- Java
- Title
- how to write a perfect shuffle method in java
- Category
- Java
- Title
- loop and save letters in a string java
- Category
- Java
- Title
- java android development find element by id
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- how to make stage dragable in javafx
- Category
- Java
- Title
- how to initialize main in java
- Category
- Java
- Title
- how to make a copy of an array java
- Category
- Java
- Title
- java how to program
- Category
- Java
- Title
- convert string to mayus java
- Category
- Java
- Title
- javafx polygon
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- java gateway process exited before sending its port number pyspark anaconda
- Category
- Java
- Title
- java main method
- Category
- Java
- Title
- sort elements with sortedset
- Category
- Java
- Title
- how to create a Scanner Object in java
- Category
- Java
- Title
- deserialize date java
- Category
- Java
- Title
- jbutton close jframe java
- Category
- Java
- Title
- java interface
- Category
- Java
- Title
- how to change actionbar color in android programmatically
- Category
- Java
- Title
- how to get witdth of window android
- Category
- Java
- Title
- how to remove all whitespace from string java
- Category
- Java
- Title
- log4j print stacktrace
- Category
- Java
- Title
- fragment manager in android
- Category
- Java
- Title
- programmation android avoir acces à la liste des intents de partage
- Category
- Java
- Title
- copy file with byte java
- Category
- Java
- Title
- java create arraly list
- Category
- Java
- Title
- string replace last character java
- Category
- Java
- Title
- how to make a loop in java
- Category
- Java
- Title
- java stream order by property
- Category
- Java
- Title
- how to create an action listener in java
- Category
- Java
- Title
- java code to get all leaf nodes of a xml
- Category
- Java
- Title
- java list as parameter
- Category
- Java
- Title
- coding with WDSL spring
- Category
- Java
- Title
- getcolor deprecated
- Category
- Java
- Title
- java check if variable is set
- Category
- Java
- Title
- abs in java
- Category
- Java
- Title
- java how to print a string[]
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- import classes from another project java
- Category
- Java
- Title
- not equal to java
- Category
- Java
- Title
- close scanner java
- Category
- Java
- Title
- 2d array length in java
- Category
- Java
- Title
- replace regex group() java
- Category
- Java
- Title
- nikita kaksharov
- Category
- Java
- Title
- finding min and max from given number in java
- Category
- Java
- Title
- create java windows application
- Category
- Java
- Title
- java list contains object with property
- Category
- Java
- Title
- java how to read a text file
- Category
- Java
- Title
- java convert char to int
- Category
- Java
- Title
- parse string to int java
- Category
- Java
- Title
- Java.awt graphics tutorial
- Category
- Java
- Title
- import java.util.*;
- Category
- Java