이의 이름을 지정하라는 메시지를 표시 하더군요 조금 원시적이지만 작동합니다. 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
무슨 질문. 파일을 읽는 방법? 명령을 처리하는 방법은 무엇입니까? –
파일을 읽고 txt 파일에서 명령을받는 방법 – randomizertech
숙제 같은 냄새가납니다. 어쩌면 그걸로 태그를 붙여야할까요? – rfeak