Reading text from file and storing each word from

2020-06-28 16:20发布

I have a .txt file with the following content:

1 1111 47
2 2222 92
3 3333 81

I would like to read line-by-line and store each word into different variables.

For example: When I read the first line "1 1111 47", I would like store the first word "1" into var_1, "1111" into var_2, and "47" into var_3. Then, when it goes to the next line, the values should be stored into the same var_1, var_2 and var_3 variables respectively.

My initial approach is as follows:

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();
    }
}

Kindly give me your suggestions. Thank You

标签: java file-io
4条回答
戒情不戒烟
2楼-- · 2020-06-28 16:46

Check out BufferedReader for reading lines. You'll have to explode the line afterwards using something like StringTokenizer or String's split.

查看更多
姐就是有狂的资本
3楼-- · 2020-06-28 16:48
        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();
    }

Hope this Helps!

查看更多
男人必须洒脱
4楼-- · 2020-06-28 17:06
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];
    }
}
查看更多
姐就是有狂的资本
5楼-- · 2020-06-28 17:06
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();

    }
}
查看更多
登录 后发表回答