Java File to String fileToString(String pathname)

Here you can find the source of fileToString(String pathname)

Description

file To String

License

Creative Commons License

Declaration

private static String fileToString(String pathname) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Creative Commons License 

import java.io.File;

import java.io.IOException;

import java.util.Scanner;

public class Main {
    private static String fileToString(String pathname) throws IOException {

        File file = new File(pathname);
        StringBuilder fileContents = new StringBuilder((int) file.length());
        Scanner scanner = new Scanner(file);
        String lineSeparator = System.getProperty("line.separator");

        try {/*from w  w  w . java  2 s. c om*/
            while (scanner.hasNextLine()) {
                fileContents.append(scanner.nextLine() + lineSeparator);
            }
            return fileContents.toString();
        } finally {
            scanner.close();
        }
    }
}

Related

  1. fileToString(String fileName, String timestamp, String adapterName)
  2. fileToString(String filePath)
  3. fileToString(String filePath, String encoding)
  4. fileToString(String nomeArquivo)
  5. fileToString(String path)
  6. fileToString(String strPath)
  7. fileToStringArray(String fileName)
  8. fileToStringBuffer(File file)
  9. fileToStringList(File f)