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 file as string java

public static String loadFileAsString(String path) {
  InputStream stream = YourClassName.class.getResourceAsStream(path);
  try {
    String result = new String(stream.readAllBytes());
    return result;
  } catch (IOException e) {
    e.printStackTrace();
    return null;
  }
}
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 :: circular imageview android 
Java :: how to create a button with javascri[t 
Java :: bukkit delayed task 
Java :: junit 5 dependency maven 
Java :: android java close app 
Java :: react-native force rtl on android 
Java :: textview set drawable right programmatically 
Java :: default code of java 
Java :: Java how to copy file 
Java :: remove double quote java 
Java :: java for loop high to low 
Java :: java selenium new window 
Java :: java betrag 
Java :: spigot run task later 
Java :: string to int java 
Java :: how to print a matrix in java 
Java :: java bufferedimage get raster data 
Java :: spring load config value in variable 
Java :: moving text in textview android 
Java :: jsoup android 
Java :: arraylist with values java 
Java :: java calculate elapsedTime 
Java :: check if map contains key java 
Java :: how to print elements in matrix java 
Java :: since when can java command compile 
Java :: How to efficiently count the number of smaller elements to the right of every array element, in Java? 
Java :: make recycler view non scrollable 
Java :: when does lockdown end 
Java :: set vs list java 
Java :: get last element of array java 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =