如何将文件保存为Java中的字符串数组?
我想按行保存一个文件作为一个字符串数组,但我是一个初学者和困惑。如何将文件保存为Java中的字符串数组?
package com.java24hours;
import java.io.*;
public class ShorteningVelocity {
public static void main(String[] arguments) throws IOException {
FileReader SVfile = new FileReader(new File("C:\\Documents\\10-27-15110mMKPSS3.dat"));
BufferedReader br = new BufferedReader(SVfile);
String temp = br.readLine();
while (temp != null) {
temp = br.readLine(); //reads file line by line
System.out.println();
}
}
}
Java数组的大小不能增长,因此您使用了一个列表(来自java.util)。这些列表动态增长,因此您可以随意使用add()
。由于列表可能包含所有内容,因此可以使用List<TypeOfWhatYouWantToStore>
指定要包含的内容。因此,List类被称为泛型类。
将它转换为数组(因为这是你想要的)有点奇怪。您分配列表大小的阵列,然后在列表上使用toArray
。这将返回列表,并转换为数组。它必须将数组作为参数,因为编译器需要使用泛型进行编译。
package com.java24hours;
import java.io.*;
import java.util.*;
public class ShorteningVelocity {
public static void main(String[] arguments) throws IOException {
FileReader SVfile = new FileReader(new File("C:\\Documents\\10-27-15110mMKPSS3.dat"));
BufferedReader br = new BufferedReader(SVfile);
String temp = br.readLine();
List<String> tmpList = new ArrayList<>();
while (temp != null) {
tmpList.add(temp);
temp = br.readLine(); //reads file line by line
}
String[] myArray = new String[tmpList.size()];
myArray = tmpList.toArray(myArray);
}
}
编辑:使用Files.readAllLines()
(见你的问题的意见)是更容易和更快的可能,但使用这个循环中,你可以看到更多的是怎么回事的。
编辑2:更常见的是使用这种循环:
String temp;
List<String> tmpList = new ArrayList<>();
while ((temp = br.readLine()) != null) {
tmpList.add(temp);
}
环路现在做了以下内容:
- 读
br.readLine()
到temp
- 如果
temp
为空,离开循环 - 如果
temp
不为空,则添加i吨至列表
您还可以通过执行打印数组:
for (String str : myArray) {
System.out.println(str);
}
*“交换循环内部的行更为常见”*将“null”作为列表的最后一个元素添加为“更常见”?您应该使用适当的'while'循环。 – Tom
哦对了......修理它 –
@Tom应该现在好吧 –
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("C:\\Documents\\10-27-15110mMKPSS3-out.dat");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter outFile = new PrintWriter(bw);
FileReader SVfile = new FileReader(new File("C:\\Documents\\10-27-15110mMKPSS3.dat"));
BufferedReader br = new BufferedReader(SVfile);
String temp;
while ((temp = br.readLine()) != null) {
//reads file line by line
System.out.println();
outFile.println(temp);
}
outFile.close();
} catch (IOException e) {
}
}
这是伟大的谢谢! – ChibiChibi
ChibiChibi,如果我的回答是最好的,你能检查我的答案吗? – sgrillon
public static void main(String[] args){
List<String> tmpList = new ArrayList<>();
try {
FileReader fileReader = new FileReader(new File("D:\\input.txt"));
BufferedReader bufferedReader = new BufferedReader(fileReader);
String temp;
while ((temp = bufferedReader.readLine()) != null) {
tmpList.add(temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] myArray = new String[tmpList.size()];
myArray = tmpList.toArray(myArray);
for(int i = 0; i< myArray.length ; i ++){
System.out.println(myArray[i]);
}
至于你的问题,我也搞不清楚初学者...你有什么问题吗?你有什么问题? – Tom
使用['Files.readAllLines()'](http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#readAllLines-java.nio.file.Path- java.nio.charset.Charset-)。 – fge