gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:07:15 25 4
gpt4 key购买 nike

我想弄清楚如何将.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com