Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中将文件保存为字符串数组?_Java_Arrays_String - Fatal编程技术网

如何在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]);
    }