Java读取文件并将文本存储在数组中
我知道如何使用Java读取文件并将文本存储在数组中,java,arrays,file-io,storing-data,Java,Arrays,File Io,Storing Data,我知道如何使用Java和Scanner读取文件,但我唯一不知道的是如何将文本作为数组存储在文件中 下面是我的代码的片段: public static void main(String[] args) throws IOException{ // TODO code application logic here // // read KeyWestTemp.txt // create token1 String token1 = ""; // for
Java
和Scanner
读取文件,但我唯一不知道的是如何将文本作为数组存储在文件中
下面是我的代码的片段
:
public static void main(String[] args) throws IOException{
// TODO code application logic here
// // read KeyWestTemp.txt
// create token1
String token1 = "";
// for-each loop for calculating heat index of May - October
// create Scanner inFile1
Scanner inFile1 = new Scanner(new File("KeyWestTemp.txt"));
// while loop
while(inFile1.hasNext()){
// how can I create array from text read?
// find next line
token1 = inFile1.nextLine();
以下是我的KeyWestTemp.txt
文件包含的内容:
70.3, 70.8, 73.8, 77.0, 80.7, 83.4, 84.5, 84.4, 83.4, 80.2, 76.3, 72.0
编辑
看起来您想要创建一个浮点数组,为此创建一个浮点数组
int count = -1;
Float[] content = new Float[200];
while(inFile1.hasNext()){
content[++count] = Float.parseFloat(inFile1.nextLine());
}
那么您的浮点数组将如下所示
content[0] = 70.3
content[1] = 70.8
content[2] = 73.8
content[3] = 77.0 and so on
while(infie1.hasNext()){
token1=inFile1.nextLine();
//将每个值放入带有字符串#split()的数组中;
String[]numStrings=line.split(“,”);
//将数字字符串解析为双精度
double[]nums=新的double[numString.length];
对于(int i=0;i
只需将整个文件读入StringBuilder,然后在空格后按点分割字符串。您将得到一个字符串数组
Scanner inFile1 = new Scanner(new File("KeyWestTemp.txt"));
StringBuilder sb = new Stringbuilder();
while(inFile1.hasNext()) {
sb.append(inFile1.nextLine());
}
String[] yourArray = sb.toString().split(", ");
如果您不知道文件中的行数,那么就没有用于初始化数组的大小。在这种情况下,使用列表更有意义:
List<String> tokens = new ArrayList<String>();
while (inFile1.hasNext()) {
tokens.add(inFile1.nextLine());
}
我发现这种从文件中读取字符串的方法最适合我
String st, full;
full="";
BufferedReader br = new BufferedReader(new FileReader(URL));
while ((st=br.readLine())!=null) {
full+=st;
}
“完整”是所有线路的完整组合。如果要在文本行之间添加换行符,可以这样做
full+=st+“\n”代码>存储为字符串:
public class ReadTemps {
public static void main(String[] args) throws IOException {
// TODO code application logic here
// // read KeyWestTemp.txt
// create token1
String token1 = "";
// for-each loop for calculating heat index of May - October
// create Scanner inFile1
Scanner inFile1 = new Scanner(new File("KeyWestTemp.txt")).useDelimiter(",\\s*");
// Original answer used LinkedList, but probably preferable to use ArrayList in most cases
// List<String> temps = new LinkedList<String>();
List<String> temps = new ArrayList<String>();
// while loop
while (inFile1.hasNext()) {
// find next line
token1 = inFile1.next();
temps.add(token1);
}
inFile1.close();
String[] tempsArray = temps.toArray(new String[0]);
for (String s : tempsArray) {
System.out.println(s);
}
}
}
公共类ReadTemps{
公共静态void main(字符串[]args)引发IOException{
//此处的TODO代码应用程序逻辑
////读取KeyWestTemp.txt
//创建令牌1
字符串标记1=“”;
//用于计算5月至10月热量指数的每个回路
//创建扫描仪内嵌1
扫描仪填充1=新扫描仪(新文件(“KeyWestTemp.txt”)。使用分隔符(“,\\s*”);
//最初的答案使用LinkedList,但在大多数情况下可能更倾向于使用ArrayList
//List temps=new LinkedList();
List temps=new ArrayList();
//while循环
while(inFile1.hasNext()){
//查找下一行
token1=inFile1.next();
临时添加(标记1);
}
inFile1.close();
字符串[]tempsArray=temps.toArray(新字符串[0]);
用于(字符串s:tempsArray){
系统输出打印项次;
}
}
}
对于浮动:
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class ReadTemps {
public static void main(String[] args) throws IOException {
// TODO code application logic here
// // read KeyWestTemp.txt
// create token1
// for-each loop for calculating heat index of May - October
// create Scanner inFile1
Scanner inFile1 = new Scanner(new File("KeyWestTemp.txt")).useDelimiter(",\\s*");
// Original answer used LinkedList, but probably preferable to use ArrayList in most cases
// List<Float> temps = new LinkedList<Float>();
List<Float> temps = new ArrayList<Float>();
// while loop
while (inFile1.hasNext()) {
// find next line
float token1 = inFile1.nextFloat();
temps.add(token1);
}
inFile1.close();
Float[] tempsArray = temps.toArray(new Float[0]);
for (Float s : tempsArray) {
System.out.println(s);
}
}
}
导入java.io.File;
导入java.io.IOException;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Scanner;
公共类ReadTemps{
公共静态void main(字符串[]args)引发IOException{
//此处的TODO代码应用程序逻辑
////读取KeyWestTemp.txt
//创建令牌1
//用于计算5月至10月热量指数的每个回路
//创建扫描仪内嵌1
扫描仪填充1=新扫描仪(新文件(“KeyWestTemp.txt”)。使用分隔符(“,\\s*”);
//最初的答案使用LinkedList,但在大多数情况下可能更倾向于使用ArrayList
//List temps=new LinkedList();
List temps=new ArrayList();
//while循环
while(inFile1.hasNext()){
//查找下一行
float token1=inFile1.nextFloat();
临时添加(标记1);
}
inFile1.close();
Float[]tempsArray=temps.toArray(新的Float[0]);
用于(浮动s:tempsArray){
系统输出打印项次;
}
}
}
我使用这种方法:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class TEST {
static Scanner scn;
public static void main(String[] args) {
String text = "";
try{
scn = new Scanner(new File("test.txt"));
}catch(FileNotFoundException ex){System.out.println(ex.getMessage());}
while(scn.hasNext()){
text += scn.next();
}
String[] arry = text.split(",");
//if need converting to float do this:
Float[] arrdy = new Float[arry.length];
for(int i = 0; i < arry.length; i++){
arrdy[i] = Float.parseFloat(arry[i]);
}
System.out.println(Arrays.toString(arrdy));
}
}
import java.util.Scanner;
导入java.io.File;
导入java.io.FileNotFoundException;
公开课考试{
静态扫描仪;
公共静态void main(字符串[]args){
字符串文本=”;
试一试{
scn=新扫描仪(新文件(“test.txt”);
}catch(FileNotFoundException ex){System.out.println(ex.getMessage());}
while(scn.hasNext()){
text+=scn.next();
}
字符串[]arry=text.split(“,”);
//如果需要转换为浮动,请执行以下操作:
Float[]arrdy=新的Float[arry.length];
对于(int i=0;i
文件中的行数超过200行时就会崩溃。预增量是怎么回事?count将包含读取行数-1…是的,假设200行,我通常将内容存储在一个数组中。是计数将存储读取的行数。在您的情况下,否,读取1行时计数为0我通常将内容存储在一个数组中
。我不理解这一部分。这是很明显的,我一直在计算,以便在将来需要时对循环有所帮助。请您更详细地解释一下您的意思,我对Java编程相当陌生。编辑并添加了示例。new LinkedList()代码>在这种情况下使用linkedlist的理由是什么?@njzk2-Hmm,我认为没有什么好的理由。ArrayList可能更可取,除非列表中添加了未知的大量温度。更新了我的答案。我认为好的示例还应该关闭infire1
扫描仪(从而关闭输入文件)。如果这是您在main
中所做的全部操作,这并不重要,但在更大的应用程序中,这可能会成为一个问题。即使是在100kb-1mb范围内的小文件上,速度也非常慢。您可以在这里看到一些统计信息,而不是使用stringbuilder!为什么要使用扫描仪
以预拆分的方式读取文件,只是为了连接零件并再次拆分它们?为什么要在没有任何新的好主意的情况下,对3年前的问题添加这样的答案?还请注意,代码中还有其他“异味”。为什么scn
是静态成员而不仅仅是局部变量?如果存在FileNotFoundException
,会发生什么情况?(提示:NullPointerException)。如果此代码段将在更大的应用程序中使用,则不关闭扫描仪也不是一个好主意。
public class ReadTemps {
public static void main(String[] args) throws IOException {
// TODO code application logic here
// // read KeyWestTemp.txt
// create token1
String token1 = "";
// for-each loop for calculating heat index of May - October
// create Scanner inFile1
Scanner inFile1 = new Scanner(new File("KeyWestTemp.txt")).useDelimiter(",\\s*");
// Original answer used LinkedList, but probably preferable to use ArrayList in most cases
// List<String> temps = new LinkedList<String>();
List<String> temps = new ArrayList<String>();
// while loop
while (inFile1.hasNext()) {
// find next line
token1 = inFile1.next();
temps.add(token1);
}
inFile1.close();
String[] tempsArray = temps.toArray(new String[0]);
for (String s : tempsArray) {
System.out.println(s);
}
}
}
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class ReadTemps {
public static void main(String[] args) throws IOException {
// TODO code application logic here
// // read KeyWestTemp.txt
// create token1
// for-each loop for calculating heat index of May - October
// create Scanner inFile1
Scanner inFile1 = new Scanner(new File("KeyWestTemp.txt")).useDelimiter(",\\s*");
// Original answer used LinkedList, but probably preferable to use ArrayList in most cases
// List<Float> temps = new LinkedList<Float>();
List<Float> temps = new ArrayList<Float>();
// while loop
while (inFile1.hasNext()) {
// find next line
float token1 = inFile1.nextFloat();
temps.add(token1);
}
inFile1.close();
Float[] tempsArray = temps.toArray(new Float[0]);
for (Float s : tempsArray) {
System.out.println(s);
}
}
}
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class TEST {
static Scanner scn;
public static void main(String[] args) {
String text = "";
try{
scn = new Scanner(new File("test.txt"));
}catch(FileNotFoundException ex){System.out.println(ex.getMessage());}
while(scn.hasNext()){
text += scn.next();
}
String[] arry = text.split(",");
//if need converting to float do this:
Float[] arrdy = new Float[arry.length];
for(int i = 0; i < arry.length; i++){
arrdy[i] = Float.parseFloat(arry[i]);
}
System.out.println(Arrays.toString(arrdy));
}
}