home
search
help
profile
liking the experience? our app is even better
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now  
showing results for file contnet tto string
1public String readFile(String filePath) {
2    String result = "";
3    try {
4        result = Files.readString(Paths.get(filePath));
5    } catch (IOException e) {
6        e.printStackTrace();
7    }
8    return result;
9}
upvote
downvote
1package test;
2
3import java.io.BufferedReader;
4import java.io.FileInputStream;
5import java.io.IOException;
6import java.io.InputStream;
7import java.io.InputStreamReader;
8import java.nio.charset.StandardCharsets;
9import java.nio.file.Files;
10import java.nio.file.Paths;
11
12
13/**
14 * Java Program to demonstrate different ways to loop over collection in 
15 * pre Java 8 and Java 8 world using Stream's forEach method.
16 * @author Javin Paul
17 */
18public class FileToStringJava8 {
19
20    public static void main(String args[]) throws IOException {
21
22        // How to read file into String before Java 7
23        InputStream is = new FileInputStream("manifest.mf");
24        BufferedReader buf = new BufferedReader(new InputStreamReader(is));
25        
26        String line = buf.readLine();
27        StringBuilder sb = new StringBuilder();
28        
29        while(line != null){
30            sb.append(line).append("\n");
31            line = buf.readLine();
32        }
33        
34        String fileAsString = sb.toString();
35        System.out.println("Contents (before Java 7) : " + fileAsString);
36        
37        
38        // Reading file into Stirng in one line in JDK 7
39        String contents = new String(Files.readAllBytes(Paths.get("manifest.mf")));
40        System.out.println("Contents (Java 7) : " + contents);
41        
42        
43        
44        // Reading file into String using proper character encoding
45        String fileString = new String(Files.readAllBytes(Paths.get("manifest.mf")), StandardCharsets.UTF_8);
46        System.out.println("Contents (Java 7 with character encoding ) : " + fileString);
47        
48
49        // It's even easier in Java 8
50        Files.lines(Paths.get("manifest.mf"), StandardCharsets.UTF_8).forEach(System.out::println);
51        
52    }
53
54
55}
56
upvote
downvote
1public static String loadFileAsString(String path) {
2  InputStream stream = YourClassName.class.getResourceAsStream(path);
3  try {
4    String result = new String(stream.readAllBytes());
5    return result;
6  } catch (IOException e) {
7    e.printStackTrace();
8    return null;
9  }
10}
11
upvote
downvote
queries leading to this page
make string file readableread string from file javajava files read to stringload text file into string 5b 5d javaread entire file java into stringget file content to string javajava read file into stringread entire file as string javahow to read a whole file into a string in javahow to covert file to stringread txt file as strign javajava read file to string java 8load file into string javajava load file as stringhow to read from file and convert to string in javaparse file to string javajava buffer file to stringjava file contents to stringhow to save file contents to a string javaload file as string javaread in a string java from filehow to read a string in a file as string javaread content of filejava file read content to stringconvert any file to stringput file in string javaload a json file into pythonjava filereader read to stringfile to string and string to file java 22read file to 22java read file in stringhow to get fiel data as a stringjava read file to string java 11java read txt file into stringread string from filefile to a string javaconvert a file to string in javafile content to string javasjava 1 8 filereader to stringconvert files content into a string in javajava file get content as stringread file into string java txt to string javareading a string java 7java 11 read file as a single stringoutput file content as string javafile contnet tto string in java 8file into string javaread a string from a filefile read as string javaread file in string javajava filereader to stringjava read from file to stringuse file content as string javaread file content into a string javajava convert filereader to stringhow to convert file into stringjava pass a file to stringload string from file javajava file to strjava file get string contenthow to save read file to string in javajava read contents of a file as stringhow to read a file in as a string javafrom file to stringread file into stringconvert file contents to string in javaget string from filejava read file by stringget file content as string javajava parse content file to stringread full file to string javajava load file string readertext file to string javajava filereader read into stringload a txt as string javaread a file as a string javajava read a file as stringread from text file java to stringfile to string conents converterjava read file content into stringfile java to stringjava read content of file as stringget file as string javajava io read file into stringjava files to stringhow to read a file and save as a stringjava read the file contentjava to read file into stringpublic string read text 28 29 3b java return stringread a text file to string javahow to read a file in java and convert to stringhow to input string in file in javareading a string from a file in javaload a file in a string javaread file as string in java 8java get file content as stringread a file and convert to string in javaread a file to a string javaread as stringfile content as string javaread the txt document to a string javahow to turn a text file line into a string javaread strings from file javainputting string from file in javajava read complete file into stringopen a file string javajava read file stream to stringread strings from files javaread file as stringread file content to string javahow to load file to string in javaread content of file to string in javahow to turn file contents into string javajava how to load text from text file into stringhow to use text from a file to string in java java read a file as a stringread file content to stringjava read file contents to stringfile content to string javajava string filesjava read text file contentsjava read line by line a file with stringbuilderconverting a file to a string javajava read text file to stringhow to read a string in java7reading text file into string javaget text file as string javahow to read file as string in javastring reader and file reader javaread in string from file at once javahow to make file in to stringjava full read file to stringread file as string javajava convert text file to stringtxt file to string javajava 8 read file to stringread from file to string javaget string from string file to display in javareadfile to string javajava get file as stringread file into a string javagetting the whole file contents with bufferedreader javbread a file to string javaread gile into stringhow to get the contents of a string bufferedreader in javahow to get strings from filesjava file to string one lineread file in striinfjava read file contentshow to read in file as a string javaget string from file javajava how to read a file into a stringfile to string java 8how to convert a file to string in javahow to get a string from a fileload file as stringhow to get string from fileread text file into string javajava 5 read file to stringstore a file in a string javaread into stringjava 8 read file as stringjava get string from txtreadallbytes to stringread whole file to string javajava get file from stringread whole file into string javajava read whole file into stringjava 7 22files lines 22txt file read in java to stringjava return file contents as stringjava read whole file to stringread file from string javafile to stringreader javaread file as string builderjava load string from fileget string of filehow to create a string from file text in javaread text file in a string javafilereader to stringfile content to stringjava file into stringread in strings from file javaread file to strfile to string jvajava read all file contents into stringjava read entire file into stringget a string from file javajava read file and put content to stringjava read file as stringread input file to a stringjava read string to filejava read entire file as stringconvert file to string javaget file content in java to stringload file from url into string javareading string out of filejava read string from filejava read file to stringbuilderload file to stringconvert file to one stringfile input as stringjava read file to stringjava file to stringfile to stringread contents of file as string javajava file to string readerget all text from file in string javafile as stringjava get file contents as stringjava file to stringjava read from file in to stringjava read file to string with encodingjava filereader read stringjava file content to stringjava text file to stringjava readfile to stringread file from content 3a 2f 2fjava files read txt to stringreading all text in file to string javaread content file javahow to read a file to a java stringread file to string javajava read file content as stringfile to string javacontent as string javajava read file into a stringfilereader string javajava file get read file to stringjava write read string from fileset file content as string javahow to read entire file into a string javajava read text file into stringreading the content of a file into a stringhow to read a file into a string javareadfile file 3a filepath tostring 28 29read file to stringread file data as string in javaread file contents java