1 62 4 55 5 6 77
我希望将它存储为一个数组列表,形如[1, 62, 4, 55, 5, 6, 77]
。在Java中应该怎么做?
1 62 4 55 5 6 77
我希望将它存储为一个数组列表,形如[1, 62, 4, 55, 5, 6, 77]
。在Java中应该怎么做?
您可以使用Files#readAllLines()
将文本文件的所有行读入List<String>
中。
for (String line : Files.readAllLines(Paths.get("/path/to/file.txt"))) {
// ...
}
教程:基本输入输出 > 文件输入输出 > 读取、写入和创建文本文件
您可以使用String#split()
根据正则表达式将String
分割成多个部分。
for (String part : line.split("\\s+")) {
// ...
}
你可以使用Integer#valueOf()
将一个String
转换为Integer
。
Integer i = Integer.valueOf(part);
您可以使用List#add()
将元素添加到List
中。
numbers.add(i);
因此,简而言之(假设文件没有空行或前导/尾随空格)。
List<Integer> numbers = new ArrayList<>();
for (String line : Files.readAllLines(Paths.get("/path/to/file.txt"))) {
for (String part : line.split("\\s+")) {
Integer i = Integer.valueOf(part);
numbers.add(i);
}
}
如果您已经使用Java 8,则可以使用Stream API,并从Files#lines()
开始进行操作。
List<Integer> numbers = Files.lines(Paths.get("/path/to/test.txt"))
.map(line -> line.split("\\s+")).flatMap(Arrays::stream)
.map(Integer::valueOf)
.collect(Collectors.toList());
Java 1.5引入了Scanner类,用于处理从文件和流中读取的输入。
它被用来从文件中获取整数,代码看起来会像这样:
List<Integer> integers = new ArrayList<Integer>();
Scanner fileScanner = new Scanner(new File("c:\\file.txt"));
while (fileScanner.hasNextInt()){
integers.add(fileScanner.nextInt());
}
不过请查看API。有许多处理不同类型的输入源、不同分隔符和不同数据类型的选项。
import java.io.*;
/**
* This example code shows you how to read file in Java
*
* IN MY CASE RAILWAY IS MY TEXT FILE WHICH I WANT TO DISPLAY YOU CHANGE WITH YOUR OWN
*/
public class ReadFileExample
{
public static void main(String[] args)
{
System.out.println("Reading File from Java code");
//Name of the file
String fileName="RAILWAY.txt";
try{
//Create object of FileReader
FileReader inputFile = new FileReader(fileName);
//Instantiate the BufferedReader Class
BufferedReader bufferReader = new BufferedReader(inputFile);
//Variable to hold the one line data
String line;
// Read file line by line and print on the console
while ((line = bufferReader.readLine()) != null) {
System.out.println(line);
}
//Close the buffer reader
bufferReader.close();
}catch(Exception e){
System.out.println("Error while reading file line by line:" + e.getMessage());
}
}
}
看这个例子,然后尝试做自己的:
import java.io.*;
public class ReadFile {
public static void main(String[] args){
String string = "";
String file = "textFile.txt";
// Reading
try{
InputStream ips = new FileInputStream(file);
InputStreamReader ipsr = new InputStreamReader(ips);
BufferedReader br = new BufferedReader(ipsr);
String line;
while ((line = br.readLine()) != null){
System.out.println(line);
string += line + "\n";
}
br.close();
}
catch (Exception e){
System.out.println(e.toString());
}
// Writing
try {
FileWriter fw = new FileWriter (file);
BufferedWriter bw = new BufferedWriter (fw);
PrintWriter fileOut = new PrintWriter (bw);
fileOut.println (string+"\n test of read and write !!");
fileOut.close();
System.out.println("the file " + file + " is created!");
}
catch (Exception e){
System.out.println(e.toString());
}
}
}
仅供娱乐,以下是我在真实项目中可能会做的事情,其中我已经使用了我最喜欢的库(在这种情况下是Guava,以前被称为Google Collections)。
String text = Files.toString(new File("textfile.txt"), Charsets.UTF_8);
List<Integer> list = Lists.newArrayList();
for (String s : text.split("\\s")) {
list.add(Integer.valueOf(s));
}
好处:几乎没有自己维护的代码(与这个相比)。编辑:虽然值得注意的是,在这种情况下,tschaible的Scanner解决方案没有更多的代码!
缺点:显然,您可能不想仅为此添加新的库依赖项。 (再说一遍,如果您的项目中不使用Guava,那么您就太傻了;-)
使用 Apache Commons(IO和Lang)来处理类似这样的简单/常见事情。
导入:
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
代码:
String contents = FileUtils.readFileToString(new File("path/to/your/file.txt"));
String[] array = ArrayUtils.toArray(contents.split(" "));
使用Java 7和NIO.2读取文件:
导入以下包:
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Path file = Paths.get("C:\\Java\\file.txt");
if(Files.exists(file) && Files.isReadable(file)) {
try {
// File reader
BufferedReader reader = Files.newBufferedReader(file, Charset.defaultCharset());
String line;
// read each line
while((line = reader.readLine()) != null) {
System.out.println(line);
// tokenize each number
StringTokenizer tokenizer = new StringTokenizer(line, " ");
while (tokenizer.hasMoreElements()) {
// parse each integer in file
int element = Integer.parseInt(tokenizer.nextToken());
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
一次性读取文件的所有行:
Path file = Paths.get("C:\\Java\\file.txt");
List<String> lines = Files.readAllLines(file, StandardCharsets.UTF_8);
private List<Integer> readIntegers(File file) throws IOException {
List<Integer> result = new ArrayList<>();
RandomAccessFile raf = new RandomAccessFile(file, "r");
byte buf[] = new byte[16 * 1024];
final FileChannel ch = raf.getChannel();
int fileLength = (int) ch.size();
final MappedByteBuffer mb = ch.map(FileChannel.MapMode.READ_ONLY, 0,
fileLength);
int acc = 0;
while (mb.hasRemaining()) {
int len = Math.min(mb.remaining(), buf.length);
mb.get(buf, 0, len);
for (int i = 0; i < len; i++)
if ((buf[i] >= 48) && (buf[i] <= 57))
acc = acc * 10 + buf[i] - 48;
else {
result.add(acc);
acc = 0;
}
}
ch.close();
raf.close();
return result;
}
读取文件,然后随心所欲地操作 Java8 Files.lines(Paths.get("c://lines.txt")).collect(Collectors.toList());