Java 如何从文件中读取并将内容保存到链表中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16247623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read from a file and save the content into a linked list?
提问by user2252700
I'm trying to write the code for a program that saves the content of a txt file into a singly linked list, every line in the txt file represent an object. I used Scanner to read from the file but I don't know how to get the words in the list.
我正在尝试为一个程序编写代码,该程序将 txt 文件的内容保存到一个单向链表中,txt 文件中的每一行都代表一个对象。我使用 Scanner 从文件中读取,但我不知道如何获取列表中的单词。
any tip or hint is highly appreciated.
任何提示或提示都非常感谢。
my attempt:
我的尝试:
class Item
类项目
public class Item implements Serializable {
private String name;
private double price;
public Item() {
name="" ;
price=0.00 ;
}
public Item(String n, double p) {
name = n ;
price = p ;
}
public void setName(String n) {
name = n ;
}
public void setPrice(double p) { price = p ;
}
public String getName() {
return name ;
}
public double getPrice() {
return price ;
}
}
class Node
类节点
public class Node {
public Item data ;
public Node next ;
public Node(Item d) {
data = d ;
}
public Node(Item d, Node n) {
data = d ;
next = n ;
}
public String toString() {
return data+"";
}
}
class ListItems
类 ListItems
public class ListItems {
private Node first;
public ListItems(){
first=null;
}
public boolean isEmpty(){
return first==null;
}
public void addAnItems(Item d, int i){
Node node1=new Node(d);
node1.next=first;
first=items.get(i);
}
public void displayList(){
Node current = first;
while(current != null){
System.out.println(current.toString());
current=current.next;
}
}
}
class FileItems
类文件项
import java.io.* ;
import java.util.* ;
public class FileItems {
public static void main (String[]args){
Scanner input1=new Scanner(System.in) ;
try {
Scanner input2=new Scanner(new File("items.txt")) ;
}
catch (FileNotFoundException e) {
System.out.println("ERROR OPENING FILE") ;
System.Exit(1) ;
}
while (input2.hasNext()){
String name1=input2.next() ;
double price1=input2.nextDouble() ;
}
ListItems items=new ListItems();
System.out.println("CHOOSE ONE OF THE FOLLOWING OPTIONS :") ;
System.out.println("ENTER 1 TO ADD AN ITEM TO THE LIST") ;
System.out.println("ENTER 2 TO DELETE AN ITEM FROM THE LIST") ;
System.out.println("ENTER 3 TO DISPLAY ALL THE ITEMS IN THE LIST") ;
System.out.println("ENTER 4 TO CLOSE THE PROGRAM") ;
int in=input1.nextInt() ;
switch (in) {
case 1 : case1() ;
break ;
case 2 : case1() ;
break ;
case 3 : displayList() ;
break ;
case 4 : System.Exit(1) ;
break ;
}
public void case1() {
System.out.println("ENTER THE NAME OF THE ITEM, THE PRICE AND THE INDEX ");
try {
System.out.println("NAME: ") ;
String n2=input1.next() ;
System.out.println("PRICE: ") ;
double p2=input1.nextDouble() ;
System.out.println("INDEX: ") ;
int index=input1.nextInt() ;
}
catch (InputMismatchException e) {
System.out.print("INVALID INPUT") ;
}
Item I=new Item(n2, p2) ;
addAnItem(I, index) ;
}
public static case2() {
System.out.println("ENTER THE INDEX OF THE ITEM TO DELETE IT ") ;
try {
int index=input1.nextInt() ;
}
catch (InputMismatchException e) {
System.out.print("INVALID INPUT") ;
}
items.remove(index) ;
}
}
}
采纳答案by Mavrick
Open the text file and read each line as a String and place that String object into a LinkedList. Print all of the lines in the LinkedList in reverse order.
打开文本文件并将每一行作为一个字符串读取,然后将该字符串对象放入一个 LinkedList。以相反的顺序打印 LinkedList 中的所有行。
import java.util.*;
import java.io.*;
public class FileLinkList
{
public static void main(String args[])throws IOException{
String content = new String();
int count=1;
File file = new File("abc.txt");
LinkedList<String> list = new LinkedList<String>();
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()){
content = sc.nextLine();
list.add(content);
}
sc.close();
}catch(FileNotFoundException fnf){
fnf.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
System.out.println("\nProgram terminated Safely...");
}
Collections.reverse(list);
Iterator i = list.iterator();
while (i.hasNext()) {
System.out.print("Node " + (count++) + " : ");
System.out.println(i.next());
}
}
}