Quantcast
Channel: How do I create a Java string from the contents of a file? - Stack Overflow
Browsing latest articles
Browse All 36 View Live

Answer by Johan Esteban Cañas Ossa for How do I create a Java string from the...

Using BufferedReader as mentioned in comments before but this way is more readable:String FILE_PATH = "filepath.txt";try (FileReader fileReader = new FileReader(FILE_PATH)) { BufferedReader...

View Article


Answer by Omkar T for How do I create a Java string from the contents of a file?

Pure kotlin code with no dependenciesWorks on all android versionsval fileAsString = file.bufferedReader().use { it.readText() }

View Article


Answer by TheWaterWave222 for How do I create a Java string from the contents...

Scanner sc = new Scanner(new File("yourFile.txt"));sc.useDelimiter("\\Z");String s = sc.next();

View Article

Answer by Nitin for How do I create a Java string from the contents of a file?

User java.nio.Files to read all lines of file.public String readFile() throws IOException { File fileToRead = new File("file path"); List<String> fileLines =...

View Article

Answer by leventov for How do I create a Java string from the contents of a...

Since JDK 11:String file = ...Path path = Paths.get(file);String content = Files.readString(path);// Or readString(path, someCharset), if you need a Charset different from UTF-8

View Article


Answer by Saikat for How do I create a Java string from the contents of a file?

Using JDK 8 or above:no external libraries usedYou can create a new String object from the file content (Using classes from java.nio.file package):public String readStringFromFile(String filePath)...

View Article

Answer by Yash for How do I create a Java string from the contents of a file?

Gathered all the possible ways to read the File as String from Disk or Network.Guava: Google using classes Resources, Filesstatic Charset charset = com.google.common.base.Charsets.UTF_8;public static...

View Article

Answer by Muskovets for How do I create a Java string from the contents of a...

Based on @erickson`s answer, you can use:public String readAll(String fileName) throws IOException { List<String> lines = Files.readAllLines(new File(fileName).toPath()); return String.join("\n",...

View Article


Answer by Malcolm Boekhoff for How do I create a Java string from the...

In one line (Java 8), assuming you have a Reader:String sMessage = String.join("\n", reader.lines().collect(Collectors.toList()));

View Article


Answer by OscarRyz for How do I create a Java string from the contents of a...

Also if your file happens to be inside a jar, you can also use this:public String fromFileInJar(String path) { try ( Scanner scanner = new Scanner(getClass().getResourceAsStream(path))) { return...

View Article

Answer by jamesjara for How do I create a Java string from the contents of a...

You can try Scanner and File class, a few lines solution try{ String content = new Scanner(new File("file.txt")).useDelimiter("\\Z").next(); System.out.println(content);}catch(FileNotFoundException e){...

View Article

Answer by Devram Kandhare for How do I create a Java string from the contents...

Use code:File file = new File("input.txt");BufferedInputStream bin = new BufferedInputStream(new FileInputStream( file));byte[] buffer = new byte[(int) file.length()];bin.read(buffer);String fileStr =...

View Article

Answer by satnam for How do I create a Java string from the contents of a file?

Using this library, it is one line:String data = IO.from(new File("data.txt")).toString();

View Article


Answer by user2058603 for How do I create a Java string from the contents of...

in java 8 , there are a new Class java.util.stream.StreamA stream represents a sequence of elements and supports different kind of operations to perform computations upon those elementsto Read more...

View Article

Answer by J-J for How do I create a Java string from the contents of a file?

import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Paths;Java 7String content = new String(Files.readAllBytes(Paths.get("readMe.txt")),...

View Article


Answer by Moritz Petersen for How do I create a Java string from the contents...

With Java 7, this is my preferred option to read a UTF-8 file:String content = new String(Files.readAllBytes(Paths.get(filename)), "UTF-8");Since Java 7, the JDK has the new java.nio.file API, which...

View Article

Answer by Haakon Løtveit for How do I create a Java string from the contents...

After Ctrl+F'ing after Scanner, I think that the Scanner solution should be listed too. In the easiest to read fashion it goes like this:public String fileToString(File file, Charset charset) { Scanner...

View Article


Answer by Ilya Gazman for How do I create a Java string from the contents of...

If you do not have access to the Files class, you can use a native solution.static String readFile(File file, String charset) throws IOException{ FileInputStream fileInputStream = new...

View Article

Answer by Andrei N for How do I create a Java string from the contents of a...

If you need a string processing (parallel processing) Java 8 has the great Stream API.String result = Files.lines(Paths.get("file.txt")) .parallel() // for parallel processing .map(String::trim) // to...

View Article

Answer by Ajk for How do I create a Java string from the contents of a file?

I cannot comment other entries yet, so I'll just leave it here.One of best answers here (https://stackoverflow.com/a/326448/1521167):private String readFile(String pathname) throws IOException {File...

View Article

Answer by Henry for How do I create a Java string from the contents of a file?

Be aware when using fileInputStream.available() the returned integer does not have to represent the actual file size, but rather the guessed amount of bytes the system should be able to read from the...

View Article


Answer by user590444 for How do I create a Java string from the contents of a...

import java.nio.file.Files;....... String readFile(String filename) { File f = new File(filename); try { byte[] bytes = Files.readAllBytes(f.toPath()); return new String(bytes,"UTF-8"); } catch...

View Article


Answer by wau for How do I create a Java string from the contents of a file?

A flexible solution using IOUtils from Apache commons-io in combination with StringWriter:Reader input = new FileReader();StringWriter output = new StringWriter();try { IOUtils.copy(input, output);}...

View Article

Answer by barjak for How do I create a Java string from the contents of a file?

This one uses the method RandomAccessFile.readFully, it seems to be available from JDK 1.0 !public static String readFileContent(String filename, Charset charset) throws IOException { RandomAccessFile...

View Article

Answer by Home in Time for How do I create a Java string from the contents of...

If it's a text file why not use apache commons-io? It has the following methodpublic static String readFileToString(File file) throws IOExceptionIf you want the lines as a list usepublic static...

View Article


Answer by Pablo Grisafi for How do I create a Java string from the contents...

A very lean solution based on Scanner:Scanner scanner = new Scanner( new File("poem.txt") );String text = scanner.useDelimiter("\\A").next();scanner.close(); // Put this call in a finally blockOr, if...

View Article

Answer by Peter Lawrey for How do I create a Java string from the contents of...

To read a File as binary and convert at the endpublic static String readFileAsString(String filePath) throws IOException { DataInputStream dis = new DataInputStream(new FileInputStream(filePath)); try...

View Article

Answer by finnw for How do I create a Java string from the contents of a file?

Guava has a method similar to the one from Commons IOUtils that Willi aus Rohr mentioned:import com.google.common.base.Charsets;import com.google.common.io.Files;// ...String text = Files.toString(new...

View Article

Answer by Scott S. McCoy for How do I create a Java string from the contents...

public static String slurp (final File file)throws IOException { StringBuilder result = new StringBuilder(); BufferedReader reader = new BufferedReader(new FileReader(file)); try { char[] buf = new...

View Article



Answer by Dan Dyer for How do I create a Java string from the contents of a...

There is a variation on the same theme that uses a for loop, instead of a while loop, to limit the scope of the line variable. Whether it's "better" is a matter of personal taste.for(String line =...

View Article

Answer by Jon Skeet for How do I create a Java string from the contents of a...

That code will normalize line breaks, which may or may not be what you really want to do.Here's an alternative which doesn't do that, and which is (IMO) simpler to understand than the NIO code...

View Article

Answer by Dónal for How do I create a Java string from the contents of a file?

If you're looking for an alternative that doesn't involve a third-party library (e.g. Commons I/O), you can use the Scanner class:private String readFile(String pathname) throws IOException { File file...

View Article

Answer by erickson for How do I create a Java string from the contents of a...

Read all text from a fileJava 11 added the readString() method to read small files as a String, preserving line terminators:String content = Files.readString(path, encoding);For versions between Java 7...

View Article


Answer by Claudiu for How do I create a Java string from the contents of a file?

Java attempts to be extremely general and flexible in all it does. As a result, something which is relatively simple in a scripting language (your code would be replaced with "open(file).read()" in...

View Article

Answer by DaWilli for How do I create a Java string from the contents of a file?

If you're willing to use an external library, check out Apache Commons IO (200KB JAR). It contains an org.apache.commons.io.FileUtils.readFileToString() method that allows you to read an entire File...

View Article

How do I create a Java string from the contents of a file?

I've been using the idiom below for some time now. And it seems to be the most wide-spread, at least on the sites I've visited.Is there a better/different way to read a file into a string in...

View Article

Browsing latest articles
Browse All 36 View Live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>