java - 将文件内容读入循环链表

标签 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();


    }

}

最佳答案

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

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

}

关于java - 将文件内容读入循环链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21173275/

相关文章:

c - 链接列表的一些帮助

c++ - 分配给函数调用返回的指针

java - java中的链表反向函数

c++ - 更改链表反转中的行会导致错误

java - 在同一个事务中创建和查找一个 jpa 实体?

java - 一种使 Eclipse 抵抗 Java 更新的更好方法

java - Java 什么时候调用 finalize() 方法?

java - 从为父类(super class)创建的链表中插入和提取子类

Java 条件语句未按预期工作

Unix 上的 Java Apache Commons CLI; > 被视为可执行文件的参数而不是重定向