Quantcast
Viewing all articles
Browse latest Browse all 36

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, Files

    static Charset charset = com.google.common.base.Charsets.UTF_8;public static String guava_ServerFile( URL url ) throws IOException {    return Resources.toString( url, charset );}public static String guava_DiskFile( File file ) throws IOException {    return Files.toString( file, charset );}

  • APACHE - COMMONS IO using classes IOUtils, FileUtils

    static Charset encoding = org.apache.commons.io.Charsets.UTF_8;public static String commons_IOUtils( URL url ) throws IOException {    java.io.InputStream in = url.openStream();    try {        return IOUtils.toString( in, encoding );    } finally {        IOUtils.closeQuietly(in);    }}public static String commons_FileUtils( File file ) throws IOException {    return FileUtils.readFileToString( file, encoding );    /*List<String> lines = FileUtils.readLines( fileName, encoding );    return lines.stream().collect( Collectors.joining("\n") );*/}

  • Java 8 BufferReader using Stream API

    public static String streamURL_Buffer( URL url ) throws IOException {    java.io.InputStream source = url.openStream();    BufferedReader reader = new BufferedReader( new InputStreamReader( source ) );    //List<String> lines = reader.lines().collect( Collectors.toList() );    return reader.lines().collect( Collectors.joining( System.lineSeparator() ) );}public static String streamFile_Buffer( File file ) throws IOException {    BufferedReader reader = new BufferedReader( new FileReader( file ) );    return reader.lines().collect(Collectors.joining(System.lineSeparator()));}

  • Scanner Class with regex \A. which matches the beginning of input.

    static String charsetName = java.nio.charset.StandardCharsets.UTF_8.toString();public static String streamURL_Scanner( URL url ) throws IOException {    java.io.InputStream source = url.openStream();    Scanner scanner = new Scanner(source, charsetName).useDelimiter("\\A");    return scanner.hasNext() ? scanner.next() : "";}public static String streamFile_Scanner( File file ) throws IOException {    Scanner scanner = new Scanner(file, charsetName).useDelimiter("\\A");    return scanner.hasNext() ? scanner.next() : "";}

  • Java 7 (java.nio.file.Files.readAllBytes)

    public static String getDiskFile_Java7( File file ) throws IOException {    byte[] readAllBytes = java.nio.file.Files.readAllBytes(Paths.get( file.getAbsolutePath() ));    return new String( readAllBytes );}

  • BufferedReader using InputStreamReader.

    public static String getDiskFile_Lines( File file ) throws IOException {    StringBuffer text = new StringBuffer();    FileInputStream fileStream = new FileInputStream( file );    BufferedReader br = new BufferedReader( new InputStreamReader( fileStream ) );    for ( String line; (line = br.readLine()) != null; )        text.append( line + System.lineSeparator() );    return text.toString();}

Example with main method to access the above methods.

public static void main(String[] args) throws IOException {    String fileName = "E:/parametarisation.csv";    File file = new File( fileName );    String fileStream = commons_FileUtils( file );            // guava_DiskFile( file );            // streamFile_Buffer( file );            // getDiskFile_Java7( file );            // getDiskFile_Lines( file );    System.out.println( " File Over Disk : \n"+ fileStream );    try {        String src = "https://code.jquery.com/jquery-3.2.1.js";        URL url = new URL( src );        String urlStream = commons_IOUtils( url );                // guava_ServerFile( url );                // streamURL_Scanner( url );                // streamURL_Buffer( url );        System.out.println( " File Over Network : \n"+ urlStream );    } catch (MalformedURLException e) {        e.printStackTrace();    }}

@see


Viewing all articles
Browse latest Browse all 36

Trending Articles



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