Gets the content from a File as String Array List : Text File « File « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » File » Text File 
11.80.10.Gets the content from a File as String Array ListPrevious/Next
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;

/*
 * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
 *
 * http://izpack.org/
 * http://izpack.codehaus.org/
 *
 * Copyright 2005 Marc Eppelmann
 *
 * 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.
 */


/**
 * Provides general global file utility methods
 *
 @author marc.eppelmann
 */
class FileUtil
{
    //~ Constructors ***********************************************************************

    /**
     * Creates a new FileUtil object.
     */
    public FileUtil()
    {
    }

    //~ Methods ****************************************************************************

    /**
     * Gets the content from a File as StringArray List.
     *
     @param fileName A file to read from.
     @return List of individual line of the specified file. List may be empty but not
     *         null.
     @throws IOException
     */
    public static ArrayList getFileContent(String fileName)
            throws IOException
    {
        ArrayList result = new ArrayList();

        File aFile = new File(fileName);

        if (!aFile.isFile())
        {
            //throw new IOException( fileName + " is not a regular File" );
            return result; // None
        }

        BufferedReader reader = null;

        try
        {
            reader = new BufferedReader(new FileReader(aFile));
        }
        catch (FileNotFoundException e1)
        {
            // TODO handle Exception
            e1.printStackTrace();

            return result;
        }

        String aLine = null;

        while ((aLine = reader.readLine()) != null)
        {
            result.add(aLine + "\n");
        }

        reader.close();

        return result;
    }

    /**
     * Searches case sensitively, and returns true if the given SearchString occurs in the
     * first File with the given Filename.
     *
     @param aFileName     A files name
     @param aSearchString the string search for
     @return true if found in the file otherwise false
     */
    public static boolean fileContains(String aFileName, String aSearchString)
    {
        return (fileContains(aFileName, aSearchString, false));
    }

    /**
     * Tests if the given File contains the given Search String
     *
     @param aFileName             A files name
     @param aSearchString         the String to search for
     @param caseInSensitiveSearch If false the Search is casesensitive
     @return true if found in the file otherwise false
     */
    public static boolean fileContains(String aFileName, String aSearchString,
                                       boolean caseInSensitiveSearch)
    {
        boolean result = false;

        String searchString = caseInSensitiveSearch
                ? aSearchString.toLowerCase() : aSearchString;

        ArrayList fileContent = new ArrayList();

        try
        {
            fileContent = getFileContent(aFileName);
        }
        catch (IOException e)
        {
            // TODO handle Exception
            e.printStackTrace();
        }

        Iterator linesIter = fileContent.iterator();

        while (linesIter.hasNext())
        {
            String currentline = (StringlinesIter.next();

            if (caseInSensitiveSearch)
            {
                currentline = currentline.toLowerCase();
            }

            if (currentline.indexOf(searchString> -1)
            {
                result = true;

                break;
            }
        }

        return result;
    }

    /**
     * Gets file date and time.
     *
     @param url The URL of the file for which date and time will be returned.
     @return Returns long value which is the date and time of the file. If any error
     *         occures returns -1 (=no file date and time available).
     */
    public static long getFileDateTime(URL url)
    {
        if (url == null)
        {
            return -1;
        }

        String fileName = url.getFile();
        if (fileName.charAt(0== '/' || fileName.charAt(0== '\\')
        {
            fileName = fileName.substring(1, fileName.length());
        }

        try
        {
            File file = new File(fileName);
            // File name must be a file or a directory.
            if (!file.isDirectory() && !file.isFile())
            {
                return -1;
            }

            return file.lastModified();
        }
        catch (java.lang.Exception e)
        {   // Trap all Exception based exceptions and return -1.
            return -1;
        }
    }

    public static String[] getFileNames(String dirPaththrows Exception
    {
        return getFileNames(dirPath, null);
    }

    public static String[] getFileNames(String dirPath, FilenameFilter fileNameFilterthrows Exception
    {
        String fileNames[] null;
        File dir = new File(dirPath);
        if (dir.isDirectory())
        {
            if (fileNameFilter != null)
            {
                fileNames = dir.list(fileNameFilter);
            }
            else
            {
                fileNames = dir.list();
            }
        }
        return fileNames;
    }

    /**
     * Test main
     *
     @param args
     */
    public static void main(String[] args)
    {
    }
}
11.80.Text File
11.80.1.Read the contents of a file and place them in a string object.
11.80.2.Dump a String to a text file with encoding.
11.80.3.Load a text file contents as a String.
11.80.4.Read and return the entire contents of the supplied File.
11.80.5.Read ByteArrayInputStream to String
11.80.6.Load File As Text
11.80.7.Load file line by line
11.80.8.Load file content to List
11.80.9.Read string from InputStream and Reader
11.80.10.Gets the content from a File as String Array List
11.80.11.Searches case sensitively in a file
11.80.12.ReadLines: read file to list of strings
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.