Java File to String fileToStringArray(String fileName)

Here you can find the source of fileToStringArray(String fileName)

Description

file To String Array

License

Open Source License

Declaration

public static String[] fileToStringArray(String fileName) throws IOException 

Method Source Code


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

import java.io.*;

public class Main {
    public static String[] fileToStringArray(String fileName) throws IOException {
        String data = fileToString(fileName);
        String str = cleanText(data);
        String[] wordArray = str.split(" ");

        return wordArray;
    }/*w  ww  . java2 s .c om*/

    public static String fileToString(String fileName) throws IOException {
        String result = "";
        try {
            FileInputStream file = new FileInputStream(fileName);
            byte[] b = new byte[file.available()];
            file.read(b);
            file.close();
            result = new String(b);
        } catch (FileNotFoundException e) {
            System.out.println("oops");
        }
        return result;

    }

    public static String cleanText(String s) {
        s = s.replaceAll("\\p{Punct}", "");
        s = s.replaceAll("\\s+", " ");

        return s;

    }
}

Related

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