如何从文件中的一行读取单词和整数

How to read word and integers from a line in a file

我这里有一个代码:-

package testFiles;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;

public class ReadFile {
public static void main(String[] args){
    FileReader in=null;
    FileWriter out=null;
    String line;
    File fp=new File("readFrom.txt");
    try {
        Scanner sc=new Scanner(fp);
        //System.out.println(sc.next());
        if(sc.next().contentEquals("Coding")){
            System.out.println("####");
            while(sc.next().contentEquals("\n")==false){
                if(sc.nextInt()==1){
                    System.out.println("Coding is set.");
                }
                else{
                    System.out.println("Coding is not set.");
                }
            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

我打算做什么:我想从文件中读出一个词 "Coding"。在这个 word.ie 之后的 space 之后会有一个整数。该文件将是: Coding 1 在读取 'Coding' 时,程序应该读取相应的数字和 return 如果数字是 1,'set',如果数字不是 1,则 'not set'。

我的问题: 我可以读取字符串并验证它是否是 Coding.But 我无法获取号码。

what i want:我希望程序根据conditions.Remember这个词读取字符串以及相应的数字和return语句 "Coding" 和数字在同一行。 请指导我。

您应该将 sc.hasNext(); 添加到您的 while 条件中。 sc.next(); 将读取下一个值,您将在检查条件时丢失它。我还添加了一些关于 finally.

上流关闭操作的建议

Code at the below prints:

Coding
Coding is set.

"readFrom.txt" has text: "Coding 1"

public static void main(String[] args) {

    FileReader in = null;
    FileWriter out = null;
    File fp = new File("D:/readFrom.txt");
    Scanner sc = null;
    try {
        sc = new Scanner(fp);
        String str = "";
        while (sc.hasNext()) {
            str = sc.next();
            if (str.contentEquals("Coding")) {
                System.out.println(str);
                if (sc.nextInt() == 1) {
                    System.out.println("Coding is set.");
                } else {
                    System.out.println("Coding is not set.");
                }
            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

        try {
            if (out != null)
                out.close(); // you should close it on finally
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            if (in != null)
                in.close(); // you should close it on finally
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            if (sc != null)
                sc.close(); // you should close it on finally
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

示例代码

    while(sc.hasNext()){
        try{
           String str = sc.nextLine();
           String strArray[] = str.split(" ");
           if ( strArray.length > 1 &&  strArray[0].equals("Coding")){
               int count = Integer.parseInt(strArray[1]);
               // check for count value == 1 or  not and do processing
               if ( count == 1){
                    System.out.println("Coding set");
               }else{
                   System.out.println("Coding not set");
               }
           }else{
                System.out.println("Coding not set");
           }
         }catch(Exception err){
             err.printStackTrace();
         }
    }