繁体   English   中英

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

[英]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 1在读取“编码”时,程序应读取相应的数字,如果数字为1,则返回“ set”,如果数字不是1,则返回“ not set”。

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

我想要的是 :我希望程序读取字符串以及相应的数字,并根据条件返回语句。记住单词“ Coding”和数字在同一行。 请指导我。

您应该添加sc.hasNext(); 到你的一会儿状态。 sc.next(); 将读取下一个值,并且在检查条件时会丢失该值。 我还finally添加了一些有关流关闭操作的建议。

下列代码中的代码:

Coding
Coding is set.

“ readFrom.txt”具有文本:“编码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();
         }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM