Java 从文本文件标记数据
我有一个文本文件 一, 二, 三, 四, 我试图将每行数据标记为一个数组。但是,令牌[0]正在读取1 2 3 4。我怎样才能做到这一点呢 我的代码基本上出了什么问题Java 从文本文件标记数据,java,io,bufferedreader,Java,Io,Bufferedreader,我有一个文本文件 一, 二, 三, 四, 我试图将每行数据标记为一个数组。但是,令牌[0]正在读取1 2 3 4。我怎样才能做到这一点呢 我的代码基本上出了什么问题 public static void readFile() { BufferedReader fileIn; String[] tokens; String inputLine; try { fileIn =
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);
}
}
我想这可能会解决你的问题
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));
}
}
我建议使用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 );
}
}
}
这依赖于您问题中所述的文本文件格式。我认为您的问题在于您使用令牌数组的方式 按照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 );
}
}
}
这取决于文本文件的格式,如您的问题所述。文件中是否有行分隔符?如果是,则不应拆分行,您可以读取每行并分配给令牌[i]文件中是否有行分隔符?如果是,则您不应拆分行,您可以读取每行并分配给令牌[i]