Parsare un file di test (.txt)


/**
 * Parse a text file of lines. Returns the mapping.
 * @param filepath the physical path of the file
 * @return the mapping of a text file of lines
 * @throws FileNotFoundException
 */

public Set<String> parseTxt(String filepath) throws FileNotFoundException{
    Set<String> result = new HashSet<String>();
    //Note that FileReader is used, not File, since File is not Closeable
    File input = new File(filepath);
    Scanner scanner = new Scanner(new FileReader(input));
    try {
        //first use a Scanner to get each line
        while (scanner.hasNextLine()){
            String line = scanner.nextLine();
            if(!line.startsWith("#")){
                result.add(line);
            }
        }
    }
    finally {
        //ensure the underlying stream is always closed
        //this only has any effect if the item passed to the Scanner
        //constructor implements Closeable (which it does in this case).

        scanner.close();
    }
    return result;
}


Vediamo un esempio di utilizzo:

/**
 * Example of usage
 */
public static void main(String[] args){
	TXTReader reader = new TXTReader();
	String sourceFilePath = "C:/Users/celli/Documents/my_doc.txt";
	Set<String> lines = new HashSet<String>();
	try {
		lines = reader.parseTxt(sourceFilePath);
		System.out.println("Found "+lines.size()+" lines");
	} catch (FileNotFoundException e1) {
		e1.printStackTrace();
	}
}				
				

Ecco invece altre varianti di parsing di un file di testo:

/**
 * Parse a text file of lines having a pattern key=value. Returns the mapping.
 * @param filepath the physical path of the file
 * @return the mapping of a text file of lines having a pattern key=value
 * @throws FileNotFoundException
 */
public Map<String, String> parseEqualTxt(String filepath) 
			              throws FileNotFoundException{
	Map<String, String> result = new HashMap<String, String>();
	//Note that FileReader is used, not File, since File is not Closeable
	File input = new File(filepath);
    Scanner scanner = new Scanner(new FileReader(input));
    try {
      //first use a Scanner to get each line
      while (scanner.hasNextLine()){
        String line = scanner.nextLine();
        if(!line.startsWith("#")){
        	String[] mapping = line.split("=");
        	result.put(mapping[0], mapping[1]);
        }
      }
    }
    finally {
      //ensure the underlying stream is always closed
      //this only has any effect if the item passed to the Scanner
      //constructor implements Closeable (which it does in this case).
      scanner.close();
    }
    return result;
}		
				

/**
 * Parse a text file of lines having a pattern key=value. Returns the 
 * reverse mapping value=key.
 * @param filepath the physical path of the file
 * @return the reverse mapping of a text file of lines having a pattern key=value
 * @throws FileNotFoundException
 */
public Map<String, String> reverseParseEqualTxt(String filepath) 
						throws FileNotFoundException{
	Map<String, String> result = new HashMap<String, String>();
	//Note that FileReader is used, not File, since File is not Closeable
	File input = new File(filepath);
    Scanner scanner = new Scanner(new FileReader(input));
    try {
      //first use a Scanner to get each line
      while (scanner.hasNextLine()){
        String line = scanner.nextLine();
        if(!line.startsWith("#")){
        	String[] mapping = line.split("=");
        	result.put(mapping[1], mapping[0]);
        }
      }
    }
    finally {
      //ensure the underlying stream is always closed
      //this only has any effect if the item passed to the Scanner
      //constructor implements Closeable (which it does in this case).
      scanner.close();
    }
    return result;
}