[英]How do I read strings from a textfile in Linked List usings Java?
我不記得如何從textfile.txt獲取字符串
我只有兩個命令,然后是信息的名稱
I = Insert
D = Delete
例:
I Blue
I Red
D Blue
I Green
D Red
D Green
所以一個是字符串命令,另一個是字符串信息
任何幫助?
編輯:
我忘了提它告訴我提示用戶指定輸入文件的名稱
這有點原始,但是可以。 它使用Scanner
類和StringTokenizer
。 當然,可能還有許多其他方法可以做到這一點。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class FReader {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Enter file name:");
Scanner input = new Scanner(System.in);
String fname = input.nextLine();
File file = new File(fname);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
StringTokenizer tokens = new StringTokenizer(line);
String command = tokens.nextToken();
String info = tokens.nextToken();
System.out.println("command = " + command + " info = " + info);
}
}
}
編輯:測試了以下數據:
I Blue
I Red
D Blue
I Green
D Red
D Green
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.