[英]Tokenizing data from textfile
我有一個文本文件
1個
2
3
4
我正在嘗試將每行數據標記為一個數組。 但是,令牌[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());
}
}
}
我認為您的問題是您使用令牌數組的方式。
建議將ArrayList用作NullOverFlow將提供所需的行為。
這是一個使用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 );
}
}
}
這依賴於問題描述格式的文本文件。
我認為這可以解決您的問題
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));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.