谁有办法做到这一点?我是新人,需要知道如何执行这个任务。真的需要开始这个项目的建议,谢谢。
代码如下:
package code;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
/**
 * 
 * This assignment involves character by character
 * processing, but this time the characters are not coming from a String, they
 * are coming from a file.
 * 
 * Because Java's file input classes throw various exceptions, which we do not
 * yet know how to handle, I am providing you with a class which deals with
 * those exceptions: UBFileReader. This is an iterator for a file.
 * You will be able to use this class during your write-up, but its source will
 * be hidden from you.
 * 
 * 
 * 
 * 
 * Overall, the project is a review viewer for various products. The information
 * about the reviews and products will be stored in a text file in the following format:
 * Products start with <p> and end with </p>
 * Reviews start with <c> and end with </c>
 * So given the input: <p>Mkay</p><c>Brown and hairy.</c>
 * The product would be: "Mkay"
 * The review would be: "Brown and hairy."
 * 
 * The products and reviews should be stored in a HashMap<String, LinkedList<String>>
 * where the key is the product and the value is a linked list containing all of the
 * reviews for the product.
 * 
 * The products and reviews and then displayed in a beautiful (light grey and yellow)
 * graphical user interface. The user can use this to browse the available products
 * and read their reviews. The user interface will be supplied for you, so all you
 * need to take care of is putting the appropriate information in the HashMap.
 * 
 */
public class TagFileParser {
    /**
     * Reads a file (identified by inputFilePath), one character at a time.
     * Products start with <p> and end with </p> as explained above. 
     * Reviews start with <c> and end with </c> as explained above.
     * Any text not inside of one of these tags should be ignored.
     * 
     * You may use only CharacterFromFileReader to read characters from the
     * input file.
     * 
     * In order to simplify the code writing experience, it is recommended
     * that you use a switch statement where the case would be the state.
     * This way, you only need to worry about what happens when you are at
     * that state. You should, however, fully understand the state diagram
     * as a whole and in parts, as you will be required to complete this
     * assignment next week at the beginning of lab.
     * 
     * @param String
     *            inputPath the path on the local filesystem to the input file
     * @returns a HashMap containing the product->linked list of reviews mappings.
     */
    public HashMap<String, List<String>> fillHashMap(String inputPath) {
        return null;
    }
}发布于 2011-02-27 03:40:20
使用BufferedReader读取文件。BufferedReader允许您逐行读取。
放入打开文件并一次读取一行的代码。
不要担心剩下的作业,就到这里吧。
当这是工作时,回来寻求更多的帮助。
发布于 2011-02-27 10:55:55
对于初学者,请通读this example。
因此,当您读取输入文件时,首先需要检查您正在读取的字符是否为'<‘。然后是“p”。然后是'>‘等等。
一旦您理解了如何将文件中的字符读取到程序中,请阅读HashMap和LinkedList。
PS: UBFileReader (您的教授提供的类)将替换该代码中的“抛出异常”短语。现在先不要担心这一点,先让程序的其余部分工作。
发布于 2015-02-16 15:23:48
public static void main(String[] args) {
        BufferedReader br = null;
        try {
            String sCurrentLine;
            br = new BufferedReader(new FileReader("C:\\testing.txt"));
            int i=0;
            HashMap<String, Integer> hm = new HashMap<String, Integer>();
            while ((sCurrentLine = br.readLine()) != null) {
            i++;
            hm.put(sCurrentLine,i);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }https://stackoverflow.com/questions/5127792
复制相似问题