Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java read file to string

public String readFile(String filePath) {
    String result = "";
    try {
        result = Files.readString(Paths.get(filePath));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}
Comment

file contents to string java

Files.readLines(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+mFileName), StandardCharsets.UTF_8)
Comment

load contents of file into string 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("
");
            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);
        
    }


}
Comment

PREVIOUS NEXT
Code Example
Java :: mockito verify more than once 
Java :: java standard exceptions 
Java :: How to efficiently find the area of largest rectangle that can be formed by adjacent buildings with known heights, in Java? 
Java :: sc.nextline skips 
Java :: how to use arrays java 
Java :: java random double between 0 and 1 
Java :: deserialize date java 
Java :: java throw an exception 
Java :: java combobox get selected item 
Java :: input long in java 
Java :: jtable font size 
Java :: default parameters in java 
Java :: constraintlayout vs coordinatorlayout 
Java :: calculate number of years months and days between two dates in java 
Java :: spring boot param object 
Java :: How to efficiently reverse a singly linked list, in Java? 
Java :: sorting list in java 
Java :: map in java 
Java :: design custom button in android 
Java :: rainbow six 
Java :: how to compare strings java 
Java :: Create class from string variable JAVA 
Java :: java how to create an array with existing objects 
Java :: iterate list in java 
Java :: actuator spring boot 
Java :: add to list java 
Java :: convert date to localdate java 
Java :: java bigdecimal 
Java :: java map keyset to list 
Java :: initialize arraylist java with size 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =