Android Text File Read fileToSet(final String dirname, final String filename, final Set set)

Here you can find the source of fileToSet(final String dirname, final String filename, final Set set)

Description

file To Set

License

Apache License

Declaration

public static boolean fileToSet(final String dirname,
        final String filename, final Set<String> set) 

Method Source Code

/*/*from   w ww.  j  a  va 2s .co m*/
Copyright 2008 Flaptor (flaptor.com) 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

    http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import org.apache.log4j.Logger;

public class Main{
    /**
     * @see fileToSet(String,String,Set<String>,Logger) 
     *
     * It uses a default Logger
     */
    public static boolean fileToSet(final String dirname,
            final String filename, final Set<String> set) {
        return fileToSet(dirname, filename, set, logger);
    }
    /**
     * Loads the lines from a file into a set.
     * @param dirname the name of the directory. If null, it gets replaced by ".".
     * @param filename the name of the file (may include path information).
     * @param set the set that will hold the lines from the file.
     * @param logger
     *          the logger where to log errors.
     * @return true if ok, false in case of an error.
     * @throws no exception is thrown by this implementation.
     *         The returned value must be checked to verify that no error has ocurred.
     */
    public static boolean fileToSet(final String dirname,
            final String filename, final Set<String> set, Logger logger) {
        List<String> list = new ArrayList<String>();
        boolean ret = fileToList(dirname, filename, list, logger);
        if (ret) {
            set.addAll(list);
        }
        return ret;
    }
    /**
     * @see fileToList(String,String,List<String>,Logger) 
     *
     * It uses a default Logger
     */
    public static boolean fileToList(final String dirname,
            final String filename, final List<String> list) {
        return fileToList(dirname, filename, list, logger);
    }
    /**
     * Loads the lines from a file into a list.
     * @param dirname the name of the directory. If null, it gets replaced by ".".
     * @param filename the name of the file (may include path information).
     * @param list the list that will hold the lines from the file.
     * @param logger
     *          the Logger where to log errors.
     * @return true if ok, false in case of an error.
     * @throws no exception is thrown by this implementation.
     *         The returned value must be checked to verify that no error has ocurred.
     */
    // Rafa assumes in com.flaptor.hounder.classifier.util.ProbsUtils#loadMaps(String, String, int)}
    // that this method returns a list in the same order that the file it reads.
    // If you change that, please update the training app.     
    public static boolean fileToList(final String dirname,
            final String filename, final List<String> list, Logger logger) {
        boolean ret = false;
        String fullname = getFullName(dirname, filename);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(fullname));
            while (reader.ready()) {
                list.add(reader.readLine());
            }
            ret = true;
        } catch (IOException e) {
            logger.error("fileToList: error reading the file " + fullname
                    + ": " + e, e);
        } finally {
            Execute.close(reader);
        }
        return ret;
    }
    /**
     * Creates a fully qualified path from a dirname and a filename.
     * @param dirname the name of the directory. If null, it gets replaced by ".".
     * @param filename the name of the file (may include path information).
     * @return a String with the full name of the file.
     */
    private static String getFullName(final String dirname,
            final String filename) {
        File f = new File(dirname, filename);
        return f.getAbsolutePath();
    }
}

Related

  1. fileToListByLine(String fileName)
  2. getFileContent(File file)
  3. getFileContent(File file, String charSet)
  4. getFileResourceText(String path)
  5. read(File file)