如何在Java中将文件保存为字符串数组?
我试图将文件逐行保存为字符串数组,但我是一个初学者,而且很困惑如何在Java中将文件保存为字符串数组?,java,arrays,string,Java,Arrays,String,我试图将文件逐行保存为字符串数组,但我是一个初学者,而且很困惑 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\\1
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
指定希望它包含的内容。因此,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);
}
}
循环现在正在执行以下操作:
- 将
读入br.readLine()
temp
- 如果
为空,则退出循环temp
- 如果
不为空,则将其添加到列表中temp
for (String str : myArray) {
System.out.println(str);
}
publicstaticvoidmain(字符串[]args){
List tmpList=new ArrayList();
试一试{
FileReader FileReader=新的FileReader(新文件(“D:\\input.txt”);
BufferedReader BufferedReader=新的BufferedReader(文件阅读器);
字符串温度;
而((temp=bufferedReader.readLine())!=null){
tmpList.add(温度);
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
String[]myArray=新字符串[tmpList.size()];
myArray=tmpList.toArray(myArray);
for(int i=0;i
作为初学者,我对你的问题也感到困惑……你的问题是什么?哪里有问题?使用。“在循环中交换行更常见”这是“更常见”要将null
作为列表的最后一个元素添加到列表中?您应该使用适当的while
循环。哦,对了it@Tom现在应该没事了这太好了谢谢!!ChibiChibi,如果我的答案是最好的,你能检查一下我的答案吗?
for (String str : myArray) {
System.out.println(str);
}
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) {
}
}
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]);
}