簡體   English   中英

將文件內容讀入循環鏈接列表

[英]Reading in file content into a circularly 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();


    }

}

如果我理解正確,請嘗試此操作。

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);
        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM