Java Text File Read by Charset readFileToString(String path, Charset charset)

Here you can find the source of readFileToString(String path, Charset charset)

Description

read File To String

License

Open Source License

Declaration

public static String readFileToString(String path, Charset charset) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static String readFileToString(String path, Charset charset) throws IOException {
        List<String> lines = Files.readAllLines(Paths.get(path), charset);
        return join("", lines);
    }/*from  w w  w  .  j a v  a  2 s  .  c o m*/

    private static String join(String delim, Collection<?> col) {
        StringBuilder sb = new StringBuilder();
        Iterator<?> iter = col.iterator();
        if (iter.hasNext())
            sb.append(iter.next().toString());
        while (iter.hasNext()) {
            sb.append(delim);
            sb.append(iter.next().toString());
        }
        return sb.toString();
    }
}

Related

  1. readFileAsString(String path, String charsetName)
  2. readFileContents(File file, Charset charset)
  3. readFileToLines(String inFile, String inCharset)
  4. readFileToList(String filePath, String charsetName)
  5. readFileToString(String filename, Charset encoding)
  6. readFromInputStream(InputStream stream, Charset charset)
  7. readInputStream(InputStream stream, Charset cs)
  8. readInputStreamToString(InputStream instream, Charset charset)
  9. readLineFromStream(InputStream is, StringBuffer buffer, CharsetDecoder decoder)