从文件中读取文本并将每行中的每个单词存储到单独的变量中

时间:2012-02-07 18:17:37

标签: java file-io

我有一个包含以下内容的.txt文件:

1 1111 47
2 2222 92
3 3333 81

我想逐行阅读并将每个单词存储到不同的变量中。

例如:当我读到第一行“1 1111 47”时,我希望将第一个单词“1”存储到var_1,将“1111”存储到var_2,将“47”存储到var_3。然后,当它转到下一行时,值应分别存储在相同的var_1var_2var_3变量中。

我的初步方法如下:

import java.io.*;
class ReadFromFile
{
    public static void main(String[] args) throws IOException
    {
        int i;
        FileInputStream fin;
        try
        {
            fin = new FileInputStream(args[0]);
        }
        catch(FileNotFoundException fex)
        {
            System.out.println("File not found");
            return;
        }
        do 
        {
            i = fin.read();
            if(i != -1) 
                System.out.print((char) i);
        } while(i != -1);

        fin.close();
    }
}

请告诉我你的建议。谢谢

4 个答案:

答案 0 :(得分:6)

public static void main(String[] args) throws IOException {
    File file = new File("/path/to/InputFile");
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    String line = null;
    while( (line = br.readLine())!= null ){
        // \\s+ means any number of whitespaces between tokens
        String [] tokens = line.split("\\s+");
        String var_1 = tokens[0];
        String var_2 = tokens[1];
        String var_3 = tokens[2];
    }
}

答案 1 :(得分:0)

        try {
        BufferedReader fr = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ASCII"));
        while(true)
        {
            String line = fr.readLine();
            if(line==null)
                break;
            String[] words = line.split(" ");//those are your words
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }

希望这有帮助!

答案 2 :(得分:0)

查看BufferedReader阅读行。之后你必须使用StringTokenizer或String的分割等方式来爆炸。

答案 3 :(得分:0)

import java.io.*;
import java.util.Scanner;

class Example {
    public static void main(String[] args) throws Exception {

        File f = new File("main.txt");
        StringBuffer txt = new StringBuffer();
        FileOutputStream fos = new FileOutputStream(f);
        for (int i = 0; i < args.length; i++) {
            txt.append(args[i] + " ");
        }
        fos.write(txt.toString().getBytes());
        fos.close();
        FileInputStream fis = new FileInputStream("main.txt");
        InputStreamReader input = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(input);
        String data;
        String result = new String();
        StringBuffer txt1 = new StringBuffer();
        StringBuffer txt2 = new StringBuffer();
        File f1 = new File("even.txt");
        FileOutputStream fos1 = new FileOutputStream(f1);
        File f2 = new File("odd.txt");
        FileOutputStream fos2 = new FileOutputStream(f2);
        while ((data = br.readLine()) != null) {
            result = result.concat(data);
            String[] words = data.split(" ");
            for (int j = 0; j < words.length; j++) {
                if (j % 2 == 0) {
                    System.out.println(words[j]);
                    txt1.append(words[j] + " ");
                } else {
                    System.out.println(words[j]);
                    txt2.append(words[j] + " ");
                }
            }
        }
        fos1.write(txt1.toString().getBytes());
        fos1.close();
        fos2.write(txt2.toString().getBytes());
        fos2.close();
        br.close();

    }
}