标记文本文件中的数据

Tokenizing data from textfile

我有一个文本文件

1

2

3

4

我正在尝试将每行数据标记为一个数组。但是,tokens[0] 正在读取 1 2 3 4。我如何以

tokens[0] = 1

tokens[1] = 2;

tokens[2] = 3;

我的代码基本上有什么问题。

  public static void readFile()
    {

        BufferedReader fileIn;

        String[] tokens;
        String inputLine;

        try 
        {
            fileIn = new BufferedReader(new FileReader("test.txt"));
            inputLine = fileIn.readLine();

            while (inputLine != null) 
            {
              tokens = inputLine.trim().split("\s+");

              System.out.println(tokens[0]);
              inputLine = fileIn.readLine();



            }
            fileIn.close();
        }

        catch (IOException ioe) 
        {
            System.out.println("ERROR: Could not open file.");
            System.out.println(ioe.getMessage());
        }    
    }
}

我认为这可能会解决您的问题

public static void readFile() {

    try {
        List<String> tokens = new ArrayList<>();
        Scanner scanner;
        scanner = new Scanner(new File("test.txt"));
        scanner.useDelimiter(",|\r\n");
        while (scanner.hasNext()) {
            tokens.add(scanner.next());
            System.out.println(tokens);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MaxByTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

我建议为此使用 ArrayList,如果需要,您可以随时将其转换为字符串数组:

String[]

试试这个:

    public void readFromFile(String path) {

    File file = new File(path);

    ArrayList<String[]> tokens = new ArrayList<String[]>(); //The reason why we store an array of strings is only because of the split method below.
    //also, why are you using split? if i were you i would totally avoid using split at all. if that is the case then you should change the above arrayList to this:
    //ArrayList<String> tokens = new ArrayList<String>();

    String inputLine; //the line to be read

    try (BufferedReader br = new BufferedReader(new FileReader(file))) { //use the "enhanced" try-catch that way you don't have to worry about closing the stream yourself. 

        while ((inputLine = br.readLine()) != null) { //check line
            tokens.add(inputLine.trim().split("\s+")); //put in the above arraylist
        }

    } catch (Exception e) {
        e.printStackTrace();
    }


    //Testing
    for (String[] token : tokens) {
        System.out.println(Arrays.toString(token));
    }

}

我认为你的问题是你使用令牌数组的方式。

按照 NullOverFlow 的建议使用 ArrayList 将提供您想要的行为。

这是一个使用 ArrayList 的快速解决方案,Raghu K Nair 的建议是采用整行而不是拆分。已完成 - 您可以 运行 自己验证:

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

public class tokenize
{
    public static List<String> readFile( String fileName )
    {
        FileInputStream fileStrm = null;
        InputStreamReader reader = null;
        BufferedReader buffReader = null;
        List<String> tokens = null;
        try
        {
            // Set up buffered reader to read file stream.
            fileStrm = new FileInputStream( fileName );
            reader = new InputStreamReader( fileStrm );
            buffReader = new BufferedReader( reader );
            // Line buffer.
            String line;
            // List to store results.
            tokens = new ArrayList<String>(); 

            // Get first line.
            line = buffReader.readLine();
            while( line != null )
            {
                // Add this line to the List.
                tokens.add( line );
                // Get the next line.
                line = buffReader.readLine();
            }
        }
        catch( IOException e )
        {
            // Handle exception and clean up.
            if ( fileStrm != null )
            {
                try { fileStrm.close(); }
                catch( IOException e2 ) { }
            }
        }
        return tokens;
    }

    public static void main( String[] args )
    {
        List<String> tokens = readFile( "foo.txt" );
        // You can use a for each loop to iterate through the List.
        for( String tok : tokens )
        {
            System.out.println( tok );
        }
    }
}

这依赖于按照您的问题所述格式化的文本文件。