gpt4 book ai didi

java - 将数据从文件打印到数组

转载 作者:行者123 更新时间:2023-12-02 00:14:33 24 4
gpt4 key购买 nike

我需要将此文件打印到数组,而不是屏幕。是的,我必须使用数组 - 学校项目 - 我对 java 很陌生,因此不胜感激。有任何想法吗?谢谢

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class HangmanProject
{
public static void main(String[] args) throws FileNotFoundException
{

String scoreKeeper; // to keep track of score
int guessesLeft; // to keep track of guesses remaining
String wordList[]; // array to store words


Scanner keyboard = new Scanner(System.in); // to read user's input

System.out.println("Welcome to Hangman Project!");

// Create a scanner to read the secret words file
Scanner wordScan = null;

try {
wordScan = new Scanner(new BufferedReader(new FileReader("words.txt")));
while (wordScan.hasNext()) {
System.out.println(wordScan.next());
}
} finally {
if (wordScan != null) {
wordScan.close();
}
}
}
}

最佳答案

尼克,你刚刚给了我们最后一 block 拼图。如果您知道要读取的行数,则可以在读取文件之前简单地定义该长度的数组

类似...

String[] wordArray = new String[10];
int index = 0;
String word = null; // word to be read from file...
// Use buffered reader to read each line...
wordArray[index] = word;
index++;

老实说,这个例子没有多大意义,所以我做了这两个例子

第一个使用 Alex 建议的概念,它允许您从文件中读取未知数量的行。

唯一的问题是行之间被更多的一个换行符分隔(即单词之间有一个额外的行)

public static void readUnknownWords() {

// Reference to the words file
File words = new File("Words.txt");
// Use a StringBuilder to buffer the content as it's read from the file
StringBuilder sb = new StringBuilder(128);

BufferedReader reader = null;
try {

// Create the reader. A File reader would be just as fine in this
// example, but hay ;)
reader = new BufferedReader(new FileReader(words));
// The read buffer to use to read data into
char[] buffer = new char[1024];
int bytesRead = -1;
// Read the file to we get to the end
while ((bytesRead = reader.read(buffer)) != -1) {

// Append the results to the string builder
sb.append(buffer, 0, bytesRead);

}

// Split the string builder into individal words by the line break
String[] wordArray = sb.toString().split("\n");

System.out.println("Read " + wordArray.length + " words");

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (Exception e) {
}
}

}

第二个演示如何将单词读入已知长度的数组中。这可能更接近您真正想要的

public static void readKnownWords() 

// This is just the same as the previous example, except we
// know in advance the number of lines we will be reading
File words = new File("Words.txt");

BufferedReader reader = null;
try {

// Create the word array of a known quantity
// The quantity value could be defined as a constant
// ie public static final int WORD_COUNT = 10;
String[] wordArray = new String[10];

reader = new BufferedReader(new FileReader(words));
// Instead of reading to a char buffer, we are
// going to take the easy route and read each line
// straight into a String
String text = null;
// The current array index
int index = 0;
// Read the file till we reach the end
// ps- my file had lots more words, so I put a limit
// in the loop to prevent index out of bounds exceptions
while ((text = reader.readLine()) != null && index < 10) {

wordArray[index] = text;
index++;

}

System.out.println("Read " + wordArray.length + " words");

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (Exception e) {
}
}

}

如果您发现其中任何一个有用,我会适本地给我一个小小的赞成票,并检查亚历克斯的答案是否正确,因为我已经适应了他的想法。

现在,如果您真的对使用哪个换行符很担心,您可以通过 System.getProperties().getProperty("line.separator") 找到系统使用的值值。

关于java - 将数据从文件打印到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12103518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com