【问题标题】:How can I save a file as a string array in Java? [duplicate]如何在 Java 中将文件保存为字符串数组? [复制]
【发布时间】:2015-11-06 17:52:25
【问题描述】:

我正在尝试将文件逐行保存为字符串数组,但我是初学者并且很困惑。

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 arrays string


【解决方案1】:

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()(请参阅有关您的问题的 cmets)更容易并且可能更快,但是使用此循环,您可以看到更多正在发生的事情。

编辑2:更常见的是使用这个循环:

        String temp;
        List<String> tmpList = new ArrayList<>();
        while ((temp = br.readLine()) != null) { 
            tmpList.add(temp);
        }

循环现在正在执行以下操作:

  • br.readLine()读入temp
  • 如果temp 为空,则退出循环
  • 如果temp不为空,则将其添加到列表中

您也可以通过以下方式打印数组:

for (String str : myArray) {
    System.out.println(str);
}

【讨论】:

  • “在循环内交换行更常见”null作为最后一个元素添加到列表中是“更常见”吗?您应该改用正确的 while 循环。
  • 哦对....修复它
  • @Tom 现在应该没事了
【解决方案2】:
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,如果我的答案是最好的,你能检查我的答案吗?
【解决方案3】:
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]);
    }

【讨论】:

    猜你喜欢
    • 2014-10-25
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多