将文件内容读入循环链表

时间:2014-01-16 21:29:14

标签: java linked-list

我试图弄清楚如何将.txt文件的内容放入循环链表中。内容可以随机放入列表中。是的,这是一项任务,但我完全陷入困境。我真的很感激。

    public class DuckDuckGoose
    {

    private FileReader fr;
    private Scanner sc;

    /**
     * openFile  method to open the file, then invokes the method that reads it
     * @param fileName name
     * @throws FileNotFoundException
     */
    public void openFile(String fileName) 
    {
        try 
        {
            fr = new FileReader(fileName);
            sc = new Scanner(fr);
            readAndSOPFile();
        } 
        catch (FileNotFoundException fnfe) 
        {
            System.out.println("File not Found");
        }
        catch (NoSuchElementException nsee)
        {
        System.out.println("No such element was found");
        }
        catch (Exception e)
        {
        System.out.println("An exception occurred");
        }
        finally
        {           
        try 
        {
            fr.close();
            sc.close();             
        }
        catch (IOException ioe)
        {
            System.out.println("Cannot close the output file");
        }
        catch (NullPointerException npe)
        {
            System.out.println("File was not created correctly");
        }
        catch (Exception e)
        {
            System.out.println("An error occurred");
        }

}
}

/**
 * readAndSOPFile reads each token and prints it to the console on a single line
 * @throws IllegalStateException
 * @throws NoSuchElementException
 */
public void readAndSOPFile() throws IllegalStateException, NoSuchElementException
{
    while (sc.hasNext())
    {
        String s = sc.next();
        System.out.println(s);          
    }
}

public static void main(String[] args)
{
    DuckDuckGoose ddg = new DuckDuckGoose();
    ddg.openFile("students.txt");

    LinkedList<String> ll = new LinkedList<String>();
    ListIterator<String> iter = ll.listIterator();


    }

}

1 个答案:

答案 0 :(得分:1)

试试这个,如果我理解正确的话。

public class DuckDuckGoose
{

    private FileReader fr;
    private Scanner sc;

    public static void main(String[] args)
    {



        List<String> ll = new LinkedList<String>();
        ListIterator<String> iter = ll.listIterator();

        DuckDuckGoose ddg = new DuckDuckGoose();
        ddg.openFile("R.txt",ll);


    }

    /**
     * openFile  method to open the file, then invokes the method that reads it
     *
     * @param fileName name
     * @param ll
     * @throws FileNotFoundException
     */
    public void openFile(String fileName, List<String> ll)
    {
        try
        {
            fr = new FileReader(fileName);
            sc = new Scanner(fr);
            readAndSOPFile(ll);
        }
        catch (FileNotFoundException fnfe)
        {
            System.out.println("File not Found");
        }
        catch (NoSuchElementException nsee)
        {
            System.out.println("No such element was found");
        }
        catch (Exception e)
        {
            System.out.println("An exception occurred");
        }
        finally
        {
            try
            {
                fr.close();
                sc.close();
            }
            catch (IOException ioe)
            {
                System.out.println("Cannot close the output file");
            }
            catch (NullPointerException npe)
            {
                System.out.println("File was not created correctly");
            }
            catch (Exception e)
            {
                System.out.println("An error occurred");
            }

        }
    }

    /**
     * readAndSOPFile reads each token and prints it to the console on a single line
     * @throws IllegalStateException
     * @throws NoSuchElementException
     */
    public void readAndSOPFile(List<String> list) throws IllegalStateException, NoSuchElementException
    {
        while (sc.hasNext())
        {
            String s = sc.next();
            list.add(s);
            System.out.println(s);
        }
    }

}