这是我的文本文件" demo.txt"。我必须读取值值并分配变量,如成功规则,产品对象......
import java.io.*;
import java.util.*;
public class Read {
public static void main(String[] args) throws Exception {
File file=new File("demo.txt");
Scanner s=new Scanner(file);
while(s.hasNextLine()) {
String rule=s.next();
System.out.println(rule);
}
}
}
这里只是想要打印规则成功然后做什么:
成功
产品
如果产品的价格大于100且数量小于2且类别为"杂货店"然后
打印折扣等于100
然后显示折扣为100
答案 0 :(得分:0)
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
*
* Java program to read file using Scanner class in Java.
* java.util.Scanner is added on Java 5 and offer convenient method to read data
*
* @author
*/
public class ScannerExample {
public static void main(String args[]) throws FileNotFoundException {
//creating File instance to reference text file in Java
File text = new File("C:/temp/test.txt");
//Creating Scanner instnace to read File in Java
Scanner scnr = new Scanner(text);
//Reading each line of file using Scanner class
int lineNumber = 1;
while(scnr.hasNextLine()){
String line = scnr.nextLine();
System.out.println("line " + lineNumber + " :" + line);
lineNumber++;
}
}
}
了解详情:http://www.java67.com/2012/11/how-to-read-file-in-java-using-scanner-example.html#ixzz4fWXsFF00